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

Mark Roethof
Tera Patron
Tera Patron

"getting errors"

 

So what errors? You are only sharing a script, no explanation or whatever?

 

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

updates are above

I have to check my instance, though just reading your script:

 

Does sys_user_role actually have a field "roles"? Can't recall having ever seen that.

 

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

Oke just checked, out-of-the-box it has not.

 

So please explain what you are trying to do/achieve/etc..

 

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