TUESDAY, FEBRUARY 07, 2012

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}

Comments

Posted On
Oct 28, 2010
Posted By
Jim Coyne

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.

Posted On
Oct 28, 2010
Posted By
Mark Stanger

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.

Posted On
Oct 28, 2010
Posted By
Jim Coyne

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

Leave a Reply


Notify me of followup comments via e-mail. You can also subscribe without commenting.

Latest Comments

  • Mark Stanger: This linkage all happens for you if you use the task survey plugin. You can look on the wiki for more...
  • Vineeth: I want a way in which if a survey is filled in by the user the response are stored in the survey response...
  • Mark Stanger: This functionality doesn’t connect to an FTP server. See this line in the post above…...
  • Mark Stanger: The report page is back-end XML so there’s no way to directly manipulate the behavior of that...