Email Templates

From ServiceNow Wiki
Home > Script > Server Scripting > Email Templates
Jump to: navigation, search
Notifications
Knowledge.gif Get the Book

Contents

1 Overview

Email templates in the ServiceNow platform enable an administrator to create reusable content for the subject line and message body of email notifications. Templates deliver consistent information on specific system activities and improve the efficiency of creating multiple email notifications for similar actions. If necessary, you can make minor changes on the fly to an email that uses a template by overriding the subject line and message body content in the notification form. A common practice is to define the message body in a template and create new subject lines for different types of recipients. There is no limit to the number of templates that you can create.

In the Aspen release, ${URI} has been replaced with ${URI_REF} in several default email notifications and templates. Customized notifications do not get the change.

2 Creating a Template

In this example, we will create a template called change.update.risk that populates the subject line and message body dynamically with selected information about a change request. The subject line is intended for the user to whom the change request is assigned, but could be redirected by overriding the subject line content in the email notification form. All email notifications that use this template will get information on the same fields in the same format.

  1. To create a new template, navigate to System Policy > Email > Templates.
  2. Click New.
  3. Complete the form as follows:
    • Name:Type a unique name for the template. For example, change.update.risk.
    • Table: Select the name of the table involved. For example, Change Request [change_request].
    • Subject: Type a subject line that explains the purpose of the email. Select the appropriate variables for your subject line from the fields available from your selected table. Place the cursor where you want the variable to appear, and click the field name from the Select variables column. For example, enter:
      ${sys_class_name} ${number} with ${risk} risk has been assigned to you.
    • Message: Construct a message that includes all the information you think the recipients need to know. Select the appropriate variables for your message from the fields available from your selected table. Place the cursor where you want the variable to appear, and click the field name from the Select variables column. The special character, <hr/>, creates a manual break in the message that sets off the Comments section. For example, we can create a message using the following variables:
    Short Description: ${short_description}
    Click here to view request: ${URI}
    Priority ${priority}
    <hr/>
    Work Notes:
    ${work_notes}
    Note: You can construct your message using HTML, or add individual HTML tags to change the font or color of the text, including the contents of the variables. The only exception to this is the appearance of the contents of the ${comments} variable, which cannot be controlled by HTML.
    • Sms alternate: Create a different message to be delivered to your SMS device. You might want a brief message, showing the most important information only. If this message field is blank, the platform uses the contents of the Message field for the SMS message.
    Your template will look like this:
    Change Risk Template Callout.png
  4. Apply your template by selecting it from the drop-down list in the Email template field of an email notification record.
  5. To override a template value, type a new value for field you want to override. For example, you might change the subject line to read The risk level of ${sys_class_name} ${number} has been raised to ${risk}. This would make the email notification appropriate for the change manager who needs to know when risk levels are escalated above a certain threshold, but not change the basic information about the change request contained in the template.

2.1 Tips

Use HTML tags to format your email body for readability. For example:

  • You can use <br/> to insert a line break
  • You can wrap paragraphs in <p> ... </p> tags to format paragraph text.

3 Using JavaScript in Email Templates

The Message field in email notifications and email templates supports the syntax for embedding JavaScript to dynamically create text for the email body. To embed JavaScript, begin and end the script with <mail_script> and </mail_script> tags respectively. To emit text to the body of the message, use the template.print("a string") function.


JavaScript in Templates.png

3.1 Parm1 and Parm2

The event.parm1 and event.parm2 properties that come from the originating event can also be used within the <mail_script>. To use these, simply type "event.parm1" or "event.parm2" in the mail script.


4 Enabling Links to ServiceNow Records

Adding the special ${URI} parameter to an outbound email body or template creates a link to a specific ServiceNow record. When a user clicks on the word LINK, the instance prompts the user to log in if not already logged in, and then redirects the user to the record specified in the URI.

Link displayed by ${URI} parameter

The ${URI} parameter has an extension called the ${URI+} format to specify additional arguments in the email link, such as sysparm terms, in addition to the automatically created URI. For example:

${URI+&sysparm_scriptlet=current.assigned_to=gs.getUserID()
&sysparm_scriptlet_condition=current.assigned_to.nil()
&sysparm_view=incident_active}

This example executes the JavaScript:

