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.

Compare two arrays and return unmatched elements in both arrays?

Shantharao
Kilo Sage

Hello Experts,

I have a list collector field on the Change Request form which references to sys_user_group table
For example, the current field holds three groups called "A, B, and C" now I have added "D, E" groups manually 
Previous list collector fields array values = [A, B, C]
Current list collector fields array values = [A, B, C, D, E]
The requirement is to fetch newly added groups and post them in the change request audit history, so in the current scenario D, E should be set to "work_notes" field

 

Scenario: I want to compare the current list array values with the previous list array and need to fetch unmatched array values
I have used the below logic but not working as expected 

 


var currentAuthorizationApproval = current.u_authorization_approval_groups.toString().split(",");
var previousAuthorizationApproval = previous.u_authorization_approval_groups.toString().split(",");

 

var final1 = getMatch(currentAuthorizationApproval, previousAuthorizationApproval);
gs.addInfoMessage(" final=>" + final1);

 

function getMatch(a, b) {
var matches = [];

for (var i = 0; i < a.length; i++) {
for (var e = 0; e < b.length; e++) {
if (a[i] != b[e]){
matches.push(a[i]);
gs.addInfoMessage("a[i]=>" +a[i]);
}
}
}
return matches;
}




1 ACCEPTED SOLUTION

kamlesh kjmar
Mega Sage

Hi @Shantharao ,

 

Below code should work:

 

var currentAuthorizationApproval = current.getValue("u_authorization_approval_groups").split(",");
var previousAuthorizationApproval = previous.getValue("u_authorization_approval_groups").split(",");
var arrayUtil = new ArrayUtil();
var final1 = arrayUtil.diff(currentAuthorizationApproval , previousAuthorizationApproval )
gs.addInfoMessage(" final=>" + final1);

 

I Hope this helps.

 

Please mark this helpful if this helps and Accept the solution if this solves your issue.

 

Regards,

Kamlesh

 

View solution in original post

4 REPLIES 4

Chinmay Tawade1
Tera Guru

Hi Shantharao,

 

Try the below code in function -

 

function getMatch(a, b) {
var matches = [];

for (var i = 0; i < a.length; i++) {
for (var e = 0; e < b.length; e++) {
if (a[i] != b[e]){
matches.push(a[i].toString());
gs.addInfoMessage("a[i]=>" +a[i]);
}
}
}
return matches;
}

 

Please mark this answer as Helpful if this works.

Thanks,

Chinmay

Hi Chinmay,

I have tried pushing as a string but no luck, is there any other way to fix this issue

 

Thanks

kamlesh kjmar
Mega Sage

Hi @Shantharao ,

 

Below code should work:

 

var currentAuthorizationApproval = current.getValue("u_authorization_approval_groups").split(",");
var previousAuthorizationApproval = previous.getValue("u_authorization_approval_groups").split(",");
var arrayUtil = new ArrayUtil();
var final1 = arrayUtil.diff(currentAuthorizationApproval , previousAuthorizationApproval )
gs.addInfoMessage(" final=>" + final1);

 

I Hope this helps.

 

Please mark this helpful if this helps and Accept the solution if this solves your issue.

 

Regards,

Kamlesh