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

Hi, 

I have the same requirement of "downloading attachments of multiple incidents(which are selected from list) on click of button", I have tried the instructions from above mentioned link but i am able to download only single incident attachment only not all the incidents that i have selected. Can anyone help on this please.

Harneet Sital
Mega Sage
Mega Sage

Hi Saridha,



I have worked on your requirement and this code would work like a charm if you want to download all the attachment from incidents selected in the list view.


For this you need to create a new field of type true/false, for all those with check true's attachment can be downloaded into a single zip file. Here we have used u_ok as a field in the processor script, change it with the name you have created a new field with. Also create UI action on the Incident table since we have created it on a custom table.



Steps to create the same :


1. Create a new UI action of available on the list choice as below ;



Code for the UI action


action.setRedirectURL('exportAttachmentsToZip.do?sysparm_sys_id=' + current.sys_id + '&sysparm_table=' + current.getTableName());



find_real_file.png


Add the following processor script in the System Definitions > Processors.



Create a new processor as following and write the code mentioned.



(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 str= "sys_idIN"+sysidList;


var theRecord = new GlideRecord(table);


//theRecord.addEncodedQuery(str);


// USE THE QUERY AS THE FIELD NAME YOU HAVE CREATED FOR ATTACHMENT IS CHECKED


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


theRecord.query();




//var querystring = "table_sys_idIN"+sysIDarray;



while(theRecord.next()){


  gs.log('Record number 1 -'+theRecord.number);


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


  gs.log(sysIDarray);


}


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', 'max-age=0');


      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();


  gs.log('Record number 2 -'+theRecord.number);


}



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);



And make sure processor name and path same as mentioned in the screenshot.


find_real_file.png



let me know if have any query or need any more clarifications.


Hi Harneet ,



Thank you for your response. I have tried with the above code, Its downloading only the recent attachment. It shows as a Zip file. But not all the attachment. Could you please help me?




Regards,


Saridha.L


Hi Sanidha,



Did you create a new field for which the attachments needs to be downloads? there needs to be a new field created of type true false which when checked for all those incidents the attachments would be downloaded.


// USE THE QUERY AS THE FIELD NAME YOU HAVE CREATED FOR ATTACHMENT IS CHECKED at u_ok place


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


Hi Harneet,



Yes, I have created a field Attachment, and put the field name in the script.



Regards,


Saridha.L