Automatic removal of non active users from groups/roles
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-08-2022 12:36 AM
Hi All,
I need to implement a business rule on insert and update of users, which will check if the Active checkbox is true or false.
If it’s false, it means that the user is inactive and therefore he should be removed from all the groups and all the roles.
Also, I need to create a warning popup which will show when we try to Save a user profile, when we have made it inactive. This will prevent us from accidentally inactivating a user.
The text should be: Are you sure you want to deactivate this user? Click OK to proceed of Cancel to go back
And then we should have OK and Cancel buttons.
Could any one can please let me know and do the needful.
Thanks & Regards,
Sireesha
- Labels:
-
Script Debugger
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-08-2022 12:44 AM
Hi,
Points
1) I need to create a warning popup which will show when we try to Save a user profile, when we have made it inactive. This will prevent us from accidentally inactivating a user.
- Create onSubmit client script on sys_user and show confirm() box with message
- Based on OK or Cancel allow form submission
2) I need to implement a business rule on insert and update of users, which will check if the Active checkbox is true or false.
- Create after update BR and remove user from groups and roles
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-08-2022 12:49 AM
Hi,
onSubmit
function onSubmit(){
var confirm = confirm('Are you sure you want to deactivate this user? Click OK to proceed of Cancel to go back');
if(confirm() == true){
return true;
}
return false;
}
After Update BR:
Active [Changes to] False
Script:
(function executeRule(current, previous /*null when async*/) {
// Add your code here
var gr = new GlideRecord("sys_user_grmember");
gr.addQuery("user", current.getUniqueValue());
gr.deleteMultiple();
var gr1 = new GlideRecord("sys_user_has_role");
gr1.addQuery("user", current.getUniqueValue());
gr1.deleteMultiple();
})(current, previous);
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-08-2022 12:32 PM
Hi Ankur
I am using the script above also to remove inactive users from the groups.
My question, should I schedule a job to run the BR?
thank you

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-05-2022 09:08 AM
Thank you, Ankur. I tried using several suggestions from other articles and nothing has worked. I just came across what you suggested above for the Update BR above (for automatic removal of non-active users from groups) and it worked perfectly. Thank you very much - the way you explain how to solve for things in your article responses throughout the community are so helpful to someone like me who is trying to learn.