ArrayUtil intersect help

traviswarren
Kilo Expert

Hello all, I am having a little issue working with the ArrayUtil Intersect.   My requirement is for the logged in user to have the ability to change the approval person if the user is in their group.   I created a script below that will provide me an array of the logged in User's groups and the approval user's group.   My intent was to use the ArrayUtil to compare the two and find if there was a matching group.   Below is a background script I used.

//initialize arrays

var iLookupArray = new Array();

var iLookupArray2 = new Array();

var iLookup = new GlideRecord('sys_user_grmember');

iLookup.addQuery('user', '15390c296f969100b9c5247cbb3ee4e7'); //a user sys_ID

iLookup.query();

while(iLookup.next())

{

iLookupArray.push(iLookup.group.name); //push group names to array

}

gs.print("assigned to " + iLookupArray);   //print array

//var loggedInUser = gs.getUser().getMyGroups();

var loggedInUser = gs.getUserID();

var gr = new GlideRecord('sys_user_grmember');

gr.addQuery('user', loggedInUser);

gr.query();

while(gr.next())

{

iLookupArray2.push(gr.group.name); //push group names to array

}

gs.print("Logged in user " + iLookupArray2);   //print array

//ArrayUtil

var a1 = new Array(iLookupArray2);

var b1 = new Array(iLookupArray);

var arrayUtil = new ArrayUtil()

gs.print(arrayUtil.intersect(a1,b1))     // should show the matching groups from a1 to b1

arrayutil.JPG

Thanks in advance!

1 ACCEPTED SOLUTION

Brad Tilton
ServiceNow Employee
ServiceNow Employee

Hi Travis,



I took this, changed the sys_id and rewrote it a bit and tested and it worked for me. I think the main thing was pushing the names to a string in the while loops:



//initialize arrays  


var iLookupArray = new Array();


var iLookupArray2 = new Array();



var iLookup = new GlideRecord('sys_user_grmember');


iLookup.addQuery('user', '15390c296f969100b9c5247cbb3ee4e7'); //a user sys_ID  


iLookup.query();


while (iLookup.next()) {


    iLookupArray.push(iLookup.group.name + ''); //push group names to array  


}



gs.print("assigned to " + iLookupArray); //print array  



//var loggedInUser = gs.getUser().getMyGroups();  


var loggedInUser = gs.getUserID();


var iLookup2 = new GlideRecord('sys_user_grmember');


iLookup2.addQuery('user', loggedInUser);


iLookup2.query();


while (iLookup2.next()) {


    iLookupArray2.push(iLookup2.group.name + ''); //push group names to array  


}



gs.print("Logged in user " + iLookupArray2 + ''); //print array  



//ArrayUtil  


var arrayUtil = new ArrayUtil()


gs.print(arrayUtil.intersect(iLookupArray, iLookupArray2));


View solution in original post

2 REPLIES 2

Brad Tilton
ServiceNow Employee
ServiceNow Employee

Hi Travis,



I took this, changed the sys_id and rewrote it a bit and tested and it worked for me. I think the main thing was pushing the names to a string in the while loops:



//initialize arrays  


var iLookupArray = new Array();


var iLookupArray2 = new Array();



var iLookup = new GlideRecord('sys_user_grmember');


iLookup.addQuery('user', '15390c296f969100b9c5247cbb3ee4e7'); //a user sys_ID  


iLookup.query();


while (iLookup.next()) {


    iLookupArray.push(iLookup.group.name + ''); //push group names to array  


}



gs.print("assigned to " + iLookupArray); //print array  



//var loggedInUser = gs.getUser().getMyGroups();  


var loggedInUser = gs.getUserID();


var iLookup2 = new GlideRecord('sys_user_grmember');


iLookup2.addQuery('user', loggedInUser);


iLookup2.query();


while (iLookup2.next()) {


    iLookupArray2.push(iLookup2.group.name + ''); //push group names to array  


}



gs.print("Logged in user " + iLookupArray2 + ''); //print array  



//ArrayUtil  


var arrayUtil = new ArrayUtil()


gs.print(arrayUtil.intersect(iLookupArray, iLookupArray2));


Works like a charm!