- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-22-2017 01:47 AM
Hi Team,
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
Solved! Go to Solution.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-24-2017 07:43 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-19-2017 01:31 AM
Hi Team,
Can anyone help me on this.
Regards,
Saridha.L
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-25-2017 03:33 AM
Hi Harneet,
I am able to download attachments from multiple Incidents. But if I select 2 or 3 Incidents also,Its downloading all the Incident attachments. And also If a attachment has a same doc name,its not downloading. Could you please check the below code and help me to fix this.
Processors Code:
var sysid = 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.addQuery('sys_id', sysid);
theRecord.query();
//theRecord.next();
var c = 0;
while(theRecord.next()){
gs.log('Record number '+c+'-'+theRecord.number);
sysIDarray = sysIDarray +','+ theRecord.sys_id;
c++
}
var querystring = "table_sys_idIN"+sysIDarray;
var StringUtil = GlideStringUtil;
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;
gs.log("SaridhaL Attachment: "+file);
addBytesToZip(out, zipName, file, binData);
count ++;
gs.log("SaridhaL : successfully added "+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();
}
Regards,
Saridha.L
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-02-2017 12:25 AM
Hi All,
Can anyone help on this?
Regards.
Saridha.L
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-02-2017 03:07 AM
I use the same script within my environment but I had to edit it a bit to handle multiple files with the same filename. If you have two files with the same name the zip becomes corrupted.
I did this by storing all filenames in a new variable called file_list. Then after "var file = gr.file_name;" I have a simple loop that checks if the filename exists in the file_list and if found adds a new number to it and checks again. It keeps incrementing until it gets a unique name and then that modified name is used when adding to the zip.
The trick is formatting the filename so that it overwrites the filename and not the extension. ie:
example.doc
example.doc
example.doc
becomes:
example.doc
example-1.doc
example-2.doc
I am not sure if this will help your entire problem, but this is ultimately necessary for what you are trying to do.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-04-2017 06:09 AM
Hi Philip,
Thank you for your response.
Could you please help me with a code ?
Regards,
Saridha.L