Delete User Roles in bulk

Vamshi_ch123
Tera Contributor

Hi All,

 

I'm trying to delete the roles added by default to the users by using below script in the background but this is not working and not allowing to delete the records

 

var userRole = new GlideRecord('sys_user_has_role');
userRole.addEncodedQuery("sys_updated_onON2024-05-29@javascript:gs.dateGenerate('2024-05-29','start')@javascript:gs.dateGenerate('2024-05-29','end')");
userRole.query();

while (userRole.next()) {
    userRole.deleteRecord();
    gs.print('Deleted role assignment with sys_id: ' + userRole.sys_id);
}

gs.print('Role removal process completed.');
 
Please help
thanks & Regards
6 REPLIES 6

Mark Roethof
Tera Patron
Tera Patron

Hi there,

 

Technically you can still delete inherited roles. Though... why? Because actually functionality something is going wrong then. What are you doing about that? Or are you just ignoring that?

 

Kind regards,

 

Mark Roethof

Independent ServiceNow Consultant

10x ServiceNow MVP

---

 

~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field

LinkedIn

Hi Mark,

 

The users are added with itil roles when we have moved some update sets to prod from dev which were not required and I'm trying to delete the records only that got created today after moving this data from sys_user_has_role

 

I don't think it will affect