
- Subscribe to RSS Feed
- Mark as New
- Mark as Read
- Bookmark
- Subscribe
- Printer Friendly Page
- Report Inappropriate Content
Have you worked with email client templates before to not have to enter the same information again and again? I recently had two requests related to them, and I wanted to share what I have found. Email client templates help you to create default mail content for a specific table (the incident table, for example) that will be used when a user clicks on the mail icon in the record header. Scripts make this an even better solution, since you can create dynamic content in your email template.
Determine the recipient through record fields without scripting
You can use the field names in the TO or CC fields of the email client template. For example:
TO: caller_id,watch_list
CC: sys_created_by
How to use scripting in email client templates
Say, for example, that you want to send an email to the Helpdesk manager in addition to the caller and watch list when a P1 incident is opened, but for all other priorities, you just want to email the caller and the watch list. How would you do this?
Send an email to the Helpdesk Manager only when a high priority incident is opened
You can use (server side) java script in email client templates. This is accomplished by starting the To: field content with "javascript:"
This code was tested and works in the To: field:
A weakness of the following script: hardcoded email addresses in the watch list will not work.
javascript:
var watchlist_email="";
if (current.priority==1) {
watchlist_email="helpdeskmanager@mycompany.com;";
}
var watcher=new GlideRecord("sys_user");
watcher.addEncodedQuery("sys_idIN" + current.watch_list.toString());
watcher.query();
var watchlist_email=watchlist_email + current.caller_id.email + ";";
while (watcher.next()) {
watchlist_email=watchlist_email + watcher.email + ";";
}
watchlist_email=watchlist_email;
recipients=watchlist_email;
}
At this time, simple scripts like the one above can be used. But complex scripts, such as scripts that can include both user names and hard-coded emails for a watchlist, need further consideration.
While testing, we found two limitations:
- You cannot put the same users into TO and CC, so if you want to run two scripts on these two fields, make sure they get different results.
- You cannot have html-type characters in your script, it will not render properly in the email client. We found this to be an issue with comma, less than, and greater than.
Create a complex script to determine email recipients
The workaround for the second limitation is to create a script include and call the script include in the TO or CC field.
Sample script include (named TemplateRecipientBuilder)
var TemplateRecipientBuilder = Class.create();
TemplateRecipientBuilder.prototype = {
initialize: function() {
},
build: function() {
if (current.short_description.startsWith("test")) {
return "testemail@somwhere.net";
}
else {
var watcher=new GlideRecord("sys_user");
watcher.addEncodedQuery("sys_idIN" + current.watch_list.toString());
watcher.query();
var watchlist_email="";
while (watcher.next()) {
watchlist_email=watchlist_email + watcher.email + ",";
}
watchlist_email=watchlist_email + current.caller_id.email;
var watchlist_str=current.watch_list.toString();
var watchlist=watchlist_str.split(",");
for (j=0;j < watchlist.length;j++) {
if (watchlist[j].indexOf("@")>0) {
watchlist_email = watchlist_email + "," + watchlist[j];
}
}
}
return watchlist_email;
},
type: 'TemplateRecipientBuilder'
};
This script include is then called from either TO or CC in the email client template as follows:
javascript: new TemplateRecipientBuilder().build();
If you are not using the email client templates, using scripted email client templates will help you promote quick, easy, and consistent emails sent from the email client within your current record. You can use simple scripts, or create more complex scripts by using the script include creation highlighted in this article.
- 6,824 Views
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.