- 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:47 PM
I need all user with the itil role to have the role evt_mgmt_operator role as well

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-06-2023 12:50 PM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-06-2023 12:38 PM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-06-2023 12:39 PM
adding the role(evt_mgmt_operator) to the itil role will not take for some reason
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-06-2023 12:44 PM
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