Remove users from groups containing 'X' role when the user is inactivated

Purushotham9963
Tera Contributor

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 

1 ACCEPTED SOLUTION

Brad Bowman
Kilo Patron
Kilo Patron

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);

View solution in original post

3 REPLIES 3

Brad Bowman
Kilo Patron
Kilo Patron

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);

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 

 

Community Alums
Not applicable

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)