- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-01-2023 03:42 AM
Hi ,
i want to remove users from groups containing 'X' role when the user is inactivated
Note : nearly 90 groups contains this role
Thanks in advance
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-02-2023 04:00 AM
You can do this with an after Update Business Rule on the sys_user table with the Filter Conditions Active changes to false. One version of the script would look like this:
(function executeRule(current, previous /*null when async*/) {
var grpArr = [];
//build an array of every group with the role
var roles = new GlideRecord('sys_group_has_role');
roles.addQuery('role', '1hjfhsdf89343hgjsdhj34hj5dss344sasda'); //sys_id of role
roles.query();
while (roles.next()) {
grpArr.push(roles.group.toString());
}
//remove inactivated user from groups in array that they are a member of
var mbr = new GlideRecord('sys_user_grmember');
mbr.addQuery('user', current.sys_id);
mbr.addQuery('group', 'IN', grpArr.join(','));
mbr.query();
while (mbr.next()) {
mbr.deleteRecord();
}
})(current, previous);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-02-2023 04:00 AM
You can do this with an after Update Business Rule on the sys_user table with the Filter Conditions Active changes to false. One version of the script would look like this:
(function executeRule(current, previous /*null when async*/) {
var grpArr = [];
//build an array of every group with the role
var roles = new GlideRecord('sys_group_has_role');
roles.addQuery('role', '1hjfhsdf89343hgjsdhj34hj5dss344sasda'); //sys_id of role
roles.query();
while (roles.next()) {
grpArr.push(roles.group.toString());
}
//remove inactivated user from groups in array that they are a member of
var mbr = new GlideRecord('sys_user_grmember');
mbr.addQuery('user', current.sys_id);
mbr.addQuery('group', 'IN', grpArr.join(','));
mbr.query();
while (mbr.next()) {
mbr.deleteRecord();
}
})(current, previous);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-12-2023 12:33 AM
Hi @Brad Bowman ,
This business rule is working fine removing the user once changes active to false
and how it works for existing inactive users
Thanks in advance
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-02-2023 07:48 AM
Hi @Purushotham9963 ,
1. If you want whenever the user get deactivated, remove them from group that has "X" role. Please follow what Brad shows for using BR.
2. If you also want to remove all existing inactive members in group that has "X" role, then you can try following code in Scripts-Background or Fix script:
// Find all Groups that have "X" role
var groupRole = new GlideRecord('sys_group_has_role');
groupRole.addQuery('role', '317b0c7693323110a61071218bba10fa'); // SYS_ID of the role
groupRole.query();
while (groupRole.next()) {
// Find all inactive Members with above groups
var member = new GlideRecord('sys_user_grmember');
member.addEncodedQuery('user.active=false^group=' + groupRole.getValue('group'));
member.query();
while (member.next()) {
member.deleteRecord() // Delete member
}
}
Note: this will also delete role "X" from those inactive members (table sys_user_has_role)