Not able to delete record from sys_user_has_role

Shridhar1
Tera Contributor

Hello,

 

We recently imported user records from one instance to other. Due to this import Roles associated to users also got imported

We want to remove roles associated to user records and option not available

 

Shridhar1_1-1667291595692.png

We checked option not available on 'sys_user_has_role' table to delete these records

 

Is there any way to remove/delete these roles from user record ?

 

Thanks

1 ACCEPTED SOLUTION

AishwaryaShukla
Kilo Guru
Kilo Guru

Hi @Shridhar1 ,

The challenge here is that the roles are inherited. So you can't delete them directly through a script as well.

You first need to set inherited as 'false' and then perform the delete action using a background script.

You can do both in the same script execution.
Refer below:

var userRoleGr = new GlideRecord('sys_user_has_role');
userRoleGr.addEncodedQuery('user=62d4a181c0a8010e0189dd6ec22324a2^role=3bf8d5b65344130084acddeeff7b122b'); // use your desired query here.
userRoleGr.query();
gs.print(userRoleGr.getRowCount()); // to verify the count

while(userRoleGr.next()){
userRoleGr.setValue('inherited', false);
userRoleGr.update();
userRoleGr.deleteRecord();
}
 
Try this and let me know if it solves your issue.
Please mark this as helpful/correct for others to benefit from this.

Thanks,
Aishwarya

View solution in original post

11 REPLIES 11

Glad to know Shridhar!

Saurav11
Kilo Patron
Kilo Patron

Hello,

 

The reason why you are not able to remove this role from the User is because they are inherited role:-

 

Saurav11_0-1667293629107.png

 

That means these user have some role which is granting them all these role. So you cannot individually remove these roles without removing the parent role.

 

To check which is the parent role click on the role inheritance map you will see something as below:-

 

Saurav11_1-1667293999519.png

So either remove the parent role 

 

OR 

 

Go to roles and you will need to remove the child role from the parent role:-

 

Saurav11_2-1667294108772.png

 

Please mark my answer as correct based on Impact.