Example scripting for email notifications
reference australia Updated Sep 10, 2026 1 min read r_ExScptEmlNtfn.md
Examples of scripting for email notifications.
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".
JavaScript
template.print("Incident number - "+ current.number);More advanced scripts, like this one, can be found by browsing through the base system email templates.
JavaScript
template.print("Summary of Requested items:<br />"); var now_GR = new GlideRecord("sc_req_item");now_GR.addQuery("request", current.sysapproval);now_GR.query();while(now_GR.next()) { template.print(now_GR.number + ": " + now_GR.quantity + " X " + now_GR.cat_item.getDisplayValue() + " at " + now_GR.cat_item.price.getDisplayValue() + " each <br />");}To dynamically change field values within an email, use the following functions within <mail_script> syntax:
JavaScript
...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");...Using the instance_name property ensures that the notification still works when migrated between instances.
JavaScript
dothis(); function dothis(){ var now_GR =new GlideRecord('sys_attachment'); now_GR.addQuery('table_sys_id',current.sys_id); now_GR.query();while(now_GR.next()){ template.print('Attachment: <a href="https://'+ gs.getProperty('instance_name')+' .service-now.com/sys_attachment.do?sys_id='+ now_GR.sys_id+'">'+ now_GR.file_name+'</a>');}}You can specify copied and blind copied recipients by using the email object within a mail script.
JavaScript
//email.addAddress(type, address, displayname); email.addAddress("cc","john.copy@example.com","John Roberts"); email.addAddress("bcc","john.secret@example.com","John Roberts");The following is an example script to add users from watch_list as copied recipients.
JavaScript
if(!current.watch_list.nil()){ //get watch list addresses and add to cc var watcherIds = current.watch_list.split(","); //get user records var user = new GlideRecord("sys_user"); user.addQuery("sys_id", watcherIds); user.addQuery("notification",2); //email user.addQuery("email","!=",""); user.query(); while(user.next()){ //add to cc list email.addAddress("cc", user.email, user.getDisplayValue());}}Parent Topic:Scripting for email notifications
Related topics
105 lines · 3.6 KB
---
title: Example scripting for email notifications
description: Examples of scripting for email notifications.
locale: en-US
canonical_url: https://www.servicenow.com/docs/r/australia/platform-administration/r\_ExScptEmlNtfn.html
release: australia
topic_type: reference
last_updated: "2026-09-10"
reading_time_minutes: 1
breadcrumb: [Scripting for email notifications, Create an email notification, Email and SMS notifications, System notifications, Notifications, Configure core features, Administer the ServiceNow AI Platform]
---
# Example scripting for email notifications
Examples of scripting for email notifications.
## 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 scripts, like this one, can be found by browsing through the base system email templates.
```
template.print("Summary of Requested items:<br />");
var now_GR = new GlideRecord("sc_req_item");
now_GR.addQuery("request", current.sysapproval);
now_GR.query();
while(now_GR.next()) {
template.print(now_GR.number + ": " + now_GR.quantity + " X " + now_GR.cat_item.getDisplayValue()
+ " at " + now_GR.cat_item.price.getDisplayValue() + " each <br />");
}
```
To dynamically change field values within an email, use the following functions within <mail\_script> syntax:
```
...
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");
...
```
Using the **instance\_name** property ensures that the notification still works when migrated between instances.
```
dothis();
function dothis(){
var now_GR =new GlideRecord('sys_attachment');
now_GR.addQuery('table_sys_id',current.sys_id);
now_GR.query();while(now_GR.next()){
template.print('Attachment: <a href="https://'+ gs.getProperty('instance_name')+'
.service-now.com/sys_attachment.do?sys_id='+ now_GR.sys_id+'">'+ now_GR.file_name+'</a>');}}
```
You can specify copied and blind copied recipients by using the email object within a mail script.
```
//email.addAddress(type, address, displayname);
email.addAddress("cc","john.copy@example.com","John Roberts");
email.addAddress("bcc","john.secret@example.com","John Roberts");
```
The following is an example script to add users from **watch\_list** as copied recipients.
```
if(!current.watch_list.nil()){
//get watch list addresses and add to cc
var watcherIds = current.watch_list.split(",");
//get user records
var user = new GlideRecord("sys_user");
user.addQuery("sys_id", watcherIds);
user.addQuery("notification",2);
//email
user.addQuery("email","!=","");
user.query();
while(user.next()){
//add to cc list
email.addAddress("cc", user.email, user.getDisplayValue());}}
```
**Parent Topic:**[Scripting for email notifications](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/australia/platform-administration/c_ScriptingForEmailNotifications.md)
**Related topics**
[JavaScript in emails]()
[Mail script variables]()
[Useful attachment scripts]()
[TemplatePrinter API](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/api-reference/server-api-reference/c_TemplatePrinterScopedAPI.md)
[GlideEmailOutbound API](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/api-reference/server-api-reference/c_GlideEmailOutboundScopedAPI.md)
| title | Example scripting for email notifications |
| description | Examples of scripting for email notifications. |
| locale | en-US |
| canonical_url | https://www.servicenow.com/docs/r/australia/platform-administration/r\_ExScptEmlNtfn.html |
| release | australia |
| topic_type | reference |
| last_updated | 2026-09-10 |
| reading_time_minutes | 1 |
| breadcrumb | Scripting for email notificationsCreate an email notificationEmail and SMS notificationsSystem notificationsNotificationsConfigure core featuresAdminister the ServiceNow AI Platform |