How to include only newly added attachment on mail and remove other attachments?

sruthig
Tera Expert

Hi All,

We have configured like when any additional comments added to the case it will sent mails to users in watch list. If any work notes added to the case it will sent mails to users in the work notes list. So in the notification selected "include attachment option".

All the attachment is attaching every time when any mail sent for additional comments or work notes added. How to include only latest updated attachments in the mail?

 

Please suggest

Regards,

Sruthi

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

@sruthig 

you cannot include single attachment using the Include Attachment checkbox

you will have to uncheck that and include the attachment link in the email

Use email script to get the latest attachment link and use that email script in the notification

Email Script:

Name - show_specific_attachment

(function runMailScript(current, template, email, email_action, event) {

    // Add your code here
   
    var gr = new GlideRecord('sys_attachment');
    gr.orderByDesc('sys_creatd_on'); //sort with created descending
    gr.addQuery('table_sys_id', current.sys_id);
    gr.setLimit(1); // pick only 1 attachment record
    gr.query();
    if (gr.hasNext()) {
        template.print("Attachment: <br />");
        while (gr.next()) {
            var url = gr.getTableName() + ".do?sys_id=" + gr.getValue('sys_id');
            var attachLink = '<a href="' + url +  '">' + gr.file_name + '</a>';
            template.print(attachLink +  "<br />");
        }
        template.print("<hr/>");
    }

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

In notification include the email script as below

${mail_script:show_specific_attachment}

Regards
Ankur

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

View solution in original post

15 REPLIES 15

Willem
Giga Sage
Giga Sage

You can use this as guidance:

 https://community.servicenow.com/community?id=community_article&sys_id=15fc554edba4d010414eeeb5ca961...

The script would look something like this:

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

    // Add your code here
    var lookforatt = new GlideRecord('sys_attachment');
    lookforatt.addQuery('table_sys_id', current.instance);
    lookforatt.addEncodedQuery('sys_created_onONToday@javascript:gs.beginningOfToday()@javascript:gs.endOfToday()')
    lookforatt.query();
    while (lookforatt.next()) {
        GlideSysAttachment.copy('incident', current.instance, 'sys_email', current.sys_id);
    }
})(current, previous);

 

The added encoded query will cause only attachment added today to be included. But you can change the query of course.

Hi Willem,

Thanks for the response.

this is after update business rule on incident table right?

Here how it will check a single incident record?

Attachment will be different for different incident record.

How here the above condition will check?

Then from which table we can encoded query condition can take?

Please suggest

Regards,

Sruthi

MrMuhammad
Giga Sage

@sruthig,

The code provided by Willem above will work only if you are sending email using email client but by looking at your requirements it seems like you are using regular notification. If yes, then you cannot send selective attachments in the email.

You can achieve this by adding a link to the selective attachments in the email instead of physically attaching the attachments. 

Please Refer below

https://docs.servicenow.com/bundle/orlando-servicenow-platform/page/administer/notification/concept/...

https://www.servicenowguru.com/scripting/send-email-notification-attachments/

I hope above docs will direct you in the right direction. Hope that helps!

Regards,

Sharjeel

 

Regards,
Muhammad

Ankur Bawiskar
Tera Patron
Tera Patron

@sruthig 

you cannot include single attachment using the Include Attachment checkbox

you will have to uncheck that and include the attachment link in the email

Use email script to get the latest attachment link and use that email script in the notification

Email Script:

Name - show_specific_attachment

(function runMailScript(current, template, email, email_action, event) {

    // Add your code here
   
    var gr = new GlideRecord('sys_attachment');
    gr.orderByDesc('sys_creatd_on'); //sort with created descending
    gr.addQuery('table_sys_id', current.sys_id);
    gr.setLimit(1); // pick only 1 attachment record
    gr.query();
    if (gr.hasNext()) {
        template.print("Attachment: <br />");
        while (gr.next()) {
            var url = gr.getTableName() + ".do?sys_id=" + gr.getValue('sys_id');
            var attachLink = '<a href="' + url +  '">' + gr.file_name + '</a>';
            template.print(attachLink +  "<br />");
        }
        template.print("<hr/>");
    }

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

In notification include the email script as below

${mail_script:show_specific_attachment}

Regards
Ankur

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader