{"revision":"c10276e953456835cdbb1f3a60d58ae1980be353","document":{"id":39209,"path":"platform-administration/r_ExScptEmlNtfn.md","title":"Example scripting for email notifications","bundle":null,"doc_type":null,"release":"australia","body":"\n# Example scripting for email notifications\n\nExamples of scripting for email notifications.\n\n## Scripting examples for email notifications\n\nA simple text string is the most basic example of the way a mail script works. This script prints out \"Incident number - INC00001\".\n\n```\ntemplate.print(\"Incident number - \"+ current.number);\n```\n\nMore advanced scripts, like this one, can be found by browsing through the base system email templates.\n\n```\ntemplate.print(\"Summary of Requested items:<br />\");  \nvar now_GR = new GlideRecord(\"sc_req_item\");\nnow_GR.addQuery(\"request\", current.sysapproval);\nnow_GR.query();\nwhile(now_GR.next()) {\n  template.print(now_GR.number + \":  \" + now_GR.quantity + \" X \" + now_GR.cat_item.getDisplayValue() \n                   + \" at \" + now_GR.cat_item.price.getDisplayValue() + \" each <br />\");\n}\n```\n\nTo dynamically change field values within an email, use the following functions within &lt;mail\\_script&gt; syntax:\n\n```\n...\nemail.setFrom(current.caller_id.email);\nemail.setReplyTo(\"joe.employee@yourcompany.com\");\nemail.setSubject(\"This is the new subject line\");\nemail.setBody(\"This is the new body\");\n...\n```\n\nUsing the **instance\\_name** property ensures that the notification still works when migrated between instances.\n\n```\ndothis();\n \nfunction dothis(){\n    var now_GR =new GlideRecord('sys_attachment');\n    now_GR.addQuery('table_sys_id',current.sys_id);\n    now_GR.query();while(now_GR.next()){\n        template.print('Attachment: <a href=\"https://'+ gs.getProperty('instance_name')+'\n            .service-now.com/sys_attachment.do?sys_id='+ now_GR.sys_id+'\">'+ now_GR.file_name+'</a>');}}\n```\n\nYou can specify copied and blind copied recipients by using the email object within a mail script.\n\n```\n//email.addAddress(type, address, displayname);\n    email.addAddress(\"cc\",\"john.copy@example.com\",\"John Roberts\");\n    email.addAddress(\"bcc\",\"john.secret@example.com\",\"John Roberts\");\n```\n\nThe following is an example script to add users from **watch\\_list** as copied recipients.\n\n```\nif(!current.watch_list.nil()){\n   //get watch list addresses and add to cc\n   var watcherIds = current.watch_list.split(\",\");\n \n   //get user records\n   var user = new GlideRecord(\"sys_user\");\n   user.addQuery(\"sys_id\", watcherIds);\n   user.addQuery(\"notification\",2); \n   //email\n   user.addQuery(\"email\",\"!=\",\"\");\n   user.query();\n \n   while(user.next()){\n      //add to cc list    \n      email.addAddress(\"cc\", user.email, user.getDisplayValue());}}\n```\n\n**Parent Topic:**[Scripting for email notifications](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/australia/platform-administration/c_ScriptingForEmailNotifications.md)\n\n**Related topics**  \n\n\n[Include mail scripts in email notifications]()\n\n[Mail script variables]()\n\n[Useful attachment scripts]()\n\n[TemplatePrinter API](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/api-reference/server-api-reference/c_TemplatePrinterScopedAPI.md)\n\n[GlideEmailOutbound API](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/api-reference/server-api-reference/c_GlideEmailOutboundScopedAPI.md)\n\n"}}