Setting dynamic filter on list collector variable only works for a single list collector?

James Byrne
Tera Contributor

I have a requirement to implement a dynamic filter on two list collector variables on a single catalog item.

The filters need to be set based on the changing of 2 other variables, i.e. one variable changing dynamically sets the filter for one list collector, and the other variable changing sets the filter for the second list collector.

I came across the script on : https://www.servicenowguru.com/scripting/client-scripts-scripting/changing-filter-list-collector-var... 

It's working perfectly for setting one list collector, but when I create the second onChange client script for the second list collector it doesn't work at all. Only the first onChange script works. I've compared both scripts extensively an cannot see why one works and the other doesn't.

I came across this community article and see the author hit the same problem: https://community.servicenow.com/community?id=community_question&sys_id=885fbe69db58dbc01dcaf3231f96... 

"I tried putting both list collector variables in a single script and also tried in two different OnChange Scripts. But in both the cases; when I make the selection - only one filter is getting set!"

Any idea why only one of these scripts will run on a form?

I've added alerting and can see that the script does get to the end where it should reset the filter but the filter doesn't actually reset. I've also added info messages to the list collector variable just to be sure I had the variable name correct.

The dynamic resetting part does't appear to be working.

The non-working script is:

function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}

// the list collector field on the record producer
var collectorName = 'u_roles';
var filterString = 'active=true^u_company='+ g_form.getValue('u_company_code'); // build filter
var defaultFilter = 'active=true'; //build default filter
var company = g_form.getValue('u_company_code');
var selectedValue = g_form.getValue('u_roles_filter');

if(selectedValue == 'u_company_only' && company != ''){
//Reset the filter query

window[collectorName + 'g_filter'].reset(); // this resets any filters
window[collectorName + 'g_filter'].setQuery(filterString); //set the filter
window[collectorName + 'acRequest'](null); //runs the filter automatically
} else if(selectedValue == 'u_company_only' && company == ''){
g_form.showFieldMsg('u_roles_filter','Please ensure the "Company Code" field has been populated and try again.','info');
} else {
//Remove filter and set default
window[collectorName + 'g_filter'].reset(); // this resets any filters
window[collectorName + 'g_filter'].setQuery(defaultFilter); //set the default filter
window[collectorName + 'acRequest'](null); //runs the filter automatically

}
}

 

1 ACCEPTED SOLUTION

James Byrne
Tera Contributor

For anyone interested, I found the solution here: https://community.servicenow.com/community?id=community_blog&sys_id=c5c46364dbba6b007d3e02d5ca9619cb

 

It looks like the ServiceNow London release introduced a new field labelled "Isolate script" on all client side scripts like Client scripts, UI Policies, UI Actions, Catalog Client Scripts, Catalog UI Policies. It's set to TRUE for all new configurations by default.

This interferes with and prevents script running which contains DOM manipulation. Unfortunately ServiceNow currently doesn't contain an OOTB feature for dynamically setting list collector filters based on the changing of other variables, so DOM manipulation is the only way.

Setting the 'Isolate script' field to False on the broken script suddenly fixed it.

 

No idea why the other onChange scripts worked at all considering they are still set to 'Isolate script' = True.

 

Hopefully this helps others out.

View solution in original post

1 REPLY 1

James Byrne
Tera Contributor

For anyone interested, I found the solution here: https://community.servicenow.com/community?id=community_blog&sys_id=c5c46364dbba6b007d3e02d5ca9619cb

 

It looks like the ServiceNow London release introduced a new field labelled "Isolate script" on all client side scripts like Client scripts, UI Policies, UI Actions, Catalog Client Scripts, Catalog UI Policies. It's set to TRUE for all new configurations by default.

This interferes with and prevents script running which contains DOM manipulation. Unfortunately ServiceNow currently doesn't contain an OOTB feature for dynamically setting list collector filters based on the changing of other variables, so DOM manipulation is the only way.

Setting the 'Isolate script' field to False on the broken script suddenly fixed it.

 

No idea why the other onChange scripts worked at all considering they are still set to 'Isolate script' = True.

 

Hopefully this helps others out.