- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-02-2022 01:59 AM
Hi community!
I would like to set up a UI Action that downloads all attachments. I know there is a functionality that does this downloading all files as a ZIP file. But that's not what I want. I need every file downloaded as is without having it in a ZIP file.
Is that possible and how would I do that?
Thanks!
Solved! Go to Solution.
- Labels:
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-02-2022 03:43 AM
Hi,
can be done
1) create a form UI action which is client side
2) Client checkbox - true
3) Onclick - downloadAll()
4) Script:
Ensure your pop-up blocker is allowing the new tabs then only the files would be downloaded
function downloadAll() {
var gr = new GlideRecord("sys_attachment");
gr.addQuery("table_sys_id", g_form.getUniqueValue());
gr.query();
while(gr.next()) {
window.open('/sys_attachment.do?sys_id='+gr.sys_id,"_blank");
}
}
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-02-2022 03:43 AM
Hi,
can be done
1) create a form UI action which is client side
2) Client checkbox - true
3) Onclick - downloadAll()
4) Script:
Ensure your pop-up blocker is allowing the new tabs then only the files would be downloaded
function downloadAll() {
var gr = new GlideRecord("sys_attachment");
gr.addQuery("table_sys_id", g_form.getUniqueValue());
gr.query();
while(gr.next()) {
window.open('/sys_attachment.do?sys_id='+gr.sys_id,"_blank");
}
}
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-02-2022 05:28 AM
Hi Ankur,
your reply directed me in the correct direction but I had several issues with your code because window is undefined and GlideRecord could not be called from my application.
To work around this I had to add a GlideAjax script which does the server side call to GlideRecord and I used "top" instead of "window".
For everyone having the same task to solve, here's my complete solution:
// Script Include (server side)
var MyAjax = Class.create();
MyAjax.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {
getAttachments: function() {
var reportSysID = this.getParameter('sysparm_report_sys_id');
var fileURLs = [];
var gr = new GlideRecord("sys_attachment");
gr.addQuery("table_sys_id", reportSysID);
gr.query();
while(gr.next()) {
fileURLs.push('/sys_attachment.do?sys_id='+gr.sys_id);
}
return JSON.stringify(fileURLs);
},
type: 'MyAjax'
});
// UI Action function (client side)
function downloadAll() {
try {
// run server side logic
var ajax = new GlideAjax('MyAjax');
ajax.addParam('sysparm_name', 'getAttachments');
ajax.addParam('sysparm_report_sys_id', g_form.getUniqueValue());
ajax.getXML(runDownload);
} catch (e) {
alert(e.message);
}
}
// callback function for ajax call
function runDownload(response) {
try {
var files = response.responseXML.documentElement.getAttribute("answer");
files = JSON.parse(files);
for (var i = 0; i < files.length; i++) {
top.open(files[i], '_blank');
}
} catch (e) {
alert(e.message);
}
}
I hope this helps!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-02-2022 07:54 AM
Glad to help.
Did you mistakenly marked your own response as correct?
The approach I shared will also work and can be enhanced as per your requirement such as using GlideAjax etc
Would you mind marking my response as correct as the overall point here was to help guide you towards the approach.
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-03-2022 12:35 AM
Hi Ankur,
coming from Stack Overflow I marked the answer correct that solved exactly my problem so future users with the same question do not get confused. As I needed a UI Action to get the work done your script did not work as it has server side code combined with client side code.
But after considering that my question title doesn't explicitly state that it should be client side and you are listing the steps I needed to know I now reasoned that I should give you the credit for the response even if the script won't work for UI Action (at least it doesn't for me as GlideRecord isn't found Client Side - maybe I am missing something).
Thanks again for your help!

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-02-2022 07:59 AM
Hi,
Nice work. Since your UI Action is client side, it would be best practice and more practical to use the method you went with versus a GlideRecord call directly in the UI Action script at that point.
Appreciate you sharing.
Please mark reply as Helpful, if applicable. Thanks!
Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!