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 lists ... Group memberships.

Steven Watts2
Tera Guru

I have a scenario whereby I need to compare two lists to confirm that all values, separated by a comma, in list one appears in list two. For example, see the output from the below script, I need to check if the values in the first line appear in the second.

 

Is there a way of doing this whereby I don't have to take the first value in the first list, loop through the second list and then repeat for all values in the first list? P.s. I haven't written this part of the script yet, the below is just a test I have started to write as a background script.

Script: 

 

var reqFor = '5627c2ce979151d4472bb5ffe153afd4';
var groups = 'itil test,pll box support';
var splitGroups = groups.split(',');
// gs.print(groups);
gs.print(splitGroups);

var myGroups = new GlideRecord('sys_user_grmember');
myGroups.addQuery('user', reqFor);
myGroups.query();

var list = [];

while (myGroups.next()) {
	list.push(myGroups.getDisplayValue('group'));
}
gs.print(list);

 

Output:

*** Script: itil test,pll box support
*** Script: PLL ITIL Role Group,CYT ITIL Role Group,CYT Admin Role Group,PLL Generic Account Approval,CYT ServiceNow Project Team,CYT Security Admin Role Group,Service Desk,CYT Problem Coordinator Role Group,PLL ACMS/FDMS Support,Windchill Container Approval UK,PLL Business Intelligence Support,PLL Automated Installs,CYT ServiceNow Approvers,PLL BI Operations,PLL CAD Support,PLL Security Group,CYT ServiceNow Admins,PLL Box Support
[0:00:00.039] Total Time

 Steven

5 REPLIES 5

Saurabh Gupta
Kilo Patron

Hi, 
The easiest way is to make two list as array and then use method intersect of ArrayUtil.


var tr="ITIL,ITil_adMin,rest_api,k,j";
var tr1="ITIL_admin,ITil,rest_api,j,k";

var trArr=tr.toLowerCase().split(",");
var tr1Arr=tr1.toLowerCase().split(",");

var au=new ArrayUtil();
if(au.diff(trArr,tr1Arr).length==0 && au.diff(tr1Arr,trArr).length==0)
gs.info('identical elements');
else
gs.info('not identical elements');




 


Thanks and Regards,

Saurabh Gupta