logo

NJP

Email attachments with scripting from a Workflow

Import · Oct 22, 2018 · article

Scenario: A workflow email notification needs to include a PDF document.

  1. I've read several times that the best way to do this is via email notification by "Create Event" Activity, instead of the "Notification" Activity.
  2. Go to the Catalog Item that runs the Workflow and attach the PDF file to it.
  3. Register the new Event.
  4. Create an Email Notification:
  5. 1. that will "Send when" [Event is fired]
    1. "Event parm 1 contains recipient" is checked.
    2. "Message HTML" has a mail script in it like ${mail_script:attach_file}
  6. Create a Notification Email Script that includes the following code:
    // attach file var gr_sys_att = new GlideRecord('sys_attachment'); gr_sys_att.addQuery('file_name',"File_Name.pdf"); gr_sys_att.addQuery('table_name',"sc_cat_item"); gr_sys_att.addQuery('table_sys_id',"b7cffa9d4f58db8031a28b8d0210c75b"); // sys_id of the Catalog Item that the file was attached to earlier. gr_sys_att.query(); while (gr_sys_att.next()) { var content = new GlideSysAttachment().getContentStream(gr_sys_att.sys_id); new GlideSysAttachment().writeContentStream(current, gr_sys_att.getValue('file_name'), gr_sys_att.getValue('content_type'), content); }
  7. Note: I found through trial & error that this code block had to be before the last template.print() statement. If its not then the code never ran.

Dog Tax: This is Radley.

image

image

View original source

https://www.servicenow.com/community/now-platform-articles/email-attachments-with-scripting-from-a-workflow/ta-p/2297237