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

Yousaf
Giga Sage

Hi Shridar,
You cannot delete the role from here these roles are in sys_user_role table. You need to click edit button and remove the groups you want from slush bucket in user record
And if you want to delete the role go to sys_user_role 

Mark Correct and Helpful if it helps.


***Mark Correct or Helpful if it helps.***

Yousaf_0-1667293068980.png

 

 


***Mark Correct or Helpful if it helps.***

Shridhar1
Tera Contributor

Shridhar1_0-1667293413559.png

For user records no groups were showing
Also I tried removing users from 'sys_user_role' but upon clicking Edit button these user records not showing in the list

Shridhar1_1-1667293632897.png

Shridhar1_3-1667293751747.png

 

 

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

Thank you Aishwarya 

 

This script worked, I could able to remove user roles