- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-04-2022 11:49 AM
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
Please help me on this issue .
Solved! Go to Solution.
- Labels:
-
Service Catalog
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-04-2022 11:26 PM
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

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-05-2022 03:20 PM - edited 11-05-2022 03:21 PM
So the log only shows up once? Before the for loop can you do a gs.log("array1: " + array1 + " length: " + array1.length);

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-05-2022 09:33 PM
Also just try deleting the variable and recreate it. I have seen in the past were this fixes the issue.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-04-2022 12:38 PM - edited 11-04-2022 12:39 PM
Untested, but i modified your code. Try this
var array1 = current.variables.u_users.toString();
var array2 = array1.split(',');
var length = array2.length;
for (var i = 0; i < length; i++) {
var grCheck = new GlideRecord('sys_user_grmember');
grCheck.addQuery('group', current.variables.u_group1);
grCheck.addQuery('user', array2[i]);
grCheck.query();
if (!grCheck.next()) {
var groupAdd = new GlideRecord('sys_user_grmember');
groupAdd.initialize();
groupAdd.user = current.variables.u_group1;
groupAdd.group = array2[i];
groupAdd.insert();
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-04-2022 10:56 PM
Hi Mike,
Thank you for your response, but after adding your code to the Run Script, users were not added to the selected group.
Please assist me with this.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-04-2022 11:26 PM
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