- 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 06:05 AM
Hi Mark,
Tried but did not work unfortunately. Just executed code in backgrounds script. Tried deleteRecord() deleteMultiple()
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-18-2020 09:46 AM
Hello Mike,
Actually just noticed, it is not allowing to delete those roles which are inherited from Groups, tried to create a user and separately assigned roles to that user and I could delete those roles.
But my requirement is to delete those roles which are inherited from Group, can we do anything with this, please?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-17-2020 04:30 AM
Hi Mason,
please share background script you tried
I tried this and it worked
var gr = new GlideRecord('sys_user_has_role');
gr.addQuery('sys_id','5152bd22db7433009f7890b6db96199b'); // sys_id of the record
gr.query();
if(gr.next()){
gr.deleteRecord();
}
If my answer solved your issue, please mark my answer as ✅ Correct & 👍Helpful based on the Impact.
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-17-2020 04:46 AM
Hi Ankur,
Below is the script I tried,
var gr = new GlideRecord('sys_user_has_role');
gr.addEncodedQuery('user.locked_out=true^user.active=false');
gr.query();
while(gr.next())
{
gr.deleteRecord();
// gr.deleteMultiple(); //Tried this as well
}

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-17-2020 04:54 AM
If going for code like this, don't go for a while + gr.deleteRecord. This performs awefull. Have a look at the example I posted earlier.
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