- Post History
- Subscribe to RSS Feed
- Mark as New
- Mark as Read
- Bookmark
- Subscribe
- Printer Friendly Page
- Report Inappropriate Content
on 03-11-2020 07:11 AM
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.
- 11,064 Views
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
Brad, is your last script sets the values in the left bucket or right bucket?
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
This resets the list filter, which would be the values displayed in the Available / left bucket.
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
I have reference qualifier for variables itself and for right side I need to populate some cis selected somewhere else onChange, so I am using AJAX to get the list and setting the variable using g_form('variable_name', comma seperated sysids) but this is leading to following, not sure if I am doing it correctly
https://community.servicenow.com/community?id=community_question&sys_id=80f589911b0bb4549f20ece7624bcb3a
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
Hi Brad,
Your article directly addresses a current need of mine. Unfortunately, I'm not getting the results. I have an On Change script where I'm able to build the query string as follows
var theCompany = g_form.getValue("company");
var theType = g_form.getValue("type_of_access");
var thePair = theCompany + "-" + theType;
var theFilter = "u_item=673e47cd1b3e3990118ccbb4604bcbaa^u_variable=Role^u_value_1=" + thePair;
g_form.addInfoMessage("theFilter " + theFilter);
var theList = g_list.get("role_s");
theList.reset();
theList.setQuery(theFilter);
I've copied the string that gets produced from the info message and compared it to the query I get if I enter the criteria against the table and they are the same. I've also confirmed that if I have the quoted part of the filter (without appending thePair, that I get the full list of options.
Can you see/suggest what I'm doing wrong?
Thanks for any information you can provide,
:{)
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
@johnfeist by full list of options do you mean what you would expect to see without thePair, or an entire unfiltered list like it's ignoring it? Have you tried using the string from the info message as the reference qualifier (without the quotes)? I don't think this applies in this case, but it can't hurt - have you tried adding the variable that this script is running onChange of to the Variable attribute with ref_qual_elements? You're using this on a List Collector variable on a Service Portal/ESC Catalog Item, correct?
It seems like you might be able to do this with only a reference qualifier and attributes
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
Thanks Brad,
I did a little more "playing" based on your suggestions and now I have it working as needed.
:{)