- 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 12:29 AM
Hi
Please let me know what length is it returning ?
And in this case user is a reference field in grmember so you will have to pass sys_id's of the users or you can even do setDisplayValue as you have done for the groups.
Mark correct if it helps.
Regards,
Omkar Mone
www.dxsherpa.com
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-20-2018 12:42 AM

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-20-2018 12:49 AM
Hi
try doing length-1,because it points to next address in case of array.
ex.
for(var i=0;i<admins.length;i++)
{
gr.user.setDisplayValue(admins[i].toString())
}
try this too.
Thanks.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-20-2018 12:47 AM
Hi Brendan,
Can you try making the admins[i] as a string by adding an empty string '' or using admins[i].toString()
If not lets print admins[i] in each iteration and see whats going on.
var admins = ["user1","user2"]; //NOTE1: make sure user1, user2,.. are sys_id's
gs.print(admins[i]);
for(var i = 0; i < admins.length; i++) //for loop
{
gs.print(admins[i]);//add a print here to see whats inside admins[i]
var gr = new GlideRecord('sys_user_grmember');
gr.initialize();
gr.user = admins[i] + '';//NOTE2: make the array element a string by adding empty string or using .toString method
gr.group.setDisplayValue('Admin')
gr.setWorkflow(false);
gs.print(gr.insert());
}v