- 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 12:11 AM
there's a rogue apostrophe after the admins[i]. Change to this:
gr.addEncodedQuery('user.name=' + admins[i] + '^group.name=Admin');
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-22-2018 12:42 AM
Cheers, the query is fixed, however, it is still adding the users.
Interestingly, when running from background scripts - the message after running it is different when running it when the users are already in the group.
when they are not in the group:
Adding Role admin to user1
Background message, type:info, message: Adding Role admin to user1
*** Script: 7e2de14adb0ce700ce49aaf6059619e2
Adding Role admin to user2
Background message, type:info, message: Adding Role admin to user2
*** Script: b62da54adb0ce700ce49aaf60596192d
when they are in the group:
*** Script: 457de546db0ce700ce49aaf6059619ec
*** Script: 857de546db0ce700ce49aaf6059619ee
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-22-2018 01:11 AM
just tested on my PDI as below. The users were added first time around and ignored thereafter. Add a couple of debug lines as below to confirm what the hasNext is returning and if the condition is passing.
For the record, i tested without lines 4-8 and users still weren't added to the group as they already existed there. Do the users in your admins array already exist in the user table?
var admins = ["Aileen Mottern","Alejandro Mascall"];
for(var i = 0; i < admins.length; i++) {
var gr = new GlideRecord('sys_user_grmember');
gr.addEncodedQuery('user.name=' + admins[i] + '^group.name=Service Desk');
gr.query();
gs.print(gr.hasNext());
if(!gr.hasNext()){
gs.print('adding users');
gr.initialize();
gr.user.setDisplayValue(admins[i]);
gr.group.setDisplayValue('Service Desk') ;
gr.setWorkflow(false);
gs.print(gr.insert());
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-22-2018 04:51 AM
ok i think I know what the issue is. I was using username in the array, not name.
how can we use username? its not the end of the world if we cant, but would be nice.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-22-2018 05:04 AM
If you want to use the "User ID" (user_name) field, then it would look like this: (note, this code has not been tested.)
var admins = ["aileen.mottern","alejandro.mascall"];
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(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');
memGr.newRecord();
memGr.user = usrGr.getValue('sys_id');
memGr.group.setDisplayValue('Service Desk') ;
memGr.setWorkflow(false);
gs.debug(gr.insert());
}
}