- 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-04-2022 12:36 PM - edited 11-04-2022 12:40 PM
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();
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-04-2022 10:50 PM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-04-2022 11:37 PM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-05-2022 01:01 AM
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