Help me with the scheduled job script on how to remove user roles.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-22-2021 01:59 AM
Hi everyone,
We have a requirement to remove user roles and user groups.
This should happen when the user record is set to Active "False".
After 7 days we need to remove user roles and need to remove the user record from the user groups as well.
Please help me with the best way to achieve this.
Thank you,
Balaram.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-22-2021 02:39 AM
Hi,
You can use flow designer approach with no script
1) Flow trigger is Active changes to False
2) Then wait for duration logic - 7 days means 7*24 hours
3) then use Look up records on sys_user_has_role for this user
4) then use Record Delete action
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
01-22-2021 04:33 AM
var usr = new GlideRecord('sys_user');
var rlo = new GlideRecord('sys_user_grmember');
usr.addQuery('active', false);
usr.query();
while (usr.next()) {
rlo.addQuery('user', usr.sys_id);
rlo.query();
while (rlo.next()) {
usr.deleteMultiple();
}
}
You can use above script accordingly.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-27-2021 02:52 AM
Hi Thanks for your response, I tried this script, we need to keep the inactive user records, but inactive records should not be part of any groups.
using this script in scheduled jobs, is deleting user records.
Thank you,
Balaram.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-27-2021 09:28 PM
You should query sys_user table with inactive users
1) then visit the sys_user_grmember table and remove those users
var userRec = new GlideRecord('sys_user');
userRec.addQuery('active', false);
userRec.query();
while(userRec.next()){
// for this inactive user remove that user from all groups
var memberRec = new GlideRecord('sys_user_grmember');
memberRec.addQuery('user', userRec.sys_id);
memberRec.query();
memberRec.deleteMultiple();
}
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader