…thoughts on ServiceNow and digital transformation

Post

Adding the Same Attachment to a Notification Every Time it Sends


If you ever wanted to send the same attachment out in a notification, you found out that there wasn’t an easy way to do it. You can send attachments from the notifications record (incident, case, or whatever table the notification is for), however you can’t send the same attachment every time. Attaching a document to the notification record itself doesn’t send it with the email. Ugh.

This solution outlines the steps to send a “generic” attachment with a notification by attaching to the notification record. For example, if you have a pdf that you want to send out as an attachment to an incident notification. The pdf is the same each time and you don’t want to attach it to every incident. Here are the steps to implement this:

1. Create a true false field on the Notification table (sysevent_email_action) called Include attachments from this notification record. I put it in the Advanced View under Include attachments.

2. Create an Business Rule on the sys_email_log table:

  • When: After
  • Insert
  • Condition: Notification-> Include attachments from this notification record = True

Script:

(function executeRule(current, previous /*null when async*/) { 
//get the attachments from the Notification record and add them to the Email Attachments 
var att = new GlideSysAttachment(); 
var attGR = att.getAttachments('sysevent_email_action', current.notification.sys_id); 
while (attGR.next()) { 
    var grSysEmailAttachmentNew = new GlideRecord('sys_email_attachment'); 
    grSysEmailAttachmentNew.initialize(); 
    grSysEmailAttachmentNew.setValue('attachment', attGR.getUniqueValue());
    grSysEmailAttachmentNew.setValue('email', current.email.sys_id); 
    grSysEmailAttachmentNew.setValue('source', 'notification'); 
    grSysEmailAttachmentNew.setValue('content_disposition', 'attachment'); 
    grSysEmailAttachmentNew.setValue('file_name', attGR.file_name); 
    grSysEmailAttachmentNew.insert(); 
} 
})(current, previous); 

4. Create a Notification and check the box Include attachments from this notification record. Attach a one or more documents to the notification record. Each time the notification sends, the attached documents will be included.