logo

NJP

Filter List Collector with List collector

Import · Sep 02, 2016 · article

This time, the scenario was like this.

I have a list collector and what I choose there, should be a filter on list collector nr.2. Now biggest thing here was that I can choose multiple values in the first and these should be OR conditions on the next list collector.

Now, in my example here I want to first let the user choose which department the user should belong to and put that as a filter on the second list collector.

First I would say that there is a Pre-helsinki solution and a Helsinki solution(maybe)

image

Picture above is also using the "no_filter" to hide the filter.

Pre-Helsinki:

In the example above our first list collector is in a variable called "depart", So what we need to do is to make a

onChange client script that hits on the variable depart. When depart changes, the script changes the filter on the second list collector "users".

This is how the onChange script can look like:

function onChange(control, oldValue, newValue, isLoading) {

  if (isLoading) {

  return;

  }

  //Name of the list collector that the filter shall apply on

  var collectorName = 'users';

  //If the newValue isn't empty, build the filter

  if( newValue != '') {

  //Lets split up the string into an array

  var answer = newValue.split(',');

  var filterString = [];

  //First filter shouldnt start with an "or" so lets set it here with the first value

  filterString = 'department=' + answer[0];

  //If there is selected more than one department, put those in as "OR" conditions

  if(answer.length > 1){

  for (var i=1; i<answer.length;i++){

  filterString += 'ORdepartment=' + answer[i];

  }

  }

  eval(collectorName + 'g_filter.reset()');

  eval(collectorName + 'g_filter.setQuery("' + filterString + '")');

  eval(collectorName + 'acRequest(null)');

  }

  //If the newValue is empty, just reset the filter

  else{

  eval(collectorName + 'g_filter.reset()');

  eval(collectorName + 'acRequest(null)');

  }

}

The script above takes the new values and put it as a "OR" condition on the second list collector. And I've also added that if they clear the all made choices so the "newValue" is null, the filter is cleared.

Helsinki and beyond(hopefully)

In Helsinki we got the field "reference qual" which makes us do real nice stuff. I was hoping that with a easy line but I can't get "IN" to work and if anyone reading this know's why, I hope you can throw a comment and explain.

Let me show all what I mean.

First of all go to the second list collector and the "Default Value" section.

Fill in "Variable attribues" like this where "depart" is the first list collectors name.

image

Now, on the section "Type Specifications" type I was hope this would work:

image

But it doesn't and I can't really understand why.

I can do like this:

image

And it will work, but only if I choose 1 value from the list collector and that takes away the meaning with a list collector...

Might as well just have a reference field then.

If I get any solution for the ref qual, I'll update this post.

//Göran

image image

View original source

https://www.servicenow.com/community/developer-blog/filter-list-collector-with-list-collector/ba-p/2286781