Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

How to add an attachment to an email vial script

Mark94
Kilo Expert

Hi,

I have a workflow, that having done bits and bobs creates a PDF file, and attaches it to the ticket (a purchase order ticket). The next step I have been asked to build is to attach that PDF to an email and send it externally.

I've tried one method which was detailed in another post -

Attempt 1 -

1. Go to System Notification > Email > Notification Email Scripts
2. Create email script called 'attachfile'
3. Add this line to the script -    
template.print('Attachment: <a href="sys_attachment.do?sys_id=b32dec79db2dd450c646f5461d9619d6">Attachment</a>');
4. Add the following line to the workflow's Notification step in the message -
${mail_script:attachfile};

Sadly, this only adds a link to the PDF, and doesn't attach it to the email (I hard coded the sys_id only as a test).

 

Attempt 2 -

I've started to create a script inside the workflow. This was again taken from another post.

var gr = new GlideRecord('sys_attachment');
  var table = '';
  gr.addQuery('table_sys_id',current.instance);
  gr.query();
  while(gr.next()) {
      table = gr.table_name;
  }
  if (table != '') {
      GlideSysAttachment.copy(table,current.instance,'sys_email',current.sys_id);
      gs.print('Creating attachment');
  }
 

I've now got a bit stuck, I'm not sure where to go from here. This in itself isn't doing anything, and I'm not sure if it's going to. Can anyone point me in the right direction. Am I on the right track?

Many thanks,

Mark.

1 ACCEPTED SOLUTION

Mark,

Excellent, yes please see the attached for 5.4 & 5.41 which shows the setup.

Please let me know once this is working, otherwise please feel free to show your setup and happy to compare.

 

Kind regards

Steve

View solution in original post

16 REPLIES 16

Hello Steve,

I got a problem on email table. When I try to search the sys_email table on the Table field, I can't find it. Is their any property that should be enable or desable .  

Chaitanya27
Tera Contributor

Great Work !! Kudos for your help ...

Anil Shewale
Mega Guru

Hi Mark

Useful attachment scripts

This is a searchable version of the Useful Attachment Scripts.

 

https://docs.servicenow.com/bundle/jakarta-servicenow-platform/page/script/useful-scripts/reference/...

If it help mark helpful or correct 

Thanks and regards

Anil

Hi Anil,

This looks like what I tried earlier, and it puts a link into the email, but not the attachment itself sadly.

Kind Regards,

Mark.

Gaurav Shirsat
Mega Sage

Hi Mark

actually you can  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.

You can attach documents and reports to email notifications by scripting or linking to the sys ID of the record.

You can include all attachments from the source record with the notification. For example, if an incident update generates a notification, you can include all attachments from the incident record with the notification. To include all attachments from the source record, select the check box for the Include attachments field. Note that email messages, including attachments, cannot exceed the maximum email size. This size includes MIME encoding. For details on MIME encoding, see Email service size restrictions.

https://docs.servicenow.com/bundle/orlando-servicenow-platform/page/administer/notification/concept/... or refer this blog:-
 
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}
 
 

Please Mark Correct and Helpful

Thanks and Regards

Gaurav Shirsat