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

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(",");

Thank you Willen, it helped.

Hi @Inderjit ,

 

I see you marked an answer as Correct. As it is the same as mine, but I was half an our earlier, can you please mark mine as correct? Since that is the fair thing to do and discourages duplicate responses.

Agreed. Inderjit kindly mark Willem's answer correct as the he suggested before me & strangely I see the comment was same.

So, since he answered first kindly mark Willem's answer correct.