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

kamlesh kjmar
Mega Sage
Mega Sage

Hi @Shridhar1 ,

 

Do you have admin role. It works for admin

 

Regards,

Kamlesh

Yes I have admin access, still no option to remove these records

 

Thanks

Hi Sridhar1,

 

Look for the role that other roles that you want to remove is member of. For example if you add itil role to any user he will get 18 more roles alnog with it that are contained by itil. Now you can't remove selective role from there. Once you remove itil those extra role that you see will automatically remove.

 

So see adding which role added those roles. You will get remove role option for that role only.

 

I hope this helps,

 

Regards,

Kamlesh

One role that I can see youare trying to remove comes from below roles, now to remove that you will have to remove below role only

 

kamleshkjmar_0-1667294185127.png

 

Regards,

Kamlesh