How to fix getBytes Error

Supriya25
Tera Guru

Hi All,

in Scoped -  SCript Include :

var fileList=[]; 
var att = new GlideRecord('sys_attachment');
            att.addQuery('table_sys_id', '1fabd08cf01a483faf9fa7e67bc0b4b7');
            att.addQuery('file_name', 'ENDSWITH', 'abc.pdf');
            att.query();
            if (att.next()) {
                fileList.push({
                    name: att.file_name.toString(),
                    bytes: new GlideSysAttachment().getContent(att)
                });
            }
        
var zipUtil = new GlobalZipUtil();
  var zipBytes = zipUtil.createZip(files);
gs.info("zipBytes :"+JSON.stringify(zipBytes));

 
Global SCript Include:

n Global SCript Include :
var GlobalZipUtil = Class.create();
GlobalZipUtil.prototype = {
    initialize: function() {},
    createZip: function(files) {
        // files = [{name: 'x.pdf', bytes: [...]}, ...]
        var outStream = new Packages.java.io.ByteArrayOutputStream();
        var zip = new Packages.java.util.zip.ZipOutputStream(outStream);

        for (var i = 0; i < files.length; i++) {
            var entry = new Packages.java.util.zip.ZipEntry(files[i].name);
            zip.putNextEntry(entry);
            zip.write(files[i].bytes, 0, files[i].bytes.length);
            zip.closeEntry();
        }
        zip.close();
        return outStream.toByteArray();
    },

    type: 'GlobalZipUtil'
};




zip.write(files[i].bytes, 0, files[i].bytes.length); we are getting Error here 
zip.write(files[i].bytes, 0, files[i].bytes.length); getting error like can't find method ava.util.zip.ZipOutputStream(string,number,number)


How to fix it please, we are calling "Client Callable Script Include " in  UI Action (client=true).



1 REPLY 1

Jennifer Metz
Giga Guru

Hello @Supriya25,

 

@Allen Andreas posted a very detailed write up on How to download all attachments from a record to a Zip file. I would highly suggest reading through this to see if there is a more efficient way to accomplish the same goal.

 

If you don't find this solution helpful and want to continue on the current path, could you post some screenshots of the actual errors you are seeing?

Jennifer Metz
Sr. ServiceNow Developer | Infosys