current.asigned_to=gs.getUserID()

when the condition of

current.assigned_to.nil()

is satisfied. Additionally, the script sets the view to incident_active.

4.1 Changing the Link Text

To show the display value of the record as the link text instead of the word LINK, use the ${URI_REF} parameter instead of the ${URI} parameter.

Link displayed by ${URI_REF} parameter

For example, if the URL displays an incident record, the link text is the incident number, which is the display value for incidents. If the URL displays a user record, then the link text is the user name.

4.2 Linking to Related Records

A notification can link to a related record by specifying a reference field in front of the ${URI} or ${URI_REF} parameters. Format the related record link as follows:

  • ${<reference field that contains the related record you want to display>.URI}
  • ${<reference field that contains the related record you want to display>.URI_REF}

For example:

Related Record to Provide Link to Notification record table Reference Field Samples
Related task record to be approved from an approval notification Approval [sysapproval_approver] Approval for [sysapproval]
  • ${sysapproval.URI}
  • ${sysapproval.URI_REF}
Related problem record in an incident notification Incident Problem [problem_id]
  • ${problem_id.URI}
  • ${problem_id.URI_REF}

For example, the following notification template produces the email links in the picture below:

Click here to view Incident: ${URI_REF}
Click here to view Related Problem: ${problem_id.URI_REF}
Example related record links

4.3 Linking to Content Pages

You can create links to content management pages using the following format:

${CMS_URI+<site>/<page>}

For example, to link the email recipient to a page called Incident in the content site ESS, with the current incident as the target document, use the following format:

${CMS_URI+ess/incident_detail}

The resulting email URL has this format:

https://<instance name>.service-now.com/ess/incident_detail.do?sysparm_document_key=incident,46e18c0fa9fe19810066a0083f76bd56

5 Calendar Integration

To integrate ServiceNow changes with your Outlook (or other) calendar, add one or more of the following iCalendar variables to your email template:

Variable Name Description
${dtstart} Start Date
${dtend} End Date
${location} Location
${alarm_time} Alert or reminder time

ServiceNow computes the value of iCalendar variables using a legacy import export map. By default, ServiceNow provides iCalendar import export maps for the following tables:

  • change_request
  • reminder
  • itil_appoint
  • incident
Legacy import export maps

Each import export map can specify a different set of iCalendar fields. For example, the icalendar.change_request import export map only maps two iCalendar fields.

Sample mappings for the dtstart and dtend variables

ServiceNow uses the External Name as the variable name in the email template. For example, the icalendar.change_request import export map defines the dtstart and dtend variables.

External Name Associated Variable Name Table Field Mapped Field Label
dtstart ${dtstart} change_request start_date Planned start date
dtend ${dtend} change_request end_date Planned end date

5.1 Creating Custom Mappings to iCalendar Fields

If you want to generate iCalendar invitations that use values from custom tables, you must create an import export map that computes the values of the iCalendar fields.

  1. Create the custom table and fields for the email template.
    Use the Date/Time (glide_date_time) field type for fields that will map to the ${dtstart} and ${dtend} variables.
  2. In the Filter text search, enter sys_impex_map.list.
  3. Filter the list on Type is icalendar.
  4. Click New.
  5. For Name use the following naming convention: icalendar.<table name>. For example, icalendar.u_my_custom_table.
  6. Right-click the form header and click Save.
  7. From the Field Maps related list, click New.
  8. From the Mapping Entry Wizard, select Mapping to a Database Field.
  9. Create field mappings for the iCalendar fields you want to use. For example, map the External Name dtstart to the u_meeting_start_time field from the u_my_custom_table table.
    Sample field mapping dtstart

  10. Click Submit.
  11. Repeat steps 7 through 9 for each iCalendar field you want to map.
  12. Click Update.


Here are sample field mappings between iCalendar variables and custom fields from a custom table:

External Name Database Field Type Map Table
dtstart u_meeting_start_time field icalendar.u_my_custom_table u_my_custom_table
dtend u_meeting_end_time field icalendar.u_my_custom_table u_my_custom_table
location u_meeting_location field icalendar.u_my_custom_table u_my_custom_table
Was this article helpful?
Yes, I found what I needed
No, I need more assistance
Views
Personal tools