Selecting Multiple Value in Market Field in x table

Dushyant Siroh
Tera Expert

There is a requirement to allow multiple selections for the Location field on the u_example table.
To address this, I am implementing a Script Include and using a Reference Qualifier with a List Collector type in the Market field.

Please note that the Location field is of String type and not a reference field.
Kindly review the attached screenshot and let me know if this approach is acceptable or if any changes are required.

Screenshot 2026-04-09 at 11.46.27 AM.png

 

Reference qualifier: javascript: new GetLocations().getUniqueLocations();

 

 

var GetLocations = Class.create();
GetLocations.prototype = {
    initialize: function () {},

    getUniqueLocations: function () {
        var gr = new GlideRecord('u_global_diageo_group_table');
        gr.addNotNullQuery('u_location');
        gr.orderBy('u_location');
        gr.query();

        var locations = [];
        while (gr.next()) {
            var loc = gr.getValue('u_location');
            if (locations.indexOf(loc) === -1) {
                locations.push(loc);
            }
        }

        if (locations.length === 0)
            return 'sys_idISEMPTY';

        return 'u_locationIN' + locations.join(',');
    },

    type: 'GetLocations'
};

 

1 REPLY 1

Naveen20
ServiceNow Employee

 

Your Script Include deduplicates the string values, but the returned encoded query (u_locationINNew York,London,...) will still match all records in u_global_diageo_group_table that have those locations. So if 10 records share u_location = "New York", all 10 appear in the List Collector — you still get duplicates.

Why: A List Collector operates on records (sys_ids), not raw string values. The reference qualifier filters which records to show, not which distinct strings.

Recommended fix — two options:

  1. Create a dedicated lookup table (e.g., u_markets) with unique location entries. Point the List Collector's reference to that table. No Script Include needed — the list is inherently deduplicated. This is the cleanest, most maintainable approach.

  2. If you must use the existing table, change the Script Include to return one sys_id per unique location using addEncodedQuery('GROUP BYu_location') or track seen values and collect only the first sys_id per location:

getUniqueLocations: function () {
    var gr = new GlideRecord('u_global_diageo_group_table');
    gr.addNotNullQuery('u_location');
    gr.orderBy('u_location');
    gr.query();

    var seen = {};
    var sysIds = [];
    while (gr.next()) {
        var loc = gr.getValue('u_location');
        if (!seen[loc]) {
            seen[loc] = true;
            sysIds.push(gr.getUniqueValue());
        }
    }

    if (sysIds.length === 0) return 'sys_idISEMPTY';
    return 'sys_idIN' + sysIds.join(',');
}

This returns sys_idIN... so only one record per unique location appears. But option 1 is strongly preferred — using a proper reference table avoids fragile workarounds and plays nicely with reporting, ACLs, and future changes.