Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

How to download the attachment from multiple Incidents

Saridha_L1
Tera Expert

Hi Team,

nayan_awadhiya

Could you please suggest me how can I download the attachment from multiple Incidents as a zip file. I have written the code, its not downloading all incident attachments. Only few attachments are downloading.   I have created afield "attachments", then I have used that field name in this code.

Code:

Processors:

(function process(g_request, g_response, g_processor) {

var sysidList = g_request.getParameter('sysparm_sys_id');

var table = g_request.getParameter('sysparm_table');

var sysIDarray;

var theRecord = new GlideRecord(table);

theRecord.addQuery('u_attachments', 'true');

theRecord.query();

while(theRecord.next()){

  if(sysIDarray==''){

  sysIDarray=theRecord.sys_id;

  }

  else

  {sysIDarray = sysIDarray +','+ theRecord.sys_id;}

  //sysIDarray = sysIDarray +','+ theRecord.sys_id;

}

var querystring = "table_sys_idIN"+sysIDarray;

var zipName = 'attachments.zip';

var StringUtil = GlideStringUtil;

var gr = new GlideRecord('sys_attachment');

gr.addEncodedQuery(querystring);

gr.query();

if (gr.hasNext()){

      g_response.setHeader('Pragma', 'public');

      g_response.addHeader('Cache-Control', 'no-cache');

      g_response.setContentType('application/octet-stream');

      g_response.addHeader('Content-Disposition', 'attachment;filename=' + zipName);

      var out = new Packages.java.util.zip.ZipOutputStream(g_response.getOutputStream());

      var count=0;

      while (gr.next()){

              var sa = new GlideSysAttachment();

              var binData = sa.getBytes(gr);

             

              var file = gr.file_name;

              addBytesToZip(out, zipName, file, binData);

              count ++;

      }

      // Complete the ZIP file

      out.close();

}

function addBytesToZip (out, dir, file, stream){

      // Add ZIP entry to output stream.

      out.putNextEntry(new Packages.java.util.zip.ZipEntry(file));

      out.write(stream, 0, stream.length);

      out.closeEntry();

}

})(g_request, g_response, g_processor);

Please help me in this issue as well.

Regards,

Saridha.L

1 ACCEPTED SOLUTION

I have found this blog post's code to work up to Helsinki: https://www.servicenowguru.com/scripting/download-attachments-zip-file/



There are many uses for it. I use it to demonstrate Edge encryption, as this code bypasses the encryption proxy and will download the encrypted files.



JarodM


View solution in original post

22 REPLIES 22

Initialise a file_list variable at the start of the the code


var file_list = '';


Add this function at the end of the code - this inserts the iterator in the correct position in the filename


function insItr(file, itr) {


  var out = '';


  var li = file.lastIndexOf('.'); //get the position of the extension (the last . character)




  if (li == -1) { //filename has no extension, so add the iterator at the end


  out = file + '-' + itr;


  } else { //filename has an extension, so add the iterator just before the extension


  out = file.substring(0, li) + '-' + itr + file.substring(li);


  }




              return out;


}



Between - var file = gr.file_name; - and -this.addBytesToZip(out, zipName, file, binData);- add the following loop which checks the file_list to see if the name exists, calls insItr function to make the filename unique, adds the new name to the file_list


var file = gr.file_name;



var this_file = file.toLowerCase();


var itr = 0;


while (file_list.indexOf(this_file) > -1) {


  itr++;


  this_file = insItr(file.toLowerCase(), itr);


}


if (itr > 0) file = insItr(file, itr);


file_list += ',' + file.toLowerCase();



this.addBytesToZip(out, zipName, file, binData);



The result in the zip will be:
example


example-1


example-2


example.doc


example-1.doc


example-2.doc


exa.mple.doc


exa.mple-1.doc


exa.mple-2.doc


Saridha_L1
Tera Expert

Hi Philip,



Thank you.



Could you please help me how to get the downloaded files with the ticket number?



Regards,


Saridha.L


Saridha_L1
Tera Expert

Can anyone help me please