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

Brian Lancaster
Tera Sage

Try this instead. you only need 1 array.

 

 

 

 

var array1 = [];

array1 = current.variables.u_users.toString().split(",");

for (var i = 0; i < array1.length; i++) {
    var gr = new GlideRecord('sys_user_grmember');
    gr.addQuery('group', current.variables.u_group1);
    gr.addQuery('user', array1[i]);
    gr.query();
    if (!gr.next()) { //if user is not a member of the group add them
        gr.group = current.variables.u_group1;
        gr.user = array1[i];
        gr.insert();
    }
}

 

 

 

 

Hi Brian,

I appreciate the response. 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.

 

Can you add a log statement in the for loop.

gs.log ("array1 index: " + array1[1]);

You can add this right before the add query. Let's see if you are getting different sys_id for each pass. You can also put a one in the if statement. Something like gs.log("in if"). This will help us know if the query is failing for some reason.

Hi Brian,

 

added gs.log ("array1 index: " + array1[1]); this log message before add querys

added gs.log("in if") this log also in if statement

 

log OUTPUT :

array1 index: <<Sys_ID of last user>>

"in if" 

 

added only first user into group, but in log message showing last user sys_id