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

Yeah that means the script is only getting one user and so it is inserting only one user.

 

What type of field is u_user is it a list field? Are the variable values correctly showing in RITM which you select.

 

Can you paste as screenshot or RITM with the variable having multiple values stored.

Sravani47_0-1667649551231.png

u_users - field type is List Collector

u_group1 - field type is reference 

 

Is u_users really a list collector? From the image it looks like a multi line text field. 

Hi @Sravani47,

 

I tried the same case on my PDI, and it worked as expected in workflow and back-ground, adding users to selected groups when they weren't previously there.

 

Back-ground scripts-

var array1 = [];
array1 = "user sys_id's"; //current.variables.u_users.toString();
var group_sys_id = "group_sys_id"; //current.variables.u_group1.toString();

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

for (var i = 0; i < array2.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();
	} else {
		gs.info("present:" + grMember.user.name + " - " + grMember.group.name);
	}
}

 

 

Thanks,

Sagar Pagar

The world works with ServiceNow