Delete orphan records from sys_user_has_role table

tiagomacul
Giga Sage

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(","));
}

 

Deleting roles creates empty roles in User's profile (visible after Express to Enterprise conversion...

Some users are having roles that they are not supposed to have

 

I couldn't find plugin and the script didn't delete records.

 

 

 

 

 

 

5 REPLIES 5

Michael Ritchie
ServiceNow Employee
ServiceNow Employee

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.

Hi Michael,

 

 Thank you for answering,  i already ready enable plugin "Contextual Security: Role Management V2" - com.glide.role_management.inh_count.

 

Regards

tiagomacul
Giga Sage

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();
}

pawel_staszewsk
Giga Guru

works also on London release, thanks Tiago.