Need to download attachments from attachment table

Chang Yeon Hong
Tera Contributor

I need to download attachments from the attachment table [sys_attachment] with a filter. There was an old way using processors but they are no longer available. I searched that rest api is another option but you can only get json file with download link but that won't do anything. I need the actual files. Has anyone found a simple way to get this done?

5 REPLIES 5

Bert_c1
Kilo Patron

Hi,

 

https://[instance_name].service-now.com/sys_attachment_list.do?sysparm_query=&sysparm_view= 

 

filter based on the available fields and then click on the desired record.  change the file extension if needed to match the content type.

Amit Gujarathi
Giga Sage

Hi @Chang Yeon Hong ,
I trust you are doing fine.
You can use below code for as a solution

var attachmentGr = new GlideRecord('sys_attachment');
attachmentGr.addEncodedQuery('YOUR_FILTER_HERE');
attachmentGr.query();
if (attachmentGr.next()) {
    var file = new GlideSysAttachment().getAttachmentStream(attachmentGr);
    // Do something with the file
}

Was this answer helpful?


Please consider marking it correct or helpful.


Your feedback helps us improve!


Thank you!


Regards,


Amit Gujrathi



 

 

 

var attachmentGr = new GlideRecord('sys_attachment');
attachmentGr.addEncodedQuery('table_nameSTARTSWITHsn_app');
attachmentGr.query();
while (attachmentGr.next()) {
    gs.info("Attachment file name: " + attachmentGr.file_name);
    var file = new GlideSysAttachment().getAttachmentStream(attachmentGr);
    gs.info("File: " + file);
    // Do something with the file
}

 

 

 

Results in:

 

 

 

*** Script: Attachment file name: response.txt
*** Script: File: undefined
*** Script: Attachment file name: response.txt
*** Script: File: undefined
*** Script: Attachment file name: response.txt
*** Script: File: undefined

 

 

 

I did find documentation,  See below. But I fail to see how that is useful to download files. The files can be downloaded from the list view of sys_attachment as I posted above.

 

https://developer.servicenow.com/dev.do#!/reference/api/utah/server_legacy/GlideSysAttachmentGlobalA...

 

However. 'getAttacmentStream()' is not documented.

I need to find a way to download multiple files at one go. Like a filter the sys_attachment table and download the files into zip.