Add multiple users into the group from catalogue form.

Sravani47
Tera Contributor

We have a catalogue form with a field name "select the users" type of "list collector" and a reference to the sys_user table, as well as a field with a "select the group" type of "reference" and a reference to the sys_user_group table. When multiple users are selected in that "select users" field and the catalogue form is submitted, all of the selected users should be added to that group.

 

Issue : : Only the first user is added to the group when multiple users are selected in the "select the users" field and the catalogue form is submitted. We want to add all of the selected users to the group.

 

In Workflow - Run Scrip Code:

var array1 =[];

array1 = current.variables.u_users.toString();

var array2 = [];
array2 = array1.split(',');
var Length = array2.length;
for (var i =0; i< Length; i++){
gr.addQuery('sys_id',array2[i]);

var gr = new GlideRecord('sys_user_grmember');
gr.initialize();
gr.addQuery('group',current.variables.u_group1);
gr.addQuery('user',array2[i]);
gr.query();
if(gr.next()){

}
else
{
gr.group = current.variables.u_group1;
gr.user = array2[i];
gr.insert();
}
}

 

catalogue form screen

Sravani47_0-1667587515296.png

 

Please help me on this issue .

1 ACCEPTED SOLUTION

Sagar Pagar
Tera Patron

Hi,

 

Try this updated scritps -

 

var array1 = [];
array1 = current.variables.u_users.toString();
var group_sys_id = current.variables.u_group1.toString();

var array2 = [];
array2 = array1.split(',');
var Length = array2.length;

for (var i = 0; i < Length; i++) {

	var grMember = new GlideRecord('sys_user_grmember');
	grMember.addQuery('user', array2[i]);
	grMember.addQuery('group', group_sys_id);
	grMember.query();
	if (!grMember.next()) {

		var newRecord = new GlideRecord('sys_user_grmember');
		newRecord.initialize();
		newRecord.group = group_sys_id;
		newRecord.user = array2[i];
		newRecord.insert();
	}
}

 

Thanks,

Sagar Pagar

The world works with ServiceNow

View solution in original post

18 REPLIES 18

Hi Sagar,

Thank you for your reply

only the first user is added to the selected group after I added your code to the Run Script Activity.

Still, the problem exists.

 

Please help me on this issue.

Saurav11
Kilo Patron
Kilo Patron

Hello,,

 

Please try the below code  and let me know :-

 

var usersToAdd = current.variables.u_users; // give correct variable name here

var group = current.variables.u_group1;  // give correct variable name here

var arr = usersToAdd.toString().split(',');

for(var i=0;i<arr.length;i++){
	var addRec = new GlideRecord('sys_user_grmember');
	addRec.addQuery("user", arr[i]);
	addRec.addQuery("group", group);
	addRec.query();
	if(!addRec.hasNext()){
		addRec.initialize();
		addRec.user = arr[i];
		addRec.group = group;
		addRec.insert();
	}
}

 

Please mark my answer as correct based on Impact

Hi Saurav,

Thank you for your reply

only the first user is added to the selected group after I added your code to the Run Script Activity.

Still, the problem exists.

Hello,

 

Can you use the below script and check what is usersToAdd printing in logs? is it printing the sys_id of all the users. also please check how many time is time getting printed in logs.

 

Let me know then

var usersToAdd = current.variables.u_users; // give correct variable name here
gs.log('usersToAdd'+usersToAdd);
var group = current.variables.u_group1;  // give correct variable name here

var arr = usersToAdd.toString().split(',');

for(var i=0;i<arr.length;i++){
	var addRec = new GlideRecord('sys_user_grmember');
	addRec.addQuery("user", arr[i]);
	addRec.addQuery("group", group);
	addRec.query();
	if(!addRec.hasNext()){
          gs.log('test');
		addRec.initialize();
		addRec.user = arr[i];
		addRec.group = group;
		addRec.insert();
	}
}

 

Hi Saurav,

 

Only first user Sys_id only displayed in logs