Download Attachments via Script

art_anderson2
Tera Expert

Hi,

I have been working on a script to export several attachments from Incidents.  Using loops I can get the Incidents and attachment records just fine, I simply cannot get them exported.

I tried using a MID Server script but I was unable to figure out how to properly set up the data set,

Scripting looked like it was going to be easy but I have fiddled with this for 4 or 5 hours now. Where I seem to be stuck is getting my output stream properly defined.  I have used these  two articles for reference:

https://community.servicenow.com/community?id=community_question&sys_id=e225af43db6f67844abd5583ca961959&view_source=searchResult
https://www.servicenowguru.com/scripting/download-attachments-zip-file/#comment-34336

For the most part, pretty helpful.  I have tried this from Script Includes, UI Action, FIX Scripts and Background scripts.  I currently have a Script Include with what I consider a pretty good definition. This is specifically where I'm tied up.  As far as the articles state, use g_response to build the output stream. I get an undefined from this code line:

var out = new Packages.java.util.zip.ZipOutputStream(g_response.getOutputStream());

I have tried passing in the variable from the UI Action, Background Script, FIX Script, etc.  I also defined the variable in the Script Include such as:

var thisResponse;
thisResponse.setHeader('Pragma', 'public');
thisResponse.addHeader('Cache-Control', 'max-age=0');
thisResponse.addHeader('Content-Disposition', 'attachment;filename=' + zipName);
thisResponse.setContentType('application/octet-stream');
var out = new Packages.java.util.zip.ZipOutputStream(thisResponse.getOutputStream());

No matter the variable I use or pass in to the Function for "thisResponse.getOutputStream()", I get undefined.

I'm, sure I'm missing something silly, I just don't know what it is.

Thanks you for your support,

Art Anderson

6 REPLIES 6

Hitoshi Ozawa
Giga Sage
Giga Sage

Hi Art,

Just want to clarify the question. Is the question about calling ServiceNow REST APi from some other computer and downloading the file? Scripted REST API can be used to create a REST API endpoint which can be called to download the files.

Or is the question about adding a button or a widget from where end-users can click to download the files.

Hi Hitoshi,

What I was attempting to do was execute this from a button or link on a form or from a widget where a field or variable could be used to pass a list of Incidents to the script which would in turn download that attachment set.  I do understand that users can request an attachment download via Manage Attachments on each individual Incident.  This would enable the ability to download a larger set of incidents based on a target of know incidents without having to open each incident individually.

I was thinking after I had posted that maybe I need to create my response variable as a certain datatype.  I am not sure if that is the case but if so not sure from which class to create it.  i.e. "var thisResponse = new SomeTypeFunction();"

I did try it as an array with same error results. i.e. "var thisResponse = [];"

Once my head clears, I will also retry using a MID server, I just could not get the ExportSet to work.

Maybe I found what I'm looking for, I will investigate this in the morning: https://community.servicenow.com/community?id=community_article&sys_id=b93cad67db831c503daa1ea6689619b1

 

Thank you,

Art

Hi Art ,@art.anderson2

 

Were you able to figure out the way?. I am also having same kind of requirement. Would you please help me to find the way out?. Thanks!!

 

Regards

Madhu

Hi Hitoshi,

Do we have any solutions if we have multiple records to be in scope and the files to be downloaded/placed to some shared folder?

 

Regards

Madhu