I want to delete a specific user's role from scripting

Ravi Borade
Giga Contributor

i've tried this but its not working as we can't delete the record even from table ui level 

also tried to switch to security admin and tried to delete it its not working

basically we can't delete this record

var gr = new GlideRecord('sys_user_has_role');
gr.addQuery('user','sys_id of the user');
gr.query();

while(gr.next()){
    gr.deleteRecord();
}
1 ACCEPTED SOLUTION

Naga Ravindra R
Kilo Sage
Hi Ravi,
 
try this below:

var userRole = new GlideRecord('sys_user_has_role');
userRole.addQuery('sys_id', '03ad33e1c08d2300964f80d1691e1a4b'); //Here you need to find which record you want to delete for that user in above table.
userRole.query();
if(userRole.next()){
    userRole.deleteRecord();
}

If my response helps please mark as correct/helpful. šŸ™‚

View solution in original post

7 REPLIES 7

Nayan  Dhamane
Kilo Sage
Kilo Sage

Hello Ravi,

Mostly the user role are driven by the group to which the user is added you can first try to remove user form the group and then see if still roles are present and then try to remove the roles.

Please mark my answer correct/ helpful.

BR,
Nayan

If my answer solved your issue, please mark my answer as āœ…Correct & ļ‘Helpful based on the Impact

Best Regards,
Nayan Dhamane
ServiceNow Community Rising Star 2023.

Mark Manders
Mega Patron

So you can't delete any role from this user, or does he only has one role left? Is the role inherited? I had this issue once when an OOB role was included in a custom role and that custom role got deleted. It was impossible to delete the role from the user (scripting/ui). We had to log a Hi case and it was done on database level (asked for the script, but didn't get it). You could try that.

If my answer helped you in any way, please then mark it as helpful. If it resolved the issue, please mark it as correct. This way others will find it in the solved queue and helps them on similar queries.

Mark


Please mark any helpful or correct solutions as such. That helps others find their solutions.
Mark

Sagar Pagar
Tera Patron

Hi,

I would suggest to check role is inherited or not? or directly assigned to user. based on that you have to write a scripts.

 

Thanks,

Sagar Pagar

The world works with ServiceNow

Naga Ravindra R
Kilo Sage
Hi Ravi,
 
try this below:

var userRole = new GlideRecord('sys_user_has_role');
userRole.addQuery('sys_id', '03ad33e1c08d2300964f80d1691e1a4b'); //Here you need to find which record you want to delete for that user in above table.
userRole.query();
if(userRole.next()){
    userRole.deleteRecord();
}

If my response helps please mark as correct/helpful. šŸ™‚