- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-26-2022 03:03 AM
Requirement is manager of a particular group should be able to add multiple users to the selected group or remove multiple users from the selected group using catalog item.
when manager submits the request, users in "Add members to group" should be added and
users in "Remove members from group" should be removed from the group.
what should be the script in workflow("run Script").
Solved! Go to Solution.
- Labels:
-
Service Catalog
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-26-2022 05:39 AM
Hi,
update as this and test
var usersToAdd = current.variables.add_users; // give correct variable name here
var usersToRemove = current.variables.remove_users; // give correct variable name here
var group = current.variables.group; // give correct variable name here
var rec = new GlideRecord('sys_user_grmember');
rec.addQuery("group", group);
rec.addQuery("user", "IN", usersToRemove.toString());
rec.query();
rec.deleteMultiple();
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();
}
}
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-09-2024 12:26 PM
@Ankur Bawiskar if I'm trying to add an existing user. is there any script to restrict before adding user into group. Please let me know how can we over come the same.
Thanks & Regards,
Prasanna Kumar
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-26-2022 03:13 AM
Hello,
Follow this script to add the members
var usersToBeAdded = current.variables.your_add_members_to_group_variable_name.split(',');
for(var i=0; i<usersToBeAdded.length;i++)
{
var grm = new GlideRecord('sys_user_grmember');
grm.initialize();
grm.group=current.variables.your_group_variable_name;
grm.user=usersToBeAdded[i];
grm.insert();
}
Follow this script to delete members
var usersToBeRemoved = current.variables.your_remove_members_from_group_variable_name.split(',');
for(var j=0; j<usersToBeRemoved.length;j++)
{
var grmd = new GlideRecord('sys_user_grmember');
grmd.addQuery('group',currentvariables.your_group_variable_name);
grmd.addQuery('user',usersToBeRemoved[j]);
grmd.query();
if(grmd.next())
{
grmd.deleteRecord();
}
}
Please let me know if you face any issues
if it works mark my answer correct
Thanks

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-09-2024 12:25 PM
@Mohith Devatte if I'm trying to add an existing user. is there any script to restrict before adding user into group. Please let me know how can we over come the same.
Thanks & Regards,
Prasanna Kumar
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-26-2022 03:50 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-26-2022 03:51 AM
Hello lakshmi ,
can you please share your script for that ?