Email notification - Check recipients role on all emails sent
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-27-2017 12:06 PM
Hi SNOW Dev Community,
Question around email notifications that get sent referencing a records number. So what I am doing is whenever an email is getting sent out that includes my notification script the script is checking the users role and spitting out a template print based on if the user has the itil role or not. I am only using this when I go to reference a records number.
Scenario -
Incident notification goes out with incident details. The details include the incident number. I want the number be just the number if the user does not have an ITIL role and a URI_REF if the user does have an ITIL role.
<tr><th>Incident #:</th>
<td>${mail_script:incident.link.role.check}</td>
</tr>
<tr><th>Opened At:</th>
<td>${opened_at}</td>
</tr>
<tr><th>Opened By:</th>
<td>${opened_by}</td>
</tr>
Notification Script -
attachLink();
function attachLink() {
//Check for requestor's role
var userObj = gs.getUser().getUserByID(current.caller_id);
if (userObj.hasRole('itil')) {
template.print('${URI_REF}');
}
else {
template.print('${number}');
}
}
Instead of making it so that the script checks for a specific field, in this case the caller_id, can I make it so that it just checks whomever the email is getting sent to? I only need this check to happen when my notification includes a ${number} or ${URI} reference.
Thoughts?
Thanks in advance,
Sean
- Labels:
-
Best Practices
-
Scripting and Coding

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-27-2017 12:33 PM
We do something similar using full GlideRecord:
gr = new GlideRecord('sys_user_has_role');
gr.addQuery('user', current.caller_id);
//check if ITIL role, if yes - RoledUser is yes, Show Take me to Incident button
gr.addQuery('role','282bf1fac6112285017366cb5f867469');
gr.query();
if (gr.next()) {
//add the rest of your code here
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-27-2017 12:58 PM
Thank you Mike. I guess I failed at explaining my concern though. I want it to that I can carry the script over into all tables examples being Change Requests, Requests, Projects, etc. So making the script check the role of the recipient of the notification instead of the field at which the recipient is in.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-27-2017 01:09 PM
Two things come to mind:
1) create a new reference field at Task level and populate caller/requested for/etc into that field for each of the tables and use that in your script
2) check the different fields and if not empty, use that field. Example, if !caller_id.nil() - use that value, else if !requested_for.nil() - use that, etc
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-15-2018 10:26 AM
Hello Sean
Did you get any where on this? I have the same request - basically one mail script that will send itil users to the back end and non-itil to the portal to see the record. Ive got the below so far but it aint working 100%.
--------------------------
(function runMailScript(current, template, email, email_action, event) {
var userObj = gs.getUserName();
if (userObj.hasRole('itil')) {
template.print('<a href="nav_to.do?uri=sc_request.do?sys_id=${request.sys_id}">${request.number}</a>');
}
else {
template.print('<a href="sp?id=sc_request&table=sc_request&sys_id=${request.sys_id}">${request.number}</a>');
}
//}
})(current, template, email, email_action, event);
---------------------------
Steve