How to call an eMail Template from a Mail Script

Peter Williams
Kilo Sage

I would like to know how we can call a eMail Template through a Mail Script for the purpose to have customized emails being sent through the workflow.

1 ACCEPTED SOLUTION

Milind Gharte
Kilo Guru

Hi,

Would it make sense to store that release date in a field on the form? That would make the email easier. You could set it with a business rule. The core script is going to be about the same, but it only needs calculating once, is visible on reports, and easier to retrieve in the email if you've got it on a field on the record.

Just my architectural thoughts.


The business rule would be something like this: (Note this is untested)

Name: Set release date

Table: (your table name)

When: before

Insert: true

Update: false

Advanced: true

Condition: (empty)

Script:

(function executeRule(current, previous /*null when async*/) {

var release = new GlideDateTime(current.sys_created_on);

release.addDaysLocalTime(3);

current.u_release_date = release;

})(current, previous);

The mail script method would be similar:

var release = new GlideDateTime(current.sys_created_on);

release.addDaysLocalTime(3);

template.print(release.getDisplayValue());

 

If it helps,Please mark Correct and 👍 Helpful.

Warm Regards,

Milind

View solution in original post

5 REPLIES 5

Jaspal Singh
Mega Patron
Mega Patron

Hi Peter,

 

Process would be as below.

1. In the notification ensure the Email Template field from What it will contains tab is blank.

2. Then, call mail script in the notification body in format ${mail_script:yourmailscriptname}

3. Create a mail script that GlideRecords the table (sysevent_email_template) that stores all email template.

4. Based on the conditions you use you can call the corresponding template.

So, for instance if my variable is abc then my mail script would have code that would be as below.

(function runMailScript(current, template, email, email_action, event) {

if(current.variables.variable_namehere=='abc')
{
 var gr = new GlideRecord('sysevent_email_template');
  gr.addQuery('name', 'Template_name_here');
  gr.query();
  if (gr.next()){
  template.print(gr.message_html);
}
else
{
 var gr = new GlideRecord('sysevent_email_template');
  gr.addQuery('name', 'Template_name_here');
  gr.query();
  if (gr.next()){
  template.print(gr.message_html);
}
})(current, template, email, email_action, event);

 

Hi Jaspal,

I gone through your code but else part of code is not working for my notification.

If the catalog item is General Access Request then notification is populating as expected but for other catalog item it is blank.

 

 

(function runMailScript( /* GlideRecord */ current, /* TemplatePrinter */ template,
/* Optional EmailOutbound */
email, /* Optional GlideRecord */ email_action,
/* Optional GlideRecord */
event) {

// Add your code here
var ritmno = current.request_item.number;
var gr = new GlideRecord('sc_req_item');
gr.addQuery('number', ritmno);
gr.query();

while (gr.next()) {
if (gr.cat_item.getDisplayValue() == 'General Access request or inquiry') {
template.print("Short Description:" + current.short_description.getDisplayValue() + "<br />");
template.print("Assignment group:" + current.assignment_group.getDisplayValue() + "<br />");
template.print("Assigned to:" + current.assigned_to.getDisplayValue() + "<br />");
template.print("General Access Request:" + current.variables.u_general_access_question + "<br />");
// template.print("Comments:" + current.variables.u_additional_comments + "<br />");

} else {
var gr1 = new GlideRecord('sysevent_email_template');
gr1.addQuery('name', 'sc_task.itil.role');
gr1.query();
if (gr1.next()) {
template.print(gr1.message_html);
}

 

Thanks.

any chance we can calculate the mail scripts inside the html before printing the

template.print(gr.message_html);

 

I tried calling a template that calls another mail script but I can't get it to be calculated, while all the other variables are correctly interpreted.

 

thanks!

DirkRedeker
Mega Sage

Hi

Have a look at the following page on the docs online:

https://docs.servicenow.com/bundle/orlando-servicenow-platform/page/script/server-scripting/referenc...

Have fun

BR

Dirk