- 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 12:35 AM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-05-2022 01:32 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-05-2022 02:06 AM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-05-2022 02:17 AM
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();
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-05-2022 03:25 AM
Hi Saurav,
Only first user Sys_id only displayed in logs