Add attachment to Notification Email

sarahkapasi
Giga Expert

I've created a Service Catalog Request which will have an attachment included. Is there a way to include that attachment to Notification Email?

Thanks,

Sarah

14 REPLIES 14

Ivano B
ServiceNow Employee
ServiceNow Employee

Hi Sarah



If you like the first approach than yes.


You need to create an email script and then reference the email script using this syntax in your notification record



${mail_script:YOURSCRIPTNAME}



And in the email script table you can create an entry with the following code.



NAME:YOURSCRIPTNAME


SCRIPT:



var gr = new GlideRecord('sys_attachment');


gr.addQuery('table_sys_id',current.sys_id);


gr.query();


while (gr.next()) {


  template.print('Attachment: <a href="http://'+gs.getProperty("instance_name")+'.service-now.com/sys_attachment.do?sys_id='


    + gr.sys_id + '">' + gr.file_name + '</a>\n');


}




More info can be found here



Scripting for Email Notifications - ServiceNow Wiki





Thanks.



I was able to add the link to the notification, but when I click on the link to open it, it states "This page can't be displayed"


Ivano B
ServiceNow Employee
ServiceNow Employee

Hi Sarah



This seems strange can i suggest you to test if you can download an attachment from you instance



https://YOUR_INSTANCE_NAME.service-now.com/sys_attachment.do?sys_id=YOUR_FILE_SYS_ID



Let me know if this is works for you


Cheers



Robo


I actually changed the Notification Email Script to the following and it worked!



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("<br><br>Attachments:</br> ");


          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 +   "   ");


          }


    }


}


Ivano B
ServiceNow Employee
ServiceNow Employee

Hi Sarah



In theory is exactly the same thing. I mean the final result.


At least the initial suggestion was the right thing



Cheers


Robo