Only Last attachment to be sent in Email Notification

awentz
Kilo Contributor

Our system is set up with Cases that are being worked and resolved (like an IT Incident). An Email Notification is sent out on case resolution that includes Close Notes the case worker types in. On occasion, there is also an attachment that needs to go back to the requester however, there can be multiple attachments (pdf, excel, etc) linked to the Case and we only want the last attachment that the case worker adds to be sent out with the final Email Notification (Resolution) - not all attachments. This would prevent the case worker from sending an additional manual email with an attachment. What is the best way to build this?

We want:

1.) The last attachment the case worker adds to the case to be sent in the the Email Notification (Resolution)

2.) If the Case worker does not upload an attachment, then the Email Notification is sent without an attachment (even if attachments are in the case as they came in from the email on Case Create).

My other thought would be if there was a way to attach a file within the 'Closed Notes' section and only that attachment would be sent. Not sure if that is even possible.

Let me know if you have any suggestions or ideas.

11 REPLIES 11

edwin_munoz
Mega Guru

Hello Angela,



Create an email script with the following code:



addAtachments();




function addAtachments() {


   


      var gr = new GlideRecord('sys_attachment');


      gr.addQuery('table_sys_id',current.sys_id);


      gr.addQuery('sys_created_by',current.YOURCASEWORKERFIELD.user_name);


      gr.orderByDesc('sys_created_on');


      gr.query();


   


      if(gr.next()) {


              template.print('Attachment: <a href="https://' +


                                            gs.getProperty('instance_name') +


                                            '.service-now.com/sys_attachment.do?sys_id=' +


                                            gr.sys_id + '">' + gr.file_name + '</a>\n');


      }


}



Then reference the mail script it on your notification


${mail_script:script name}


Thank you Edwin,


The above code will create a link to the attachment, correct? This notification goes to people outside our organization, will they be able to access the link?


Hi Edwin,


I've implemented the above code and it's doing exactly what I want except for one thing. Inserting a link instead of the actual attachment. How do I have it actually send the attachment? Thanks!


Hello Angela,



Sorry for not replying earlier, we would have to use a different approach than the one I suggested above.



You will need to create two business rules, one before insert (to copy the attachments from the case record)and the other one after insert (to delete all attachments except the latest one):



Before insert business rule:


Condition: current.mailbox.getDisplayName() == "Outbox" && current.target_table == 'yourCaseTableNameHere'  



function onBefore(current, previous) {



GlideSysAttachment.copy(current.target_table, current.instance, "sys_email", current.sys_id);


   


}



After insert business rule


Condition: current.mailbox.getDisplayName() == "Outbox" && current.target_table == 'yourCaseTableNameHere'  


function onAfter(current, previous) {


var attachment = new GlideRecord('sys_attachment');    


  attachment.addQuery('table_sys_id', current.sys_id);    


  attachment.orderByDesc('sys_created_on');  


  attachment.query();  


 


  attachment.next()



  while(attachment.next()){    




  attachment.deleteRecord();    




  }  


   


}