Advanced Filtering of List Collector Variables - Dependent on a Value of Another Variable
A question I get a lot is how to filter a list collector variable - either on initial form load based on some complex criteria, or when a variable changes.
I'll start with a basic example and build from there.
Scenario 1: You have a reference variable 'Group' on sys_user_group and a list collector 'Users' on sys_user_grmember and want to only show Users that are a member of the selected Group. In the most basic sense, you could just add a reference qualifier to the users variable
javascript:"group=" + current.variables.u_group
where 'u_group' is the name of the 'Group' variable. Keep in mind that this only affects the list on form load and filter change, so if there is no default Group selected on the initial request form, the list of available Users will be empty. Once a group is selected, the Users list will only update when the filter criteria is changed - so you can type a letter in the search window, then remove it and the Users list will refresh, or you can add this to the Variable attributes field to have the filter update as soon as the group changes
no_filter = true,ref_qual_elements=u_group
The first gets rid of the buttons and fields in the filter to save space and prevent confusion since you're messing with the filter dynamically. The second makes the reference qualifier refresh when the 'Group' variable changes. Also keep in mind in this scenario that you probably want the User names to display in this list, not their sysids, so go to the table definition and change the Display of the User column to true.
Scenario 2: If your initial or on change filter criteria is more complex, here's the full onChange client script to set a list collector filter - in this case it was on the cmdb_ci_server table, and there is a script include that builds the applicable list after some queries and what not
var varName = 'v_assets';// name of the list collector variable
var filterString = '';
var apps = new GlideAjax('AssetFilter');// script include
//... variables and values to pass to script include...
apps.getXML(assetList);
function assetList(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
filterString = answer;
try{
var myListCollector = g_list.get(varName);
myListCollector.reset();
myListCollector.setQuery(filterString);
}
//Revert to Service Catalog method
catch(e){
window[varName + 'g_filter'].reset();
window[varName + 'g_filter'].setQuery(filterString);
window[varName + 'acRequest'](null);
}
}
The answer returned by the script include is a string in the format sys_idIN1...,2...,3...
With this method you have quite a bit of flexibility on filtering a list as can pass in any number of other variables and criteria for the script include to churn through, push into an array, then return a comma-separated list of sys_ids.
https://www.servicenow.com/community/itsm-articles/advanced-filtering-of-list-collector-variables-dependent-on-a/ta-p/2309426