Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

How to compare two list fields

Indirakumar2
Tera Contributor

Hi,

I wanted to compare two list fields(u_groups,u_approver_groups) and return the left out value and update the list field "u_approver_groups" with the left out value.

 

Client Script

function onLoad() {
var requestedBy = g_form.getReference('cmdb_ci',setGroup);


}
function setGroup(requestedBy)
{
var array1=[];
array1=requestedBy.u_groups;//list field contains multiple groups
alert("The array1 is :"+array1);
var array2 = [];
array2= g_form.getValue('u_approver_groups');//list field in change form contains multiple groups
alert("The array2 is :"+array2);
var array3 = [];

for(var i = 0; i <= array1.length; i++)
{
if(array2.indexOf(array1[i]) == -1){
array3.push(array1[i]);

}
}
alert("The array3 is :"+array3);// It returns empty ,I want to return the groups which are present extra in array1

}

But works fine in the below scenario:

function onLoad() {

var array1=['0a52d3dcd7011200f2d224837e6103f2','3cc3c7680b982300cac6c08393673a03','2156c3a80b982300cac6c08393673a7e'];
var array2 = ['0a52d3dcd7011200f2d224837e6103f2','3cc3c7680b982300cac6c08393673a03'];
var array3 = [];
for(var i = 0; i <= array1.length; i++)
{
if(array2.indexOf(array1[i]) == -1){
array3.push(array1[i]);// This returns 2156c3a80b982300cac6c08393673a7e

}
}

}

Thanks in advance

1 ACCEPTED SOLUTION

Willem
Giga Sage
Giga Sage

Are you shure the values are typeOf Array? Can you try split them?

For example:

array1=requestedBy.u_groups.split(",");

View solution in original post

7 REPLIES 7

Thank you Jaspal for your kind gesture.

Apology Willem, unfortunately I didn't see the timing.Now , I have marked your answer correct.

Jaspal Singh
Mega Patron
Mega Patron

Hi Inderjit,

 

Try replacing

array1=requestedBy.u_groups; 

with

array1=requestedBy.u_groups.split(',');