You are currently viewing List Of Basic Scripts in ServiceNow Email Notifications

List Of Basic Scripts in ServiceNow Email Notifications

We have to use many scripts inside ServiceNow email notifications, whether they are in the notification or in the template. In this post, I have collected all those necessary scripts which will be very useful for you and save you time. Apart from this, please let us know in the comment section if you need any other script.

How do you add Notification Email Scripts to email notifications?

${mail_script:name of notification script}
Example:
${mail_script:collaboration_group_message_body}
where:
name of notification script = collaboration_group_message_body

Add an attachment to a notification

You can add an attachment to a notification by linking to the attachment record in the notification message. Email recipients log in to the instance to view the attachment record after clicking the link.

template.print( 'Attachment: <a href="/sys_attachment.do?sys_id=' + now_GR. sys_id + '">' + now_GR. file_name + '</a>\n ' ) ;

Attaching reports using the Sys ID

You can also attach various reports, including gauges, dashboards, and charts, to a notification.

${report:X:Y}
where:
X is the type of report you want to attach (reportID, gaugeID, dashboardID, or chartID).
Y is the sys ID of the report, gauge, dashboard, or chart to be attached.
Example:
• ${report:reportID:1498b7489f232100d5f9b3e2957fcf79}
• ${report:gaugeID:4578b7489f232100d5f9b3e2957fcf79}
• ${report:dashboardID:7896b7489f232100d5f9b3e2957fcf79}
• ${report:chartID:3256b7489f232100d5f9b3e2957fcf79}

Email notification variables

Use notification variables to display dynamic information in the notification body, such as a field value, a link to a record, or a link to system preferences.

Syntax : ${variable-name+variable-parameters}

The variable-name portion is always required. Not all notification variables support the variable-parameters portion. When available, most variable parameters are optional.

VariablesDescriptionExample
${field-name}Display the value of the specified field.SourceIncident ${number} - comments added
OutputIncident INC1000001 – comments added
${image-field-name}Display an image associated with a
record.
Source<img src='${picture}?t=medium'/>
Output: Image displayed
${URI}Display a link to the current record. The link text is the word LINK.SourceClick here to view incident: ${URI}
OutputExample that shows how the link text word Link serves as the link to the current record
${URI_REF}The link text is the display value
of the record.
SourceClick here to view ncident: ${URI_REF}
OutputExample that shows how the link text displayed is the value of the record
${CMS_URI}Display a link to the specified record within a CMS page.Source${CMS_URI+ess/incident_detail}
Output: a link to a target CMS page

How to add an unsubscribe link in an email attachment?

Use the code snippet below to add the unsubscribe option to the notification.

${NOTIF_UNSUB} from this notification by email or ${NOTIF_UNSUB+link_text="click here"}.
Manage your ${NOTIF_PREFS} or ${NOTIF_PREFS+link_text="click here"}.
Sample email

How to print in the mail script?


template.print("message"); //outputs message to the email body
template.space("number ofspaces"); //outputs spaces to the email body

What is the use of email_action in Notification Email Scripts?

In ServiceNow email scripts, email_action contains the GlideRecord for the email notification. Using the dot operator we can directly use the values from the notification.

Examples -
email_action.name - email notification name
email_action.sys_id - email notification sys_id
email_action.category.getDisplayValue() - email notification category

What is the use of events in Notification Email Scripts?

In ServiceNow email scripts, the event object contains the GlideRecord object for the event that fired the notification (sysevent). Using the dot operator we can directly use the values from the event.

Examples - 
event.parm1 - parameter 1 of event
event.parm2 - paramter 2 of event

How to add cc/bcc in email notification ServiceNow?

Yes, you can add cc in the ServiceNow email notifications email variable with the addAddress method.

Format adding cc/bcc-
addAddress(String type, String address, String displayname)
Example-
email.addAddress("cc","john.copy@example.com","John Roberts");
email.addAddress("bcc","john.secret@example.com","John Roberts");

How do I set the values of email from the mail script?

We can use the following methods to change field values within an email dynamically.

Syntax -
setFrom(String address): override the sender address
setReplyTo(String address): override the reply to address
setSubject(String subject): override the subject of the message
setBody(String message): override the body of the message
Examples -
email.setFrom(current.caller_id.email);
email.setReplyTo("joe.employee@yourcompany.com");
email.setSubject("This is the new subject line");
email.setBody("This is the new body");

Scripting examples for email notifications?

A simple text string is the most basic example of the way a mail script works. This script prints out “Incident number – INC00001″.

template.print("Incident number - "+ current.number);

More advanced scripting examples for email notifications?

Sample Script -
template.print("Summary of Requested items:<br />"); 
var now_GR = new GlideRecord("sc_req_item");
gr.addQuery("request", current.sysapproval);
gr.query();
while(gr.next()) {
 template.print(gr.number + ": " + gr.quantity + " X " + gr.cat_item
.getDisplayValue() + " at " + gr.cat_item.price.getDisplayValue() + " 
each <br />");
}
Output -
Summary of Requested items:
RITM0001:3XMobile Phone at $500 each
RITM0002:1XDesk Phone at $200 each
RITM0002:2XSatalite Phone at $800 each

end footer