- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-20-2018 12:24 AM
Hi all,
I am pretty sure that I am close, however, the script is not running for all users in the array.
This is what I have so far:
var admins = ["user1","user2"]; //array
for(var i = 0; i < admins.length; i++) //for loop
{
var gr = new GlideRecord('sys_user_grmember');
gr.initialize();
gr.user = admins[i];
gr.group.setDisplayValue('Admin')
gr.setWorkflow(false);
gs.print(gr.insert());
}
So the script adds user1 to the group, and then also adds a blank entry in the user list of the group
anyone know what I'm missing?
Thanks, Brendan
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-24-2018 12:52 AM
Morning Brendan,
Try out the code below, i've just given it a little test and it seems to work as required. Obviously you'll need to change the user names and the group to whatever is on your instance.
var admins = ["abel.tuter","abraham.lincoln"];
for(var i = 0; i < admins.length; i++) {
//check user is not already in group
var gr = new GlideRecord('sys_user_grmember');
gr.addEncodedQuery('user.user_name=' + admins[i] + '^group.name=Service Desk');
gr.query();
gs.debug(admins[i] + ": " + gr.hasNext());
if (!gr.hasNext()){
//use user_name to get the user record
var usrGr = new GlideRecord('sys_user');
if(usrGr.get('user_name', admins[i])) {
//add the new record to the group
gr.initialize();
gr.user = usrGr.getValue('sys_id');
gr.group.setDisplayValue('Service Desk');
gr.setWorkflow(false);
gs.debug(gr.insert());
}
else{
// User not found by user_name
gs.error('Cannot find user: ' + admins[i]);
}
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-22-2018 05:12 AM
Just change the field you're dot-walking to in the encoded query:
gr.addEncodedQuery('user.user_name=' + admins[i] + '^group.name=Service Desk');
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-22-2018 04:52 PM
That did it! I swear I tested that and it didn't work haha.
Thanks a lot everyone for your help 🙂

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-23-2018 05:00 AM
I should note that while this solution will FIND (or not find) records based on the user ID field, it won't provide you with the sys_id (or display value) to create the value in the user field for sys_user_grmember.user. You'll still need to do a GlideRecord query, based on the userID.
Check out my comment with the full script from yesterday for the complete solution. 🙂
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-23-2018 06:18 AM
Yeah good point Chuck. setDisplayValue() won't work if you're giving it the user_name. You'll need to either use Chucks script above or populate your array with the display value (name) or the sys_id and amend the script accordingly.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-23-2018 10:52 PM
Cheers again guys.
I have tried Chuck's code above and no entries end up in the groups.
The if (!gr.hasNext()){} debug log returns false, that is meant to right?
var admins = ["abel.tuter","abraham.lincoln"];
for(var i = 0; i < admins.length; i++) {
var gr = new GlideRecord('sys_user_grmember');
gr.addEncodedQuery('user.user_name=' + admins[i] + '^group.name=Service Desk');
gr.query();
gs.debug(admins[i] + ": " + gr.hasNext());
if (!gr.hasNext()){
// try to get the user record by user_name to track the sys_id
var usrGr = new GlideRecord('sys_user');
if (!usrGr.get('user_name', admins[i])) {
// User not found by user_name
gs.error('Cannot find user: ' + admins[i]);
continue;
}
// Now build the new record in sys_user_grmember
var memGr = new GlideRecord('sys_user_grmember');
gs.debug('adding users');
gs.print(usrGr.name);
//memGr.newRecord();
gs.print('users sys_id: ' + usrGr.getValue('sys_id'));
memGr.user = usrGr.getValue('sys_id');
memGr.group.setDisplayValue('Service Desk') ;
memGr.setWorkflow(false);
gs.debug(gr.insert());
}
}