- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-19-2018 06:01 AM
Hello,
I'm working on an email script that should include a link to the incident ticket if the user role is 'itil' and if anything else they'll be directed to the ESS portal.
My if/else statement is working. But for some reason .hasRole is always 'true'. I thought it was because I was logged in as an admin, but when I impersonate a user with no roles, I still only get a notification with the incident link.
Can anyone spot what I'm getting wrong?
Thanks!
var currentUser = gs.getUser();
var link = current.getLink();
template.print(currentUser.hasRole('itil'));
if (currentUser.hasRole('itil')) {
template.print('<a href="' + link + '">GO TO INCIDENT</a>');
}
else {
template.print('<a href="https://dev.service-now.com/ess">GO TO PORTAL</a>');
}
Solved! Go to Solution.
- Labels:
-
Incident Management

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-19-2018 10:28 AM
just to be clear you want to show go to Incident only when caller_id user has itil role?
var currentUser = gs.getUser().getUserByID(current.caller_id).hasRole('itil');
var link = current.getLink();
template.print(currentUser);
if (currentUser){
template.print('<a href="' + link + '">GO TO INCIDENT</a>');
}else{
template.print('<a href="https://dev.service-now.com/ess">GO TO PORTAL</a>');
}
if you want to get other field than you need to change current.u_requested_for in belwo code.
var currentUser = gs.getUser().getUserByID(current.u_requested_for).hasRole('itil');
var link = current.getLink();
template.print(currentUser);
if (currentUser){
template.print('<a href="' + link + '">GO TO INCIDENT</a>');
}else{
template.print('<a href="https://dev.service-now.com/ess">GO TO PORTAL</a>');
}

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-19-2018 06:05 AM
Hi
Have you tried withcurrentUser.hasRolesExactly('role_name') ?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-19-2018 06:16 AM
I tried that. Then it becomes 'undefined'.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-19-2018 06:34 AM
How are you testing this? hasRoleExactly() is a client side method so you won't be able to use it here. You may need to simulate the function by querying the sys_user_has_role table and check if there is a record where the current user has this role and inherited is false, something like:
var currentUser = gs.getUser();
var gr = new GlideRecord('sys_user_has_role');
gr.addQuery('user', currentUser);
gr.addQuery('role', '282bf1fac6112285017366cb5f867469'); // ITIL Role Sys Id
gr.addQuery('inherited', false);
gr.query();
if(gr.next()) {
// has itil role logic
} else {
// no itil role logic
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-19-2018 07:11 AM
I've been testing by adding the email script to a working notification. Then using the preview to see the body. I change to different INC that have been opened by different users with both roles.
I tried your code, which is a big help, but now I'm always getting a 'false' response. I think I'm missing something. Not sure what, though.
var currentUser = gs.getUser();
var link = current.getLink();
var gr = new GlideRecord('sys_user_has_role');
gr.addQuery('user', currentUser);
gr.addQuery('role', '282bf1fac6112285017366cb5f867469'); // ITIL Role Sys Id
gr.addQuery('inherited', false);
gr.query();
if(gr.next()) {
template.print('<a href="' + link + '">GO TO INCIDENT</a>');
} else {
template.print('<a href="https://dev.service-now.com/ess">GO TO PORTAL</a>');
}