Help with a business rule script on the sys_email table for client scripts to indicate attachments

jlaue
Kilo Sage

Hello -  I am working on a way to identify which attachment(s), if any were included on an outbound email utilizing the Email Client functionality in a scoped application.  I searched around on community and found a script that is exactly what I am looking for and it is working:

https://www.servicenow.com/community/developer-forum/show-the-name-of-the-attachment-on-activity-log...

 

My problem is that when there are not any attachments included, it is still adding the "Attachment(s) included in this email:", which I understand why because the variable is declared at the top and is included in the 'body' update regardless, as it is outside of the query loop.  I am wondering how I can just have the 'attachment' variable only utilized if there is actually an attachment.  I am sure I am missing something easy, looking for any assistance, thanks in advance!!  

 

(function executeRule(current, previous /*null when async*/ ) {
//This script will include the attachment names on the email that was sent via the email client
 
    var attachment = "Attachment(s) included in this email:";
    var body_text = current.body;
    var gr = new GlideRecord("sys_attachment");
    var count = 0;
    gr.addQuery("table_sys_id", current.sys_id);
    gr.query();
    while (gr.next()) {
        if (gr.hasNext()) {
            attachment = attachment + "\n" + gr.file_name + " , ";
        } else {
            attachment = attachment + "\n" + gr.file_name;
        }
    }
 
body_text = body_text + attachment;
current.body = body_text;
current.update();
 
})(current, previous);
0 REPLIES 0