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

Abhijit4
Mega Sage

You can use below 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 = [];
var count=0;
while (myGroups.next()) {
for(var i=0;i<splitGroups.length;i++){
	if(myGroups.getDisplayValue('group')==splitGroups[i]){
count++; // increment count whenver any of the group matches
}
}
}
if(count==splitGroups.length) //if matched group count is equels to all group count
gs.print("All groups matching");
else
gs.print("All groups are not matching");

 

 

 

Mark answer as Correct or Helpful based on impact.

By marking my response as correct or helpful, you contribute to helping future readers with similar issues.
Regards,
Abhijit
ServiceNow MVP

Mohith Devatte
Tera Sage
Tera Sage

Hello @Steven Watts2 ,

So you are pushing your group names into the array right ?

then you have some static values where you used split which is also converted into an array then you might have to loop it this way 

if its not -1 then its found in list 

 

for(var i=0; i< splitGroups.length; i++)
{
if(list.indexOf(splitGroups[i])!=-1)
{
//write your logic
}
}

Hope this helps 

Mark my answer correct if this helps you 

Thanks

 

GTSPerformance
Tera Guru

Check if their lengths are the same first. If not, then you can stop processing.

Next sort the arrays using Array.sort() - unless we can assume the array's elements are in the same order.

Finally, convert the Arrays to Strings and compare them.

NOTE: This will treat certain empty values as equivalent. '',  undefined, and null would all appear as empty strings when the Arrays are converted to a String (Array.toString())

 

Best regards,

Your ServiceNow Technical Support Performance team

(Check out our articles. Thanks!)

Steven Watts2
Tera Guru

Thanks all for the responses. I'll try these updates shortly and provide feedback :).

 

Steven