Help
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Jim Coyne
Kilo Patron
Part of the Tips 'N Tricks" series.

 

Ever need to attach a particular "shared" or "common" document to a Notification for your clients?  A document that is the same for everyone?  Like a "Terms of Service" document, some instructions document or something similar?  That's easy with the "Include attachments" field on the Notification record, but what if you don't want to rely on it being added on the parent record?

 

I wrote a similar post some 6 years ago, Sending Documents in an Email Notification, but wanted to write an updated solution.  The "problem" with the previous article is the context record for the Notification is the KB article and NOT the particular record we are really dealing with.  So it will not show up in the Activity formatter when we add the "Sent/Received Emails" activity.

 

ServiceNow acts a little differently now.  Before, the attachments would be copied to the Email record and the "Include attachments" option would then send those attachments out in the actual email to the users.  That was creating a ton of duplicate attachments.  Now, there is an "Email Attachments" table that contains links to the attachments that should go out with the email, cutting down on the number of Attachment records.  So all we need to do is leverage it and make sure there's an entry in there for the emails we want to send out with the shared documents.

 

There are of course a whole bunch of ways to do things in ServiceNow and this is what I've come up with:

Step 1 - Create a new System Property that contains the sys_id(s) of the Attachment(s) you want to send out and attach the documents to that record.

 

find_real_file.png

 

The Name of the property can be anything of course, but should be something that makes sense.  Notice that I have 2 attachments on the property itself and 2 sys_ids, separated by a comma, in the Value field.  These are the sys_ids of the actual attachments on that record.  And having the documents on the System Property keeps things together.

 

Step 2 - Create a Business Rule that "attaches" the documents to the Email record.

Name:       Custom - Add Attachment for Notification
Table:      Email [sys_email]
Advanced:   checked
When:       before
Insert:     checked
Condition:  Subject is Attachments for You (whatever your Subject is)
Script:

 

 

(function executeRule(current, previous /*null when async*/ ) {
	var sysId = gs.getProperty("u.fpc.email.attachment.sys_id").split(",");
	var number = sysId.length;

	if (number > 0) {
		for (i = 0; i < number; i++) {
			//add a new Email Attachment record, 1 for each entry in the System Properties
			var attachment = new GlideRecord("sys_email_attachment");
			attachment.newRecord();
			attachment.attachment = sysId[i];
			attachment.file_name = attachment.attachment.file_name.toString();
			attachment.source = "notification";
			attachment.email = current.getValue("sys_id");
			attachment.source = "notification";
			attachment.content_disposition = "attachment";
			attachment.insert();
		}
	}

})(current, previous);

 

 

The Business Rule loops through the array of sys_id(s) and inserts a new record in the Email Attachment table for each of the attachments.

 

Step 3 - Create a Notification record to send the email with attachments.

Set up the Notification record to fire on whatever condition you need (insert/update or an Event) and contains whatever text you need.  Make sure the Subject matches what is in the Business Rule.

 

Here is where you need to make a decision - do you want the attachments that might have been manually added to the record to be included in your email?  If yes, then make sure the "Include attachments" field is checked.  If no, then make sure it is NOT checked.  That field being checked is what triggers the Email Attachment records to be created in the first place.  So if you want your email to only include your "shared" documents, leave the field unchecked as the Business Rule takes care of creating the records for us.

 

In my PDI, I have a Notification setup on the Incident table with the Subject of "Attachments for You".  The Notification fires when a new record is created and is sent to the Caller.  So with all of the above setup, this is what I get when I create a new Incident:

 

Email with the attachments:

find_real_file.png

 

And a record of the email being sent shows up in the Activity formatter on the Incident record:

 

find_real_file.png

 

I think this solution is pretty simple and gives you some flexibility in what to include/exclude in the email.  It could be expanded to add a work note detailing which attachments were sent as that is not actually shown in the email details. 

 

The Business Rule Script for that could look like this:

 

 

(function executeRule(current, previous /*null when async*/ ) {
	var sysId = gs.getProperty("u.fpc.email.attachment.sys_id").split(",");
	var number = sysId.length;
	
	//get the parent Incident record
	var incident = new GlideRecord("incident");
	if (incident.get(current.getValue("instance"))) {
		//and add a work note listing the files that were sent
		if (number > 0) {
			var files = [];
			for (i = 0; i < number; i++) {
				//add a new Email Attachment record, 1 for each entry in the System Properties
				var attachment = new GlideRecord("sys_email_attachment");
				attachment.newRecord();
				attachment.attachment = sysId[i];
				attachment.file_name = attachment.attachment.file_name.toString();
				attachment.source = "notification";
				attachment.email = current.getValue("sys_id");
				attachment.source = "notification";
				attachment.content_disposition = "attachment";
				attachment.insert();
				
				files.push(attachment.attachment.file_name.toString());
				
			}
			incident.work_notes = "The following files were sent to the user: " + files.join(", ");
			incident.update();
		}		
		
		
	}

})(current, previous);

 

 

And the Activity formatter would look like this:

 

find_real_file.png

 

As always, try it out in your company's development instance first, or better yet, your own Personal Development Instance.

1 Comment