Not able to delete empty role references from sys_user_has_role table

Parveen28
Tera Contributor

In the sys_user_has_role table (User Roles) which contain rows of empty role references while we neither have (empty) role in sys_user_role table nor within any assignment group. So as per the analysis if we want to delete the empty role references from sys_user_has_role if it is not needed but looks like there is some OOTB restrictions to delete those empty references. Please suggest in this case how we can do this clean up for empty role references from sys_user_has_role table.

6 REPLIES 6

Sandeep Rajput
Tera Patron
Tera Patron

@Parveen28 Do you get any errors when you try to delete such records?

Parveen28
Tera Contributor

Hi Sandeep,

Upon executing the script some records has been deleted but with the rest of the records same script is not working and UI Action on selected row is also disabled.

 

Thx

 

Hi @Parveen28 

 

Can you try with deleteMultiple() once?

 

Regards,

Amit

Sumanth16
Kilo Patron

Hi @Parveen28 ,

 

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 & Regards,
Sumanth Meda