Sending an email notification when an attachment is added to an Incident record

amacqueen
Mega Guru

Thanks to @ccajohnson and his advice here https://community.servicenow.com/community?id=community_question&sys_id=bb8b0b61db9cdbc01dcaf3231f96191e I have been able to cretae the event, business rule and notification for this. However there are two other things I would like to achieve. Firstly when I look at the email log for the generated email I see the following - 'Attachment Email.png has been added to Incident INC427265' however when I preview the notification I see 'Attachment 0e606f7adb0f30505384b5f6f3961985 has been added to Incident INC427265' so it looks like the SID of the attachment from the attachment table is being added and ideally I would like the name of the attachment, in this case Email.png.

Secondly and more importantly I would like the outgoing email generated from the notification to include the attachment in this case Email.png.

As I'm not much of a coder I would greatly appreciate any help.

Any questions please ask.

Thanks

 

8 REPLIES 8

can you share the screenshot of the email?

Thanks

shloke04
Kilo Patron

Hi @amacqueen 

Please refer the approach mentioned by Goran using Flow designer on the same topic which will include the attachment and will not require code here:

https://www.youtube.com/watch?v=b_l5pOHKmVY

This should work for you.

Hope this helps. Please mark the answer as correct/helpful based on impact.

Regards,
Shloke

Hope this helps. Please mark the answer as correct/helpful based on impact.

Regards,
Shloke

shloke04
Kilo Patron

Hi @amacqueen 

Also would like to add, after reviewing your existing set up, you need to make the below changes:

1) Create a After Insert Business Rule on Attachment Table and use the script below:

BR Details:

Table: Attachment(sys_attachment)

When: After Insert

Condition: Table Name is incident

Script:

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

	// Add your code here
	gs.eventQueue('Event Name',current,current.file_name,current.sys_id);

})(current, previous);

 

Screenshot of BR for reference:

find_real_file.png

Now in your Notification, please make sure to check the below check boxes under whom will receive tab as shown below:

1) Parm 1

2) parm 2

3) Send to Event Creator

Include Attachment checkbox under 3rd tab of Notification form:

find_real_file.png

find_real_file.png

 

Now Navigate to the module "Notification Email Script" as shown below:

find_real_file.png

 

Click on New and use the script below:

(function runMailScript( /* GlideRecord */ current, /* TemplatePrinter */ template,
    /* Optional EmailOutbound */
    email, /* Optional GlideRecord */ email_action,
    /* Optional GlideRecord */
    event) {

    // Add your code here
   
    var myStringArray = event.parm2.toString();
  
    var sys = current.sys_id;

    //This queries the sys_attachment table for all attachments where the table_sys_id is the sys_id you used above.
    var grSysAtt = new GlideRecord('sys_attachment');
    grSysAtt.addQuery('table_sys_id', sys);
    grSysAtt.query();
    while (grSysAtt.next()) {
        var asys = grSysAtt.sys_id;

        //This if statement checks the attachments that you used in myStringArray and makes sure that it only grabs those attachments.
        if (myStringArray.indexOf(asys) >= 0) {
            var content = new GlideSysAttachment().getContentStream(grSysAtt.sys_id);
            new GlideSysAttachment().writeContentStream(current, grSysAtt.getValue('file_name'), grSysAtt.getValue('content_type'), content);
            template.print("File name" + grSysAtt.getValue('file_name'));
        }
    }

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

 

Now just call this Notification Email script in your notification using the syntax below:

${mail_script:Notification Email Script Name here}

 

find_real_file.png

Hope this helps. Please mark the answer as correct/helpful based on impact.

Regards,
Shloke

Hope this helps. Please mark the answer as correct/helpful based on impact.

Regards,
Shloke

shloke04
Kilo Patron

Hi @amacqueen 

I have shared both approach using Code via Business Rule as well as using No code approach using Flow Designer. Please try and let me know if you are still stuck on this.

Hope this helps. Please mark the answer as correct/helpful based on impact.

Regards,
Shloke

Hope this helps. Please mark the answer as correct/helpful based on impact.

Regards,
Shloke