Downloading All attachments using SNGuru solution to capturing all attachments on a task as a ZIP file ?

Liyakhat
Mega Expert

Hello All,

 

I am trying to download all the attachments from attachment table for incidents/request items

i have seen a solution provided by servicenow guru 

https://www.servicenowguru.com/scripting/download-attachments-zip-file/

this works fine for single incident/request item and it downloads perfectly

my requirement is to download all the attachments from the attachments table,here we are passing sys_id and table name from ui action to script processor ,can we pass multiple sys_ids and loop through it to download all attachments of all incidents??

 

1 ACCEPTED SOLUTION

asifnoor
Kilo Patron

Hi Liyakhat,

I have provided a similar solution earlier here in the community. 

Kindly refer to this link where I have tweaked the code to download all attachments as a zip file. Follow the steps given in the Accepted answer in this link and it will work for you.

https://community.servicenow.com/community?id=community_question&sys_id=e225af43db6f67844abd5583ca96...

Kindly mark my comment as a correct answer and also helpful once worked.

 

View solution in original post

11 REPLIES 11

Harsh Vardhan
Giga Patron

If you have multiple sysid then you can split them to set as an array and then create a for loop here to run on length of the array

 

eg: 

 

var abc = 'sysid1,sysid2,sysid3';

var res = abc.split(','); // in your case you can try to split('') like this. 

gs.log('Length is'+res.length);

for(var i=0;i<res.length;i++){

 

put your glide record here  filter would be like below. 

gr.addQuery('sys_id','IN', res[i]);

}

 

 

asifnoor
Kilo Patron

Hi Liyakhat,

I have provided a similar solution earlier here in the community. 

Kindly refer to this link where I have tweaked the code to download all attachments as a zip file. Follow the steps given in the Accepted answer in this link and it will work for you.

https://community.servicenow.com/community?id=community_question&sys_id=e225af43db6f67844abd5583ca96...

Kindly mark my comment as a correct answer and also helpful once worked.

 

Hello asifnoor,

can i put a query in this to download table records seperately,like incident seperately,request items seperately as below highlighted??

var zipName = 'attachments.zip';
var StringUtil = GlideStringUtil;
var gr = new GlideRecord('sys_attachment');


gr.query('table_name','incident');

gr.query();


if (gr.next()){
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);
//add counter so that the filename is NOT duplicate
var file = gr.file_name+"-"+count;
gs.info("Entered into this loop"+file);
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();
}

Yes you can and it will download only incident attachments.