create a fix script to add evt_mgmt_operator role to itil , getting errors

ServNowDev
Tera Guru

 

 

addRoletoRole();

function addRoletoRole(){
    // Find the ITIL role
    var itilRole = new GlideRecord('sys_user_role');
    itilRole.addQuery('name', 'ITIL');
    itilRole.query();

    if (itilRole.next()) {
        // Check if the evt_mgmt_operator role already exists in ITIL role
        var itilRoles = itilRole.getValue('roles');
        if (itilRoles.indexOf('evt_mgmt_operator') === -1) {
            // Add evt_mgmt_operator role to the ITIL role
            var newRoles = itilRoles + ',evt_mgmt_operator';
            itilRole.setValue('roles', newRoles);
            itilRole.update();
            gs.info('evt_mgmt_operator role added to ITIL role successfully.');
        } else {
            gs.info('evt_mgmt_operator role already exists in ITIL role.');
        }
    } else {
        gs.error('ITIL role not found.');
    }
}

 

getting error below:

 

Evaluator: com.glide.script.RhinoEcmaError: Cannot convert null to an object.
   script : Line(12) column(0)
      9:     if (itilRole.next()) {
     10:         // Check if the evt_mgmt_operator role already exists in ITIL role
     11:         var itilRoles = itilRole.getValue('roles');
==>  12:         if (itilRoles.indexOf('evt_mgmt_operator') === -1) {
     13:             // Add evt_mgmt_operator role to the ITIL role
     14:             var newRoles = itilRoles + ',evt_mgmt_operator';
     15:             itilRole.setValue('roles', newRoles);

[0:00:00.022] Total Time
1 ACCEPTED SOLUTION

Explanation:

evt_mgmt_operator contains evt_mgmt_user role

evt_mgmt_user contains sam_core_user role

sam_core_user contains itil role

 

So itil is already contained inside evt_mgmt_operator

 

View solution in original post

13 REPLIES 13

I need all user with the itil role to have the role evt_mgmt_operator role as well

Shouldn't you be checking the sys_user_has_role table then? Or sys_user and it's roles field?

Now you are querying the sys_user_role table, which will simply not work.

 

Kind regards,
Mark

 

Kind regards,

 

Mark Roethof

Independent ServiceNow Consultant

10x ServiceNow MVP

---

 

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

LinkedIn

Manmohan K
Tera Sage

Hi @ServNowDev 

 

Why are you adding role via script

You should be adding it directly from System UI

adding the role(evt_mgmt_operator) to the itil role will not take for some reason 

Can you share the error 

Also rather than adding evt_mgmt_operator in the itil role, consider creating a custom role and add itil and evt_mgmt_operator role to it

This way you do not give additional access to people having itil role