Send an Email Notification with Associated Attachments

/, Scripting/Send an Email Notification with Associated Attachments

Send an Email Notification with Associated Attachments

I

t’s very common to send out an email notification at different stages of a task. There are great example notifications out-of-box in the Service-now platform that show you how to set these up. In some cases, you may wish to send more than the standard information in your email. The ‘mail_script’ tag can be used to do some more advanced script processing and then print out the results using the ‘template.print’ command. Here’s a sample script that you could use in an outbound email notification. The sample script is designed to send out information about a particular task. It also includes a ‘mail_script’ function to perform some advanced processing and include links to all of the associated attachments for the task. This script can be pasted directly into the ‘Message’ field on an email notification or email template record.

Short description: ${short_description}
</br>
Click here to view: ${URI}
<hr/>
<mail_script>
attachLinks();
function attachLinks() {
   //Check for any attachments and add attachment links if they exist
   var gr = new GlideRecord('sys_attachment');
   gr.addQuery('table_sys_id',current.sys_id);
   gr.query();
   if(gr.hasNext()){
      template.print('Attachments: \n');
         while (gr.next()) {
            var attachLink = '<a href="' + gs.getProperty("glide.servlet.uri") + gs.generateURL(gr.getTableName(),gr.sys_id) +  '">' + gr.file_name + '</a>';
            template.print(attachLink +  '\n');
         }
      template.print('<hr/>');
   }
}
</mail_script>
Comments:
${comments}
As of the June 2011 release of ServiceNow you now have the ability to actually attach a record’s attachments to an outgoing email simply by checking the ‘Include Attachments’ checkbox on the notification form. You may have to personalize the notification form to add this field.
By | 2018-07-09T15:00:11+00:00 March 12th, 2010|Categories: Email Notifications, Scripting|Tags: , |7 Comments

About the Author:

Mark has worked in the IT industry since 2002 and with ServiceNow since 2007. He is the founder and creator of SN | Guru and the co-founder of Crossfuze, one of the worlds leading ServiceNow consulting partners. Prior to co-founding Crossfuze, he worked for ServiceNow as a Senior Architect on the Professional Services team. He has personally led dozens of successful implementations encompassing every part of the ServiceNow platform. He is also responsible for designing and developing groundbreaking ServiceNow solutions and best practices in the form of various applications, turnkey solutions, and integrations during his tenure at ServiceNow, Crossfuze and, of course, SN | Guru. These solutions are used today by ServiceNow administrators and consultants alike in hundreds of ServiceNow instances around the world!

7 Comments

  1. Jim Coyne October 28, 2010 at 2:52 am - Reply

    I have this working for incident notifications, but for some reason the attachments will not appear in a change request notification. Added the code to the email template but it simply will not display the attachments.

    • Mark Stanger October 28, 2010 at 3:16 am - Reply

      Just tested this at https://demo.service-now.com and it seems to work fine there on the change request table. I’ve got a notification set up there called ‘Test change attachments email’ that you can use for comparison purposes. The other thing I would try would be to put some ‘gs.log’ statements in the script to see if it is being executed, ‘current.sys_id’ matches the sys_id of the change request, and if your GlideRecord query is returning any attachments.

      • Jim Coyne October 28, 2010 at 3:18 pm - Reply

        Of course – “current.sys_id” is not what I want, but the sys_id of the actual request.

        • Martijn March 14, 2013 at 3:41 am - Reply

          Did you update the url?
          gs.generateURL(gr.getTableName(),gr.sys_id):
          the “getTableName” may send you the wrong table, so the URL will not be determined properly (make sure it is “change_request ” and not “incident”).

          A question that I have: how do I really send out attachments? These are just links and the user receiving these emails still has to log into ServiceNow to read them. There are cases where you don’t want this.

          • Mark Stanger March 14, 2013 at 6:46 am - Reply

            Check out the green notice in the article above. You can define this with a per-notification checkbox if you want to include the actual attachments.

  2. Martijn March 14, 2013 at 8:25 am - Reply

    Yes, I found that. In Notifications you can select if you want attachments to be sent, too (“include attachments option”… who would guess ;-))
    However, another option I found is to copy the attachment to the sys_email record before it is sent and then the attachment will be added to the email, too.

  3. Scott July 3, 2013 at 9:50 pm - Reply

    sort along the same lines but…. is there a way to configure an email notification that has an attachment on it. when the email notification is triggered dependent on its conditions, it sends the email notification and the attachment to the recipient.
    storyline to this is, a record producer on the Service Cat, user fills out the form and submits it, this triggers an email notification (that has a built in attachment) to the “send to:” person or group.

    im not looking for the user to attach anything to the record producer at the time of filling it in and I’m not trying to send any attachment that may be on the “INC” or “REQ”, just want to configure an email notification that has a built in attachment and send that.

Leave A Comment