logo

NJP

Adding latest attachment from record to the outbound email

New article articles in ServiceNow Community · Jan 09, 2025 · article

Often, we need to send only the latest attachment via email. However, using the "Include attachment" checkbox in notifications sends all attachments, making it impossible to send just the latest or a specific attachment.

To address this, follow these steps:

1) Create business rule which is After Insert on sys_email with correct condition for table name

AnkurBawiskar_0-1736429521127.png

2) Use the below script to pick latest attachment

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

    var s_Attach = new GlideRecord('sys_attachment');
    s_Attach.addEncodedQuery('table_sys_id=' + current.instance);
    s_Attach.orderByDesc('sys_created_on');
    s_Attach.setLimit(1);
    s_Attach.query();
    if(s_Attach.next()){
        var rec = new GlideRecord('sys_email_attachment');
        rec.initialize();
        rec.attachment = s_Attach.sys_id;
        rec.file_name = s_Attach.file_name;
        rec.source = 'notification';
        rec.content_disposition = 'attachment';
        rec.email = current.sys_id;
        rec.insert();
    }

})(current, previous);
View original source

https://www.servicenow.com/community/service-operations-workspace/adding-latest-attachment-from-record-to-the-outbound-email/ta-p/3145303