Uncle Rob
Kilo Patron

Hello friends!   A frequent request from stakeholders is physical attachments on outbound email.   Its most common with Approvals.   Change approvers are blind to info in attachments like non-tracked CIs, back out plans, deployment / testing instructions, etc.   Its even more frequent on custom applications.   Most commonly hyperlinks to attachments are placed in outbound messages, but this has a critical weakness: role requirements.   It's also a high-friction user experience as people are already instinctively used to file attachment on email.

The good news is, its a REALLY simple feature once you understand how GlideSysAttachment.copy works.   You may have used this to copy attachments between records before, but you may not have known that attachments made to sys_email records before they're send will physically attach them to the email.

GlideSysAttachment.copy Parameters

GlideSysAttachment.copy(source table, source_id, target table, target_id)

Source Table:   what table is the attachment associated with.   You need to be more careful here than usual because the implied "records are in the table they're extended from" doesn't apply here.   You can't say "task", for example, you must use the specific task table the record can be found in.

Source ID:   The sys_id of the record the attachment is associated with

Target Table: The table containing the record the attachments will be copied to.   Remember, the same specificity requirements as source_table.

Target ID:   The sys_id of the record the you copy attachments to.

BTW, Why do I need to know the table AND the sys_ID?
Attachments aren't stored ON the table you attach them to.   If you add a file to an incident, you're storing it in the sys_attachments table, and that holds references to the record you attached to.   To save itself from searching the entire platform for a single sys_id, the sys_attachment table first specifies the table the containing record resides (since attachments can be added to dang near everything).

GETTING FANCY WITH PARAMETERS FOR EMAIL ATTACHMENTS

In my case I wanted physical attachments for approvals.   So I had a couple issues:

STEP 1 - Intercepting the sys_email record before it sends.

Easy enough with a before-insert business rule:

find_real_file.png

- I had a further condition to ensure this only fired for Approvals

STEP 2 - Fancy parameters

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

//remember:   current is the email record being generated

var source = current.instance.document_id; //Instance is the reference to the record triggering email. document ID is the reference to what the approval record approves

var source_table = current.instance.source_table //ask what table the record triggering the email resides in

var target = current.sys_id; //attaching to this specific email record

GlideSysAttachment.copy(source_table, source, 'sys_email', target);

})(current, previous);

Et voila!   ALL approvals now carry the physical attachments of whatever record you're approving.

17 Comments