- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-05-2023 04:20 AM
How to add multiple users to a group using run script in servicenow workflow.
Catalog Fields:
and in the run script , I have given the below script. Its not working, Can anyone please suggest whats wrong with my code?
RunScript Code:
var groups= current.variables.group_name;
var user=current.variables.user_list.split(',');
for(i=0;i<user.length;i++){
var gr = new GlideRecord('sys_user_grmember');
gr.initialize();
gr.group = groups;
gr.user = user[i];
gr.insert();
}
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-05-2023 04:30 AM - edited 05-05-2023 04:31 AM
Hi @Vasanthi Unnam,
Try this updated scripts in workflow and make sure to add 2 seconds timer activity.
var groups = current.variables.group_name.toString();
var user = current.variables.user_list.toString().split(',');
for (i = 0; i < user.length; i++) {
var newUser = new GlideRecord('sys_user_grmember');
newUser.initialize();
newUser.group = groups;
newUser.user = user[i];
newUser.insert();
}
Thanks,
Sagar Pagar
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-05-2023 04:30 AM - edited 05-05-2023 04:31 AM
Hi @Vasanthi Unnam,
Try this updated scripts in workflow and make sure to add 2 seconds timer activity.
var groups = current.variables.group_name.toString();
var user = current.variables.user_list.toString().split(',');
for (i = 0; i < user.length; i++) {
var newUser = new GlideRecord('sys_user_grmember');
newUser.initialize();
newUser.group = groups;
newUser.user = user[i];
newUser.insert();
}
Thanks,
Sagar Pagar
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-05-2023 05:45 AM
Thank you Sagar. Its working. Is it like the "toString()" method gives the reference value as string field, as the sys_user_grmember table user and group fields are string fields?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-05-2023 10:22 AM
Hi @Vasanthi Unnam,
Yes. toString() will convert reference sys_ids into string type. Also, make sure before splitting it should be converted to string.
Thanks,
Sagar Pagar
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-09-2023 05:54 AM
Thanks for the clarification Sagar.