Need to add two specific attachments to a notification

Moedeb
Tera Guru

I have a notification that gets sent as part of a custom flow and is triggered when a particular event occurs - this is working perfectly.

 

The issue I have is, when that notification sends, I want it to include 2 particular attachments every time it sends.

I have tried the "Include attachments" checkbox in the notification, but that I believe is meant to include any attachments within the triggering record - in this case it would be a RITM, that actually has no attachments, therefore no attachments are added to the notification.

 

I have saved the attachments as part of a knowledge document and their sys_id's are:

Document 1 - 28a3323f11f235103b97b89ea08322d7

Document 2 - 2ca3323f11f235103b97b89ea08322d9

 

Knowledge articles Sys_id - f39272b3117635103b97b89ea083228b

 

The notification has a subject starting with - Security Requirements

 

So what I have tried is to create an advances business rule:

Name: Add attachment

Table: email [sys_email]

When to run = 

when: before 

order: 100

Insert = true (all else false)

 

Conditions:

Subject starts with Security Requirements

 

Advanced Tab:

(function executeRule(current, previous /*null when async*/) {

	var sourceID = 'f39272b3117635103b97b89ea083228b';
	var sourceTable = 'kb_knowledge';
	var document1ID = '28a3323f11f235103b97b89ea08322d7';
	var document2ID = '2ca3323f11f235103b97b89ea08322d9';

	GlideSysAttachment.copy(sourceTable, sourceID, 'sys_email', document1ID);
	GlideSysAttachment.copy(sourceTable, sourceID, 'sys_email', document2ID);

})(current, previous);

 

I also added an action to add a message when it runs.

 

The issue I'm having is not only does it still send the notification without attachments, I'm also not getting any message showing that the business rule is even running.

 

Anyone know the right way to do this?

 

I've looked at a few posts here and there and none of them are really helping me, so please don't reply with just a link to another article.

1 ACCEPTED SOLUTION

I know you didn't want links, but this one seems to be directly addressing what you are trying to do;

 

https://www.servicenow.com/community/developer-articles/adding-attachments-to-email/ta-p/2320991

View solution in original post

6 REPLIES 6

Danish Bhairag2
Tera Sage
Tera Sage

@Danish Bhairag2 I thank you for an attempt to help, but as I said in my question:

"I've looked at a few posts here and there and none of them are really helping me, so please don't reply with just a link to another article."

 

The article you sent me has also not worked, I see a log entry at least, but it still doesn't actually attach anything.

Kai Tingey
Tera Guru

Hi There

 

You could use a mail script to generate an attachment link.

 

We have a mail script that does this, to attach any documents from an RITM to the approval email.

Our mail script looks like this:

 

 

(function runMailScript(/* GlideRecord */ current, /* TemplatePrinter */ template,
          /* Optional EmailOutbound */ email, /* Optional GlideRecord */ email_action,
          /* Optional GlideRecord */ event) {
	
var gr = new GlideRecord('sys_attachment');
gr.addQuery('table_sys_id', current.sysapproval.sys_id);  
gr.query();
while (gr.next()) {
              var attachLink = '<a href="' + gs.generateURL(gr.getTableName(),gr.sys_id) +   '">' + gr.file_name + '</a>';
              template.print(attachLink +   "<br />");
}

})(current, template, email, email_action, event);

 

 

 and then the notification email just contains the mail script where we want the link. if they click it - it just downloads the document.

 

you should be able to modify the above pretty easily to do what you want

 

another option might be to copy the attachments to your RITM before it generates the notification event, and then just include attachments the traditional way - but it's probably not good practice.

@Kai Tingey thanks, unfortunately this notification is going outside the organisation, so we need it to be an actual attachment, not a link because the recipients will not have access via a link