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

Omkar Mone
Mega Sage

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

Brendan Hallida
Kilo Guru
Thanks for getting back to me Omkar. What will be the best way to log the length value in this case?

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.

athm
Tera Guru

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