Compare value of list collector

JLeong
Mega Sage

Hi Guys,

Need some assistance here.....

I have a catalog request form that populates a list collector based on some fields selection. I need to hide this list collector if the listed values are exactly the same as the other list collector on the form.

How can I compare 2 list collectors?

Thank you in advance.

Regards,

Jocelyn

 

1 ACCEPTED SOLUTION

Hi,

You should not get that error.

 

For sample purpose : run this code in scripts background and check if this works 

var arrayUtil = new ArrayUtil();
var a1 = new Array("a", "b", "c");
var a2 = new Array("c", "d", "e");
gs.print(arrayUtil.diff(a1, a2));

If this works , then put your sample code also in scripts background and check . 

 

It shoudl work most likely. I would suggest putting some print statement for print list array1 and array2 in your code , it will help you to identify rootcause.

View solution in original post

7 REPLIES 7

Sagar Pagar
Tera Patron

Hi,

 

You need to take the list collector values in array

 

var arrayListCollector1 = [];

var arrayListCollector2 = [];

 

var arrayListCollector1 = current.variables.your_list_collector_variable1.toString().split(',');

var arrayListCollector2 = current.variables.your_list_collector_variable2.toString().split(',');

 

After that apply for loop.

 

    for (var i = 0; i < arrayListCollector1.length; i++) {

        if (arrayListCollector1[key] == (arrayListCollector2[key] )) {
            // write logic here
        }
        else{
            // write logic here
        }

 

Hope this will help you.

Regards,

Sagar Pagar

 

The world works with ServiceNow

Hi Sagar,

Thanks for your email. 

I am doing this using catalog client script and "current" is not valid.

 

Ct111
Giga Sage

Hi JLeong,

 

Convert the list collector to an array like below

 

var listcollector1 = your_field_name_here.toString();

 

var array1 = listcollector1.split(',');

 

 

var listcollector2 = your_field_name_here.toString();

 

var array2 = listcollector2.split(',');

 

And then compare both

 

var arryUtil = new ArrayUtil();

arryUtil.diff(array1,array2);

gs.info(arryUtil);

 

This will

Return an array of items from  array1 that were not found in either array 2. Duplicates are removed from the result.

 

Mark my ANSWER as CORRECT and HELPFUL if it works.

 

Hi,

Thanks for helping!!! I get an error: ArrayUtil is not defined