How to use JSZip library

MakotoHorioka
Tera Guru

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'
};

 

1 ACCEPTED SOLUTION

Hi @MakotoHorioka 

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

View solution in original post

8 REPLIES 8

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.

Maik Skoddow
Tera Patron
Tera Patron

Hi @MakotoHorioka 

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

Thank you for your reply.
I checked about Flow Designer's Zip Step, but it's a paid feature, right?

Hi @MakotoHorioka 

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