
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-28-2019 12:02 AM
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??
Solved! Go to Solution.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-28-2019 12:52 AM
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.
Kindly mark my comment as a correct answer and also helpful once worked.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-28-2019 12:51 AM
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]);
}

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-28-2019 12:52 AM
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.
Kindly mark my comment as a correct answer and also helpful once worked.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-28-2019 01:12 AM
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();
}

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-28-2019 01:25 AM