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

Ankur Bawiskar
Tera Patron
Tera Patron

@MakotoHorioka 

so what's the error?

ServiceNow has inbuilt flow step for this. Did you check that?

Zip operation 

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Thank you for your reply.
The error code is as follows.

<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.

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

Nick Parsons
Mega Sage

It doesn't appear that you've marked your CreateZip script include as "client callable". If it is, you need to ensure that you extend it from global.AbstractAjaxProcessor:

 

 

CreateZip.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {

 

You also need to remove the initialize() method when extending like this, or do a super call to the parent constructor within the initialize method if you wish to keep it with global.AbstractAjaxProcessor.prototype.initialize.apply(this, arguments). Once that's done, you should check your system logs for errors to see if the script include is functioning as expected.