Download all attachments by a separate processor

pawan2
Tera Expert

Hi All,

Seeking your help to understand why SNGURU solution is not working in Quebec version.

Here's the article I followed to download as I need a separate processor to download all attachments from one table with the zip file name with the ticket number.

Download Attachments as a ZIP File - ServiceNow Guru

Appreciate your assistance.

 

Thanks in advance,

Pawan

3 REPLIES 3

Tony Chatfield1
Kilo Patron

Hi, without seeing the code that you have written\deployed to your instance and details of your debugging\diagnostics there is not a lot of feedback the forum can provide. But the use of 'Processors' has been depreciated for some time and I am surprised to hear that you were able to create a new one?

https://docs.servicenow.com/bundle/paris-application-development/page/script/processors/concept/c_Processors.html

As the documentation indicates, you should not use processors and instead should use REST API's to deliver the functionality you require.

However a quick search of the community forums shows that there is an existing OOB processor that provides the functionality you appear to be looking for.

/sys_processor_list.do?sysparm_query=name%3DDownloadAllAttachmentsProcessor&sysparm_view=

And so all you need to do is create a UI action to call it.
Based on the SNCGuru example something like.
Condition:
current.hasAttachments();

Script:
action.setRedirectURL('download_all_attachments.do?sysparm_sys_id=' + current.getUniqueValue());

 

 

Hi Tony,

 

Thanks for the response. Much appreciated! 

Below is the code I used.

 

-UI Action-

 

Name:Save Attachments as ZIP
Condition:current.hasAttachments();

Script:

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

 

-Processor-

Name: exportAttachmentsToZip
Type: script
Path: exportAttachmentsToZip
Script:

 

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

var theRecord = new GlideRecord(table);
theRecord.addQuery('sys_id', sysid);
theRecord.query();
theRecord.next();

var zipName = 'attachments.zip';

var StringUtil = GlideStringUtil;

var gr = new GlideRecord('sys_attachment');
gr.addQuery('table_sys_id', theRecord.sys_id);
gr.addQuery('table_name', theRecord.getTableName());
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();
}
 
Appreciate you thoughts to understand why this is not working.
 
 
Thanks in advance!
Pawan

 

Pradeep51
ServiceNow Employee
ServiceNow Employee

Hi @pawan2 

Did you find any solution on how to set the custom file name for the zip file?

If yes can you please share with me.

 

Thank you