- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-21-2024 10:19 PM
Hi Team.
I would like to use the JSZip library to collect attachments attached to a table into a Zip file.
I created "JSZip" using the code from the jszip.js file as a script include.
I am calling a Script Include "CreateZip" in a UI Action "Demo Zip" and calling "JSZip" on that Script Include.
This will cause no error and nothing will happen. Please let me know if there is a correct way.
UI Action「Demo Zip」
function onClick() {
var ga = new GlideAjax('CreateZip');
ga.addParam('sysparm_name','createZip');
ga.getXML(ManagerParse);
function ManagerParse(response) {
alert('A');
}
}
Script Include「CreateZip」
var CreateZip = Class.create();
CreateZip.prototype = {
initialize: function() {},
createZip: function() {
var ga = new GlideRecord('sys_attachment');
ga.addQuery('table_name', 'u_demo_table');
ga.query();
// Initialize JSZip
var zip = new JSZip();
while (ga.next()) {
// Add files to the zip
var file = new GlideSysAttachment().getContentStream(ga);
var fileName = ga.getValue('file_name');
zip.file(fileName, file);
}
// Generate the zip file
zip.generateAsync({
type: "blob"
}).then(function(content) {
// Create a download link for the zip file
var link = document.createElement("a");
link.href = URL.createObjectURL(content);
link.download = "table_data.zip";
link.click();
});
zip.generateAsync({
type: "blob"
})
.then(function(blob) {
saveAs(blob, "hello.zip");
});
},
type: 'CreateZip'
};
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-21-2024 11:44 PM
as already answered by me, it makes no sense to continue. Your given code contains JavaScript promises (.then()) which cannot work in ServiceNow as your Script Include seems to be created in the global scope thus using the old ES5 engine. Further I read something like "generateAsync". Also this cannot work on server-side as ServiceNow has no nodeJS engine and any asynchronous approaches have to be implemented with OOTB means.
Don't waste your time!
Maik
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-21-2024 11:43 PM
Thank you for your reply.
As you taught me, the call from the client side was not possible.
However, this warning appears on the console.
If you know anything, please let me know.
<table_list>.do%3Fsysparm_userpref_module%3D615288a983d03550e7b771647daad32e:1 The resource <instance>.service-now.com/uxasset/set-cache-buster/1704632735275.js was preloaded using link preload but not used within a few seconds from the window's load event. Please make sure it has an appropriate `as` value and it is preloaded intentionally.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-21-2024 11:12 PM
to my mind you should stop going that way as I don't believe it will work. Furthermore a wrong implementation could lead to a system crash, for example because the consumed data extends the available memory.
Instead, use the already proposed ZIP step in the Flow designer. It's provided by ServiceNow and therefore the safe alternative. Furthermore, you can way better debug a Flow than source code.
Maik
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-21-2024 11:20 PM
Thank you for your reply.
I checked about Flow Designer's Zip Step, but it's a paid feature, right?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-21-2024 11:46 PM
as you can see in https://www.servicenow.com/content/dam/servicenow-assets/public/en-us/doc-type/legal/snc-addendum-in... the ZIP step is part of the IntegrationHub Professional license which is a paid version.
Maik