--- 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-03-12" 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:
"); 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
"); } ``` 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: '+ now_GR.file_name+'');}} ``` 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** [Include mail scripts in email notifications]() [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)