- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-27-2024 09:08 PM
I'm trying to remove roles from a user, but they're not being removed.
[Image 1] This is the user I want to remove roles from.
[Image 2] I've removed the roles in the Edit Role section and saved.
[Image 3] The roles are still listed in the Related list tab.
When I check the User tab from the Role itself, the user account is still there.
What should I do to Remove Roles?
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-30-2024 11:41 PM
Hi @yana7 ,
Please try the below in background script:
var userRole = new GlideRecord('sys_user_has_role');
userRole.get('61737f1bc36602103869d64d050131ee'); //Replace SysId of the role relationship in the [sys_user_has_role] table
userRole.inherited = false; //Updating it to false, then only we will be able to delete it.
userRole.update();
userRole.deleteRecord();
Highlighted sys_id is what you need to replace in the above code as per your requirement
Note: Please try in non prod first and check and it is not recommended to run background script in Prod.
Mark this as Helpful / Accept the Solution if this helps
Mark this as Helpful / Accept the Solution if this helps.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-30-2024 08:09 PM
Hi,
Try below script-
var userRole = new GlideRecord("sys_user_has_role");
userRole.addQuery('role','PUT SYS ID OF ROLE');
userRole.addQuery('user','PUT SYS ID OF USER FOR WHICH ROLE NEEDS TO BE REMOVED');
userRole.query();
if(userRole.next()){
userRole.deleteRecord();
}
Regards,
Vaishnavi L
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-30-2024 11:06 PM
This doesn't work
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-30-2024 11:41 PM
Hi @yana7 ,
Please try the below in background script:
var userRole = new GlideRecord('sys_user_has_role');
userRole.get('61737f1bc36602103869d64d050131ee'); //Replace SysId of the role relationship in the [sys_user_has_role] table
userRole.inherited = false; //Updating it to false, then only we will be able to delete it.
userRole.update();
userRole.deleteRecord();
Highlighted sys_id is what you need to replace in the above code as per your requirement
Note: Please try in non prod first and check and it is not recommended to run background script in Prod.
Mark this as Helpful / Accept the Solution if this helps
Mark this as Helpful / Accept the Solution if this helps.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-01-2024 07:38 PM
it works,Thank you very much friend