- 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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-17-2020 04:19 AM
Hi there,
What do you mean with "it does not allow to remove records"? Can you share an example. Or might this be inherited roles perhaps (are the group relations removed also)?
Deleting records from this table should be possible.
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
---
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-17-2020 04:47 AM
Hi Mark,
There is not "Delete" option in list choice, most of them are inherited roles from groups but there are 100+ different groups.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-17-2020 04:53 AM
Do you have steps to reproduce on how you are trying to do this? Out-of-the-box I just have a Delete UI Action.
Did this got changed in your instance? ACL's? What role are you using? Etc..
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
---
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-17-2020 05:00 AM
So did you actually try to remove those users from those groups? With that, also the inherited roles will be removed.
Almost same query:
(function() {
var grUser = new GlideRecord('sys_user_grmember');
grUser.addEncodedQuery('user.active=false');
grUser.deleteMultiple();
})();
(do add a setLimit for testing purposes!)
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
---