- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-06-2023 12:27 PM - edited 06-06-2023 12:30 PM
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
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-06-2023 01:05 PM
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

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-06-2023 12:29 PM
"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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-06-2023 12:37 PM
updates are above

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-06-2023 12:42 PM
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

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-06-2023 12:44 PM
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