<h2>Example scripting for email notifications</h2><br/><div style="overflow-x:auto"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"><head><meta content="text/html; charset=UTF-8" /><meta name="copyright" content="(C) Copyright 2025" /><meta name="DC.rights.owner" content="(C) Copyright 2025" /><meta name="generator" content="DITA-OT" /><meta name="DC.type" content="reference" /><meta name="DC.title" content="Example scripting for email notifications" /><meta name="abstract" content="Examples of scripting for email notifications." /><meta name="description" content="Examples of scripting for email notifications." /><meta name="DC.subject" content="Server Scripting, email notifications, example scripts" /><meta name="keywords" content="Server Scripting, email notifications, example scripts" /><meta name="DC.relation" scheme="URI" content="../../../script/server-scripting/concept/c_ScriptingForEmailNotifications.html" /><meta name="DC.relation" scheme="URI" content="../../../administer/general/concept/intro-now-platform-landing.html" /><meta name="DC.relation" scheme="URI" content="../../../administer/general/concept/config-now-platform-core-features.html" /><meta name="DC.relation" scheme="URI" content="../../../administer/notification/reference/notifications.html" /><meta name="DC.relation" scheme="URI" content="../../../administer/notification/concept/system-notifications-landing.html" /><meta name="DC.relation" scheme="URI" content="../../../administer/notification/concept/c_EmailNotifications.html" /><meta name="DC.relation" scheme="URI" content="../../../administer/notification/task/t_CreateANotification.html" /><meta name="DC.relation" scheme="URI" content="../../../script/server-scripting/concept/c_UseJavaScriptInEmails.html" /><meta name="DC.relation" scheme="URI" content="../../../script/server-scripting/reference/r_MailScriptAPI.html" /><meta name="DC.relation" scheme="URI" content="../../../script/useful-scripts/reference/r_UsefulAttachmentScripts.html" /><meta name="DC.relation" scheme="URI" content="../app-store/dev_portal/API_reference/TemplatePrinterScoped/concept/c_TemplatePrinterScopedAPI.dita/c_TemplatePrinterScopedAPI.html" /><meta name="DC.relation" scheme="URI" content="../app-store/dev_portal/API_reference/GlideEmailOutboundScoped/concept/c_GlideEmailOutboundScopedAPI.dita/c_GlideEmailOutboundScopedAPI.html" /><meta name="DC.creator" content="ServiceNow" /><meta name="DC.date.created" content="2023-08-03" /><meta name="DC.date.modified" content="2024-02-01" /><meta name="DC.format" content="XHTML" /><meta name="DC.identifier" content="r_ExScptEmlNtfn" /><link rel="stylesheet" type="text/css" href="../../../CSS/commonltr.css" /><title>Example scripting for email notifications</title></head><body id="r_ExScptEmlNtfn"> <div class="breadcrumb"><a class="link" href="../../../administer/general/concept/intro-now-platform-landing.html" title="As a platform administrator, you have the power of the Now Platform at your fingertips. The Now Platform is an application platform as a service that automates business processes across the enterprise.">Administer the Now Platform</a> > <a class="link" href="../../../administer/general/concept/config-now-platform-core-features.html" title="The Now Platform provides for a multitude of customization options to your applications. Customize your UI, handle user and data administration, and localize your instance for time zones, currencies, and more.">Configure Now Platform Core Features</a> > <a class="link" href="../../../administer/notification/reference/notifications.html" title="Use ServiceNow Notifications to manage system email, create system notifications, and configure how your system responds to inbound email.">Notifications</a> > <a class="link" href="../../../administer/notification/concept/system-notifications-landing.html" title="Set up notifications to alert users of record changes that may concern them. You can notify users via email, SMS text message, push notification, or messaging app.">System notifications</a> > <a class="link" href="../../../administer/notification/concept/c_EmailNotifications.html" title="Use email notifications to send selected users email or SMS notifications about specific activities in the system, such as updates to incidents or change requests.">Email and SMS notifications</a> > <a class="link" href="../../../administer/notification/task/t_CreateANotification.html" title="Create an email notification specifying when to send it, who receives it, what it contains, and if it can be delivered in an email digest.">Create an email notification</a> > </div> <h1 class="title topictitle1" id="ariaid-title1">Example scripting for email notifications</h1> <div class="body refbody"><p class="shortdesc">Examples of scripting for email notifications.</p> <div class="section"><h2 class="title sectiontitle">Scripting examples for email notifications</h2> <div class="p">A simple text string is the most basic example of the way a mail script works. This script prints out "Incident number - INC00001". <pre class="pre codeblock"><code>template.print("Incident number - "+ current.number);</code></pre></div> <div class="p">More advanced scripts, like this one, can be found by browsing through the base system email templates. <pre class="pre codeblock"><code>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 />"); }</code></pre></div> <div class="p">To dynamically change field values within an email, use the following functions within <mail_script> syntax: <pre class="pre codeblock"><code>... 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"); ...</code></pre></div> <div class="p">Using the <span class="keyword parmname">instance_name</span> property ensures that the notification still works when migrated between instances. <pre class="pre codeblock"><code>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>');}}</code></pre></div> <div class="p">You can specify copied and blind copied recipients by using the email object within a mail script. <pre class="pre codeblock"><code>//email.addAddress(type, address, displayname); email.addAddress("cc","john.copy@example.com","John Roberts"); email.addAddress("bcc","john.secret@example.com","John Roberts");</code></pre></div> <div class="p">The following is an example script to add users from <span class="keyword parmname">watch_list</span> as copied recipients.<pre class="pre codeblock"><code>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());}}</code></pre></div> </div> </div> <div class="related-links"> <div class="familylinks"> <div class="parentlink"><strong>Parent Topic:</strong> <a class="link" href="../../../script/server-scripting/concept/c_ScriptingForEmailNotifications.html" title="Email scripts allow for business rule-like scripting within an outbound email message.">Scripting for email notifications</a></div> </div> <div class="linklist relinfo relconcepts"><strong>Related concepts</strong><br /> <ul class="linklist"><li class="linklist"><a class="link" href="../../../script/server-scripting/concept/c_UseJavaScriptInEmails.html" title="Create mail scripts in System Notifications > Email > Notification Email Script , and refer to them by using ${mail_script:script name} in the script field.">JavaScript in emails</a></li></ul></div> <div class="linklist relinfo relref"><strong>Related reference</strong><br /> <ul class="linklist"><li class="linklist"><a class="link" href="../../../script/server-scripting/reference/r_MailScriptAPI.html" title="Certain variables are available when processing mail_script scripts.">Mail script API</a></li><li class="linklist"><a class="link" href="../../../script/useful-scripts/reference/r_UsefulAttachmentScripts.html" title="This is a searchable version of the Useful Attachment Scripts.">Useful attachment scripts</a></li></ul></div> <div class="linklist relinfo"><strong>Related topics</strong><br /> <ul class="linklist"><li class="linklist"><a class="link" href="../app-store/dev_portal/API_reference/TemplatePrinterScoped/concept/c_TemplatePrinterScopedAPI.dita/c_TemplatePrinterScopedAPI.html" target="_blank" rel="noopener noreferrer">TemplatePrinter API</a></li><li class="linklist"><a class="link" href="../app-store/dev_portal/API_reference/GlideEmailOutboundScoped/concept/c_GlideEmailOutboundScopedAPI.dita/c_GlideEmailOutboundScopedAPI.html" target="_blank" rel="noopener noreferrer">GlideEmailOutbound API</a></li></ul></div> </div> </body></html></div>