- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-29-2017 11:24 AM
Hey all,
I am working on a script for a Run Workflow activity and am stuck. Here is an overview:
User submits a form on the Portal that creates a RITM. When they Select the option "Remove Access" another field pops up with a list of the current groups they are in. They can select which group or groups they want to have access removed from and then submit the form. The workflow is set up such as if they request removal from group(s) they are members of, there is no approval needed and the system can go ahead with the automation and remove them. I have all of that stuff figured out, its the script I am stuck on.
Here is what I have so far, admittedly I am not terribly experienced with scripting so I may be going in the totally wrong direction:
var grpRemove = new GlideRecord('sys_user_grmember');
grpRemove.addQuery('user', current.variables.requester_name.sys_id);
grpRemove.addquery('group.name', current.variables.var_groups_removal);
grpRemove.query();
if(grpRemove.next()) {
grpRemove.deleteRecord();
}
I am thinking I need a way for it to remove the users from more than one group if they select more than one, but I was just trying to get it to remove them from one first and then build on it from there. I have tried a few variations and changes on the above script but no luck so far. Any help would be appreciated!
Thanks!
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-29-2017 03:15 PM
OK,
Got it working after much trial and error and enlisting the help from a fellow developer on a Skype meeting 😃 Want to post here to complete the question and for anyone else who may run in to this in the future. What we ended up having to do with the script is as follows:
var grps = current.variables.var_groups_removal.toString().split(',');
var grpRemove = new GlideRecord('sys_user_grmember');
grpRemove.addQuery('user', current.variables.requester_name);
var groups = grpRemove.addQuery('group', grps[0]);
for (var i = 1; i< grps.length;i++)
groups.addOrCondition('group', grps[i]);
grpRemove.query();
while(grpRemove.next()) {
grpRemove.deleteRecord();
}
var removed = current.variables.var_groups_removal.getDisplayValue();
current.comments = "The user has been successfully removed from the following groups: " + removed;
Thanks all for the help and suggestions with this, definitely made it easier!

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-29-2017 12:58 PM
Hello Kris,
Here is the updated script. Please try this.
var grps = current.variables.var_groups_removal.getDisplayValue.split(',');
for (var i = 0; i< grps.length;i++)
{
var grpRemove = new GlideRecord('sys_user_grmember');
grpRemove.addQuery('user', current.variables.requester_name);
grpRemove.addquery('group.name', grps[i]);
grpRemove.query();
if(grpRemove.next()) {
grpRemove.deleteRecord();
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-29-2017 01:23 PM
Pradeep,
Tried the script as posted above and it doesn't remove any groups at all.
I also changed the 'group.name' to 'group' to see if it mattered and same results....no groups removed at all.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-29-2017 11:33 AM
Hello Kris,
Is the variable "var_groups_removal" a reference field on the form? If yes then change the line from grpRemove.addquery('group.name', current.variables.var_groups_removal); to grpRemove.addquery('group', current.variables.var_groups_removal);

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-13-2019 07:53 AM
Hi All,
I have implemented this script with great success. Thank you to all contributors. What I would like to have accomplish is a line of code that will exclude this script from taking action on the 'Admin' group or the 'admin role'. Please help.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-29-2017 11:38 AM
Hi Kris,
Try the below script, if you are removing from one group.
var grpRemove = new GlideRecord('sys_user_grmember');
grpRemove.addQuery('user', current.variables.requester_name);
grpRemove.addquery('group', current.variables.var_groups_removal);
grpRemove.query();
if(grpRemove.next()) {
grpRemove.deleteRecord();
}
If you are removing from multiple groups, You will need to split the groups
var grps = current.variables.var_groups_removal.split(',');
for (var i = 0; i< grps.length;i++)
{
var grpRemove = new GlideRecord('sys_user_grmember');
grpRemove.addQuery('user', current.variables.requester_name);
grpRemove.addquery('group', grps[i]);
grpRemove.query();
if(grpRemove.next()) {
grpRemove.deleteRecord();
}
}
Please mark this response as correct or helpful if it assisted you with your question.