The CreatorCon Call for Content is officially open! Get started here.

Quick help with adding users from an array to a group

Brendan Hallida
Kilo Guru

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

find_real_file.png

find_real_file.png

 

anyone know what I'm missing?

Thanks, Brendan

1 ACCEPTED SOLUTION

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]);
		}
	}
}

View solution in original post

26 REPLIES 26

Just change the field you're dot-walking to in the encoded query:

gr.addEncodedQuery('user.user_name=' + admins[i] + '^group.name=Service Desk');

That did it!  I swear I tested that and it didn't work haha.

 

Thanks a lot everyone for your help 🙂

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. 🙂

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.

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());
  }
}