Auto remove inactive managers from active groups and inactive users from active groups

Sai Santhosh Ch
Tera Contributor

When a user goes inactive we need to check if that user was a manager of a group or if that user was a member of   a group. If the user is a member of a group we will remove them from that group. If they were a manager of that group we need to replace that user with their manager and notify the manager that they have been assigned the group manager.

1 ACCEPTED SOLUTION

Mike Allen
Mega Sage

A business rule on sys_user that says:



before Insert


condition current.active.changesTo('false')



var group = new GlideRecord('sys_user_group');


group.addQuery('manager', current.sys_id);


group.query();


while(group.next()){



group.manager = current.manager;


group.update();


gs.eventQueue('group.manager.changed', group, group.manager, current.name);



}



var grmember = new GlideRecord('sys_user_grmember');


grmember.addQuery('user', current.sys_id);


grmember.query();


while(grmember.next()){



grmember.deleteRecord();



}


View solution in original post

10 REPLIES 10

Mike Allen
Mega Sage

A business rule on sys_user that says:



before Insert


condition current.active.changesTo('false')



var group = new GlideRecord('sys_user_group');


group.addQuery('manager', current.sys_id);


group.query();


while(group.next()){



group.manager = current.manager;


group.update();


gs.eventQueue('group.manager.changed', group, group.manager, current.name);



}



var grmember = new GlideRecord('sys_user_grmember');


grmember.addQuery('user', current.sys_id);


grmember.query();


while(grmember.next()){



grmember.deleteRecord();



}


Then register the event 'group.manager.changed' in Event Registry.



Then create a notification that fires when this event fires.   Add the text to the notification that tells the manager what group was changed.


Thanks Mike Allen, that helped a lot.


I have another question which is similar to this type.



Q1) If user goes inactive we need to see if that user was assigned any active tasks, if so we will need to reassign those tasks to the users manager and notify the manager that they are being assigned those tasks because the user has become inactive.



Q2) If user goes inactive and has approvals assigned to them we will want to notify the requested for that the approver has become inactive and the request will auto cancel within 3 days.



Thanks in advance


Similar query:



var task = new GlideRecord('task');


task.addActiveQuery();


task.addQuery('assigned_to', current.sys_id);


task.query();


while(task.next()){



task.assigned_to = current.manager;


gs.eventQueue('task.reassigned', task, current.manager, current.sys_id);



}



var approval = new GlideRecord('sysapproval_approver');


approval.addQuery('approver', current.sys_id);


approval.addQuery('state', 'requested');


approval.query();


while(approval.next()){



gs.eventQueue('approval.cancelled', approval, current.sysapproval.requested_by, current.sys_id);



}