- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-09-2015 01:58 PM
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
Thanks in advance!
Solved! Go to Solution.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-09-2015 02:13 PM
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));

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-09-2015 02:13 PM
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));
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-09-2015 03:16 PM
Works like a charm!