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

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?

Hi 

gr.addQuery('user',   gs.getUserID());//this will get all the users present in the group

 

add it in your code only.

 

Thanks.

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

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.

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