- 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-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:01 AM
Hi Ankur,
Not sure what is wrong but it is not deleting records from sys_user_has_role table 😞
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-18-2020 06:21 AM
Hi Mason,
Are you able to delete them manually?
Can you try deleting only 1 record from script?
what error it gives?
is there any before DELETE BR on sys_user_has_role table which is stopping it?
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 09:45 AM
Hello Ankur,
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-18-2020 10:07 AM
HI Mason,
For removing roles inherited from Groups, you need to remove users from the group and that will automatically remove the roles being inherited from group and associated to the user.
(function(){
var gr = new GlideRecord('sys_user_grmember'); // Group memeber Table
gr.addQuery('user.locked_out', 'true'); // user should be locked out
gr.addQuery('user.active', false); // user should be inactive.
gr.query();
gr.deleteMultiple(); // Delete all the records match above criteria.
})();
Please mark this accepted & helpful if it answered your question.
Thanks & Regards,
Sharjeel
Muhammad