- 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-20-2018 07:07 PM
Cheers Chuck, that worked.
Omkar did mention that in the first post, however, I did not fully get it until I saw it in action.
is there a way to now query if the user exists in the group, and ignore if it does?

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-20-2018 11:17 PM
Hi
gr.addQuery('user', gs.getUserID());//this will get all the users present in the group
add it in your code only.
Thanks.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-21-2018 12:40 AM
Hi Brendan,
Your script won't add a user to the group if that user already exists in there. However, if you want to exclude them in the script anyway you could do something like this:
var admins = ["user1","user2"];
for(var i = 0; i < admins.length; i++) {
var gr = new GlideRecord('sys_user_grmember');
gr.addEncodedQuery('user.name=' + admins[i]' + '^group.name=Admin');
gr.query();
if(!gr.hasNext()){
gr.initialize();
gr.user.setDisplayValue(admins[i]);
gr.group.setDisplayValue('Admin')
gr.setWorkflow(false);
gs.print(gr.insert());
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-21-2018 05:41 PM
Hi David,
I have tested, and the script does in fact keep adding users.
I have tested yours, however, I keep getting an error with the encoded query
Javascript compiler exception: missing ) after argument list (null.null.script; line 4) in:
I have played around with the query, but cannot seem to get the syntax right.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-21-2018 11:37 PM
Hi
Try this
var admins = ["user1","user2"];
for(var i = 0; i < admins.length; i++) {
var gr = new GlideRecord('sys_user_grmember');
gr.addEncodedQuery('user.name=' + admins[i] + '^group.name=Admin');
gr.query();
if(!gr.hasNext()){
gr.initialize();
gr.user.setDisplayValue(admins[i]);
gr.group.setDisplayValue('Admin') ;
gr.setWorkflow(false);
gs.print(gr.insert());
}
}
Mark correct if it helps.
Regards,
Omkar Mone
www.dxsherpa.com