Delete orphan records from sys_user_has_role table

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-30-2018 11:46 AM
Hi Team,
Platform is Jacarta
I have an issue with deleting orphan records from sys_user_has_role table.
Gee, answered, install plugin
Contextual Security: Role Management Enhancements (com.glide.role_management.inh_count)
https://community.servicenow.com/community?id=community_question&sys_id=c164c769dbd8dbc01dcaf3231f9619e8
workarround
deleteEmptyRoles();
function deleteEmptyRoles() {
//script to delete the empty roles a user contains
var roleL = [];
var gr = new GlideRecord("sys_user_has_role");
gr.addNullQuery('role.sys_id');
gr.query();
gs.print("Number of records that will be deleted: " + gr.getRowCount());
while (gr.next()) {
roleL.push(gr.getValue('sys_id'));
//gr.deleteRecord();
}
gs.print("the sys_ids of the records that will be deleted are: ");
gs.print(roleL.join(","));
}
Some users are having roles that they are not supposed to have
I couldn't find plugin and the script didn't delete records.
- 2,440 Views
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-30-2018 11:54 AM
Search for the plugin based on the ID:
com.glide.role_management.inh_count
You referenced this post above, see the last reply regarding that you have to change the inherited flag to false otherwise you cannot delete the records.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-30-2018 12:07 PM
Hi Michael,
Thank you for answering, i already ready enable plugin "Contextual Security: Role Management V2" - com.glide.role_management.inh_count.
Regards

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-02-2018 08:02 AM
SOLVED
var objUser = new GlideRecord("sys_user_has_role");
objUser.addQuery("user.user_name", "tiago.macul");
objUser.query();
while (objUser.next())
{
gs.print(objUser.user.user_name + "|" + objUser.role.name + "|" +objUser.inherited);
objUser.inherited.setValue(false);
objUser.setWorkflow(false);
objUser.update();
objUser.deleteRecord();
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-13-2019 01:56 AM
works also on London release, thanks Tiago.