Iterating through a List Collector and not displaying certain values

3.9k views Asked by At

I have a Service Catalog item in ServiceNow (Geneva release) that personnel use to request access to additional ServiceNow groups. A list collector displays the all of the groups available. We want to limit it and disallow a few choices from appearing. When you build a List Collector variable, you can specify a List table, but you cannot construct a filter on it.

Is it possible to use a Catalog Client Script to check each item being loaded into the List Collector and skip it, perhaps by checking sysID or another value? I assume a glide record call is being done somewhere in order to populate the list collector, but I'm unsure how to modify it or interrupt it.

For this example, the List Collector is named 'bucket'.

I can probably try to start with:

var lcFilter = g_form.getControl('bucket')

There is probably a way to manipulate the contents from that object. I just need help figuring out what part of the object I need to manipulate. We currently use a similar function to rename the headers of the List Collector like this:

var headers = g_form.getControl('bucket').parentElement.querySelectorAll('.col-xs-4');

headers[0].childNodes[0].firstChild.data = 'All Available Groups';
headers[1].childNodes[0]firstChild.data = 'Groups you wish to add';
1

There are 1 answers

4
Kirk On BEST ANSWER

You may want to try a solution that allows you to directly apply a filter to your list collector that I've used from http://www.servicenowguru.com/scripting/client-scripts-scripting/changing-filter-list-collector-variable-client-script/

The code below is a copy and paste from there, so I don't take too much credit, but you want to put your list collector name here

var collectorName = 'bucket';

Then an encoded query here to filter it on.

var filterString = 'group_nameNOT LIKEApproval^active=true'

This would be in a Catalog Client Script for onLoad

function onLoad() {
   //Apply a default filter to the list collector variable
   var collectorName = 'configuration_items';
   var filterString = 'name!=NULL^sys_class_nameANYTHING';
   //Hide the list collector until we've set the filter
   g_form.setDisplay(collectorName, false);
   setCollectorFilter();

   function setCollectorFilter(){
      //Test if the g_filter property is defined on our list collector.
      //If it hasn't rendered yet, wait 100ms and try again.
      if(typeof(window[collectorName + 'g_filter']) == 'undefined'){
         setTimeout(setCollectorFilter, 100);
         return;
      }
      //Find and hide the filter elements (optional)
      //Simple method for items with only one list collector
      //$('ep').select('.row')[0].hide();
      //Advanced method for items with more than one list collector (more prone to upgrade failure)
      //var el = $('container_' + g_form.getControl(collectorName).id).select('div.row')[0].hide();

      //Reset the filter query
      window[collectorName + 'g_filter'].reset();  
      window[collectorName + 'g_filter'].setQuery(filterString);  
      window[collectorName + 'acRequest'](null);  
      //Redisplay the list collector variable
      g_form.setDisplay(collectorName, true);
   }
}