Application Automation · Topic 4 of 4
Sending and Receiving Email
ServiceNow CAD email: build notifications on record changes or events, choose recipients, use email scripts, and handle replies with inbound email actions.
- 5 min
- Notifications have three parts: when to send, who will receive, what it will contain.
- Send when: record inserted or updated, or an event is fired.
${field}inserts values;${mail_script:name}runs an email script that usestemplate.print().
What it is
An application sends email through notifications: records that say when to send, who receives the message and what it contains. It receives email through inbound email actions: scripts that run when a message arrives and create or update records.
Both are application files, so they are packaged with the application. Sending and receiving must also be enabled in the instance's email properties.
How it works
When to send
A notification is built on a table and starts in one of two ways.
| Send when | Use |
|---|---|
| Record inserted or updated | Simple cases, with conditions such as State changes to Approved |
| Event is fired | The logic is in a script that calls gs.eventQueue, or one event drives several responses |
Use the event option when the decision is complex or the recipient comes from a script. The event's parm1 and parm2 can carry recipients or values.
Who receives it
| Recipient option | Meaning |
|---|---|
| Users | Named users |
| Groups | Every member of named groups |
| Users/Groups in fields | Users or groups in reference fields on the record, such as Requested for |
| Event parm 1 or parm 2 contains recipient | The event passes the recipient |
| Send to event creator | Whether the user who caused the event gets the email when they are a recipient |
Recipients from fields are the most maintainable choice, because they follow the data on each record.
What it contains
The subject and body can include field values with ${field_name}, such as ${number} or ${requested_for}. Dot-walking works too, such as ${requested_for.manager}.
For logic in the body, write an email script and include it with ${mail_script:script_name}. The script writes into the message with template.print().
// email script x_acme_travel_trip_summary
var util = new TravelUtils();
template.print('Estimated cost: ' + current.estimated_cost);
if (util.isOverBudget(current)) {
template.print('<br>This trip is over budget.');
}
An email template stores a subject and body that several notifications can reuse.
Receiving email
Inbound email is classified as new, reply or forward. A reply is matched to its record through the watermark, such as Ref:MSG0000008, that outbound email carries in the subject or body.
An inbound email action has a target table, a type (New, Reply or Forward), an optional condition and an order. Its action type is Record Action to change a record or Reply Email to send a response back.
In the script, current is the target record, and email holds the message, such as email.subject and email.body_text.
// inbound action, type Reply, table x_acme_travel_request
current.comments = 'Reply from ' + email.from + ':\n' + email.body_text;
current.update();
For a reply, current is the matched record. For new email, the action fills fields on a new record and saves it.
Testing email
Check the email log (System Logs > Emails) for outbound messages and their state. Preview Notification on the notification record shows the rendered message for a chosen record without sending it.
For inbound actions, send a test message and open the received email record to see which action processed it. If no action matched, check the type, condition and target table.
How the exam asks about this
- "Email the requester whenever their request is approved." A notification on the table with Record updated and a condition, recipient from the Requested for field.
- "A script decides who gets the message." Fire an event and use Event parm 1 contains recipient.
- "Replies from users should add a comment to the request." An inbound email action of type Reply on that table.
What to remember for the exam
- Notifications have three parts: when to send, who will receive, what it will contain.
- Send when: record inserted or updated, or an event is fired.
${field}inserts values;${mail_script:name}runs an email script that usestemplate.print().- Inbound email is new, reply or forward; replies match records through the watermark.
- Inbound email action scripts use
currentandemail. - Preview Notification shows the rendered email without sending.
In an inbound email action, current is the target record and email is the message. Code that reads current.subject to get the email's subject is wrong; use email.subject.
Common mistakes
Hard coding recipients by name. People change jobs. Use fields on the record or groups, so the recipient follows the data.
Building a notification on the wrong table. The notification only sees fields of its own table and dot-walked references. Build it on the table that holds the record being reported.
Expecting replies to match without a watermark. If the user deletes the watermark, the email may be treated as new and creates a record instead of updating one.
Check yourself
When should a notification use Event is fired instead of Record inserted or updated?
When a script decides that the email is needed or who receives it; the script fires the event and passes values in parm1 and parm2.
How does an inbound reply find the record it belongs to?
Through the watermark that the outbound email carried, which the instance looks up to find the associated record.
How does an email script add text to the message body?
It calls template.print() with the text, and the notification includes the script with ${mail_script:script_name}.
Topic 4 of 4 in Application Automation
Test yourself on Application Automation
The full ServiceNow CAD question bank has 600 questions, with timed practice exams and an explanation for every answer.
This study guide is independent and is not affiliated with, endorsed by, or sponsored by ServiceNow. Vendor names, certification names and exam codes are trademarks of their respective owners and are used for identification only.