Filtering out locations by region using a dynamic ref qualifier

Sam10
Tera Expert

Hi All, 

 

I have a requirement to work with two drop down fields in a service request (region and location), upon selecting a region (u_region) , locations (cmn_location) that are tied to that region should display. 

 

So far, I tried below with no luck, 

 

1. Created a script include (see below)

2. Created a dynamic reference qualifier and linked the script include

3. Updated the field with the dynamic ref qualifier created. 

 

Can you please highlight what am I missing here? 

 

Script Include

--------------------------------------------------------

var LocationsByRegion = Class.create();
LocationsByRegion.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getLocationsByRegion: function() {
var region = this.getParameter("sysparm_region");
var gr = new GlideRecord("cmn_location");
gr.addQuery("u_region", region);
gr.query();
var locations = [];
while (gr.next()) {
locations.push(gr.name.getDisplayValue());
}
return locations;
},
});

 

 

 

1 REPLY 1

Brad Bowman
Kilo Patron
Kilo Patron

Hi Sam,

It sounds like your "two drop down fields" are actually two variables on a Catalog Item that have the type of reference.  Assuming this is the case, since the reference table of the locations variable (cmn_location) contains a field (u_region) that is a reference to the selected region, all you need to do in this case is to add a reference qualifier to the locations variable that looks something like this: 

BradBowman_0-1675944052937.png

Using the name of your region variable obviously.

 

In cases where the relationship is not there it's good to have the Script Include approach in your tool belt.  The best way to do that is to use a reference qualifier that looks more like this:

BradBowman_1-1675944320087.png

Then in the Script Include, instead of getParameter like you use when a Client Script calls it, you declare the function with an argument for the value passed in from the qualifier, so yours would look like this:

getLocationsByRegion: function(region) {

The Script Include needs to return the value "sys_idIN1,2,3" to work with the reference qualifier in this format.  The value of a reference field, and those of the list of records is always a sys_id, even though it displays a name or something more meaningful, so the end of your script would look more like this:

    while (gr.next()) {
        locations.push(gr.sys_id.toString());
    }
    return 'sys_idIN' + locations.join(',');
  },
});

You can also do this by putting the 'sys_idIN' part in the reference qualifier.  The .join(',') on the array will happen by default if not specified, but it's good to be explicit to not get in the habit of assuming something will happen, then waste time troubleshooting in the case it doesn't happen when you thought it would.  The .toString() in the array push is critical since this is a sys_id.  Otherwise you'll get an array full of the same sys_id since you're telling it to push the memory location of that value to the array, or something like that.