Automatically remove user from group
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-21-2013 08:31 AM
Hello,
I am trying to build an automated user remove catalog request in ServiceNow and am having trouble with the script part.
A user will fill out a form with:
User's name - variable = group_members
Group to be removed from - variable = workgroup
I want to user a GlideRecord to search for that user and remove him/her from the group.
I have been using the below and cannot get it to work. Any ideas?:
var rec = new GlideRecord('sys_user');
rec.addQuery('sys_id', current.variables.group_members);
rec.query();
while (rec.next())
{
var rec1 = new GlideRecord('sys_user_grmember');
rec1.addQuery('sys_id', current.variables.workgroup);
rec1.query();
while (rec1.next())
{
rec1.deleteRecord();
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-21-2013 09:02 AM
Assuming the Group Members field only contains ONE user, Group field (reference to sys_user_group) & Group Members (reference to sys_user) the following should work: If the Group Members is a list of users you will need to modify the script: rec1.addQuery('user', 'IN', current.variables.group_members);
var rec1 = new GlideRecord('sys_user_grmember');
rec1.addQuery('group', current.variables.workgroup);
rec1.addQuery('user', current.variables.group_members);
if (rec1.query()){
rec1.deleteRecord();
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-21-2013 09:39 AM
Okay, well unfortunately this script is not working and the user is not being removed. I will try working with it some.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-21-2013 09:51 AM
This should work a bit better, Forgot the rec1.next():
var rec1 = new GlideRecord('sys_user_grmember');
rec1.addQuery('group', current.variables.workgroup);
rec1.addQuery('user', 'IN',current.variables.group_members);
rec1.query();
if (rec1.next()){
rec1.deleteMultiple();
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-21-2013 01:49 PM
See if this works for you. I have this type of script running in our instance for a disable group. I have modified it some to your variables. It removes users automatically.function removeMemberships() {
var grpName = current.variables.workgroup;
var grpUser = current.variables.group_members;
var rm = new GlideRecord('sys_user_grmember');
rm.addQuery('group',grpName);
rm.addQuery('user',grpUser);
rm.query();
rm.deleteMultiple();
}