- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-17-2020 04:12 AM
Hi All,
I am trying to remove all roles of users(from sys_user_has_role) who are inactive and locked out in user table but it does not allow to remove records from sys_user_has_role table, does not delete with Background script as well.
How can we achieve this, any idea?
Thanks in Advance!
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-17-2020 05:03 AM
Hi Mason,
ensure you wrap code inside function
updateRecord();
function updateRecord(){
try{
var gr = new GlideRecord('sys_user_has_role');
gr.addEncodedQuery('user.locked_out=true^user.active=false');
gs.info('Row Count is: ' + gr.getRowCount());
gr.query();
while(gr.next())
{
gr.deleteRecord();
}
}
catch(ex){
gs.info('Exception is: ' + ex);
}
}
Mark ✅ Correct if this solves your issue and also mark 👍 Helpful if you find my response worthy based on the impact.
Thanks
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
06-18-2020 11:23 PM
Hi Mason,
As suggested by Muhammad you need to remove those users from the groups and automatically roles would get deleted
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
06-18-2020 11:35 PM
So did you had a look at what I mentioned 2 days ago? Removing the groups relationship?
Code = tested. Obviously you could/should test yourself with a setLimit first.
(function() {
var grUser = new GlideRecord('sys_user_grmember');
grUser.addEncodedQuery('user.active=false');
grUser.addEncodedQuery('user.locked_out=true');
grUser.setLimit(10);
grUser.deleteMultiple();
})();
If my answer helped you in any way, please then mark it as helpful.
Kind regards,
Mark
2020 ServiceNow Community MVP
2020 ServiceNow Developer MVP
---
LinkedIn
Community article list
Kind regards,
Mark Roethof
Independent ServiceNow Consultant
10x ServiceNow MVP
---
~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-22-2020 10:48 PM
Hi there,
Did this solve your question? Or do we need to follow-up on this?
Please mark this answer as correct if it solves your question. This will help others who are looking for a similar solution. Also marking this answer as correct takes the post of the unsolved list.
Thanks.
Kind regards,
Mark
2020 ServiceNow Community MVP
2020 ServiceNow Developer MVP
---
LinkedIn
Community article list
Kind regards,
Mark Roethof
Independent ServiceNow Consultant
10x ServiceNow MVP
---
~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-28-2020 11:41 PM
Hi there,
Did this solve your question? Or do we need to follow-up on this?
Please mark this answer as correct if it solves your question. This will help others who are looking for a similar solution. Also marking this answer as correct takes the post of the unsolved list.
Thanks.
Kind regards,
Mark
2020 ServiceNow Community MVP
2020 ServiceNow Developer MVP
---
LinkedIn
Community article list
Kind regards,
Mark Roethof
Independent ServiceNow Consultant
10x ServiceNow MVP
---
~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-17-2020 04:40 AM
For example, just tested below code, works fine with admin role. Just update the encodedQuery. Do note what I mentioned, you might also want to look at the group relation.
(function() {
var grUser = new GlideRecord('sys_user_has_role');
grUser.addEncodedQuery('user.active=false');
grUser.deleteMultiple();
})();
If performing this in a background script or fix script, do wrap the code inside a function. This is best practice.
If my answer helped you in any way, please then mark it as helpful.
Kind regards,
Mark
2020 ServiceNow Community MVP
2020 ServiceNow Developer MVP
---
LinkedIn
Community article list
Kind regards,
Mark Roethof
Independent ServiceNow Consultant
10x ServiceNow MVP
---
~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field