- 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 11:15 AM
Ah-ha. So, it works with current.caller_id. I think there may have been some confusion because the label say 'Requested for', but its field name in caller_id. But getting the user id then checking for the 'itil' role fixed the issue. Thanks, Mike!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-19-2018 08:23 AM
Few question:
- How/when does it go out?
- What is the content of the email?
- Who does it go to?
The reason I ask is the roles the current user has may be irrelevant to the email. I may trigger the email to go out to someone else, so whether I have "itil" or not should not matter. It is the recipient that matters.
Take a look at the "Incident commented for ITIL" and "Incident commented for ESS" Notifications as examples. They are different because the audiences are different, which I believe is what you are looking for.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-19-2018 08:38 AM
It goes out with incident notifications. So, this email script will either provide the 'ticket link' or 'ESS link' with each notification. It should go to the 'requested for'.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-19-2018 09:04 AM
In that case, we don't want to use gs.getUser() you'll want to check if current.requested_for has the ITIL role.
You can try this:
var isITIL = current.requested_for.hasRole('itil');
if(isITIL) {
// has itil role logic
} else {
// no itil role logic
}
If that doesn't work, or you want it to be more like hasRoleExactly() you can try this:
var gr = new GlideRecord('sys_user_has_role');
gr.addQuery('user', current.requested_for);
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 10:03 AM
Gave this a try. Ended up with role being undefined each time.