Advanced Map Pages - Scripted Filter Items

Rob Greeley
Tera Contributor

I am trying to use the features of the "Advanced Map Pages" (https://docs.servicenow.com/bundle/tokyo-platform-user-interface/page/administer/navigation-and-ui/t...), in particular, I am trying to create a filter item that is a drop down of the countries in which the locations (cmn_location) are located.   The documentation for creating map page filter items is here:https://docs.servicenow.com/bundle/tokyo-platform-user-interface/page/administer/navigation-and-ui/t...

 

Since cmn_location.company is string field, not a choice field, I believe I need to use a script to build the filter item values, rather than referencing a data item.

 

Does anyone have an example script that they have used to build a Map Page Filter Item?

 

Per the documentation, the script to be included has to include a return in the following format:

 

/**
* set the answer with the format:
 {unique_index_of_the_selective_option: {
    "label": label_string,
    "val": the_value_of_the_option,
  },
  another_unique_index_of_the_selective_option: {
    "label": label_string,
    "val": the_value_of_the_option,

  },
* }
**/
answer = {};
 
Other than what is in the docs.service-now.com page, is there any other documentation or experience on how to use the Advanced Map Pages features?
 
Thank you
2 REPLIES 2

Rob Greeley
Tera Contributor

Here's answering my own question, the following script worked:

// Initialize the answer object
var answer = {};

/**
 * @function getUniqueValues
 * @description Get unique values for one field.
 * @Param table {string} - name of the table to be queried
 * @Param groupingAttribute {string} - name of the field that the unique values are needed
 * @Param encodedQuery {string} - encoded query to pre-filter the results
 * @returns {array} array of unique values, empty array if not found
 */
function getUniqueValues(table, groupingAttribute, encodedQuery) {
    var uniqueValues = [];

    var ga = new GlideAggregate(table);
    ga.addAggregate('COUNT');
    ga.groupBy(groupingAttribute);
    ga.addHaving('COUNT''>''0'); // get only values where count is more than 1

    // check if encoded query has been provided, if yes then it will be added to the GlideAggregate object
    if (encodedQuery !== undefined) {
        ga.addEncodedQuery(encodedQuery);
    }

    ga.query();

    while (ga.next()) {
        // check if the row is for a unique value and not for an overall count
        if (ga.getDisplayValue(groupingAttribute)) {
            uniqueValues.push(ga.getDisplayValue(groupingAttribute)); // add the value to the array
        }
    }

    return uniqueValues;
}

// Set the input table, field, and encodedQuery
var inputTable = 'insert_name_of_table';
var inputField = 'insert_field';
var inputEncodedQuery = 'Insert Encoded Query if Desired, else delete'// Replace with your encoded query if needed

// Call the getUniqueValues function to retrieve unique field values
var uniqueValues = getUniqueValues(inputTable, inputField, inputEncodedQuery);

// Sort the unique values alphabetically
uniqueValues.sort();

// Populate the answer object
for (var i = 0; i < uniqueValues.length; i++) {
    var uniqueIndex = "index_" + i; // Unique index based on loop iteration
    var value = uniqueValues[i];
    answer[uniqueIndex] = {
        "label": value,
        "val": value
    };
}

// Log the answer object along with inputTable and inputField
gs.info("Answer for " + inputTable + "." + inputField + ": " + JSON.stringify(answer));

Kenneth Zabrisk
Tera Guru

Thanks for posting this, it's a nice solution.   I've been very frustrated with how poor the documentation is for

Map Page Filter Item (and how poorly it works when the Data type is Data item ) and even more obtuse to me is the Map Filter Data Mapping.  I can see nothing it changes over what is already specified in the Map Page Filter Item.