Attachments

Khozema Attar1
Tera Guru

Hello Experts,

We are performing a third-party incident Integration, we need to send attachments to the third party while we create a new incident via business rule only.

we have written a business rule which has 2 fields: FileName & FileType through which we need to send the attachments to the third-party.

Can anyone help on how to send the attachments to the third-party through the below business rule.

find_real_file.png

Thanks in advance

Khozema Attar

1 ACCEPTED SOLUTION

I am able to send attachment but after download that attachment that file(png) is not opening and differ in size

View solution in original post

16 REPLIES 16

Hi Sneha,



I was able to send attachments to third party .


But, as we are using SOAP for the integration ,attachments of size greater than 5MB are not transmitted.


So, Is there a way I can call REST API's POST method in my existing business rule to send attachments.



Find the Screenshot of the BR:


find_real_file.png



Thanks & Regards


Khozema


Hi Khozema,



getBytes() method won't work for file more than 5MB. Use below code to getBytes() for any file which is less than 5MB or greater than 5MB



var gr = new GlideRecord("sys_attachment");


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


  gr.addQuery("table_name", current.getTableName());


  gr.query();


  if (gr.next()){


  var StringUtil = new GlideStringUtil();



  var gsa = GlideSysAttachmentInputStream(gr.sys_id.toString());


  var baos = new Packages.java.io.ByteArrayOutputStream();


  gsa.writeTo(baos);


  baos.close();


var base64EncodedData =   StringUtil.base64Encode(baos.toByteArray());



Mark Correct if this solves your issue and also hit Like and Helpful if you find my response worthy based on the impact.


Thanks


Ankur


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

Hi   Ankur Bawiskar,



Can you rectify my code according to your script:



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



  var attachment = new GlideRecord('sys_attachment');


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


      attachment.addQuery('table_name', current.getTableName());


  attachment.query();


  while (attachment.next()) {


      sendAttachment(attachment);


}


  function sendAttachment(att) {


      try {


              var sa = new GlideSysAttachment();


              var binData = sa.getBytes(att);


              var encData = GlideStringUtil.base64Encode(binData);


              var file_name = att.file_name.toString();


              sendRequest(encData, file_name);


      } catch (Exception) {


              gs.log('Failed sending Attachment to due to Exception: ' + Exception);


      }


}


  function sendRequest(encData, file_name) {


      //Set up variables


  s.setStringParameter('attachment.filename', file_name);


              s.setStringParameter('attachment.type',encData);    


}






})(current, previous);




Thanks & Regards


Khozema