We've updated the ServiceNow Community Code of Conduct, adding guidelines around AI usage, professionalism, and content violations. Read more

Background script to remove itil roles from users and add asset role.

Roshin Chandra
Tera Contributor
 
1 REPLY 1

AnveshKumar M
Tera Sage

Hi @Roshin Chandra ,

You can use the following script to remove the ITIL role from all users and add the asset role.

 

var roleGr = new GlideRecord('sys_user_has_role');
roleGr.addQuery('role', '282bf1fac6112285017366cb5f867469'); // Sys_ID of ITIL Role
roleGr.query();
while(roleGr.next()){
    gs.print('Changing ITIL role to asset role for user: ' + roleGr.getDisplayValue('user'));
    var asRoleGr = new GlideRecord('sys_user_has_role');
    asRoleGr.initialize();
    asRoleGr.user = roleGr.user;
    asRoleGr.role = 'bf7dfe64c0a80169002ee4e83aa20d57'; //asset role sys_id
    asRoleGr.insert();
    roleGr.deleteRecord();
}

 

Note - 1: Very careful while running this script, because it runs on all users who have ITIL role, add proper condition in line 2 to limit the users.

Note - 2: This will not remove the role if the role is inherited from another role or group.

 

Thanks,

Anvesh

Thanks,
Anvesh