Converting CSV/excel file into ZIP file in Business Rule

sushma123vu
Tera Contributor

We have scheduled report and whenever scheduled report runs it sends an email, where which we have one BR written on sys_email which gets hits whenever email is sent. In that Br we are attaching the attachment from the email to kb article.

var gsAttachment = new GlideSysAttachment();
        gsAttachment.copy("sys_email", current.sys_id, "kb_knowledge", kbSysID);
        gsAttachment.deleteAll(current);
 
Now I dont want directly my file to be attached to kb article, instead I should change that file into ZIP and then attach to kb article. I have tried different codes but its not working. Could anyone help with the code to convert into ZIP file.
2 REPLIES 2

Ankur Bawiskar
Tera Patron
Tera Patron

@sushma123vu 

so that zip will have only 1 file within it?

If yes then check this link on how to generate zip

You can use flow designer Zip Operation to create zip and add to KB, from that Business rule trigger subflow and pass the Email sysId, KB sysId, then use "Zip Operation" in subflow

Zip operation 

If you want to use script then check this link, remember it will work only in global scope as it uses package calls

Create Zip File From Script 

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

J Siva
Tera Sage

Hi @sushma123vu 
Check out the below script.

var recordIds = ["46e2fee9a9fe19810049b49dee0daf58"]; // SYS ID OF THE KB ARTICLE
var tableName = "incident"; // CHANGE THE TABLE NAME AS REQUIRED
var fName = "TestAttachmentGenerateFromScript.zip";

try {
    // Create a byte output stream for the zip file
    var outStream = new Packages.java.io.ByteArrayOutputStream();
    var out = new Packages.java.util.zip.ZipOutputStream(outStream);

    // Loop through each record ID to fetch attachments
    for (var i = 0; i < recordIds.length; i++) {
        var attachmentGr = new GlideRecord("sys_attachment");
        attachmentGr.addQuery("table_name", tableName);
        attachmentGr.addQuery("table_sys_id", recordIds[i]);
        attachmentGr.query();

        while (attachmentGr.next()) {
            // Get the file stream from the attachment
            var gsa = new GlideSysAttachment();
            var fileStream = gsa.getBytes(attachmentGr);
            var fileName = attachmentGr.getValue("file_name");

            // Add the attachment as an entry in the zip file
            out.putNextEntry(new Packages.java.util.zip.ZipEntry(fileName));
            out.write(fileStream, 0, fileStream.length);
            out.closeEntry();
        }
    }

    // Close the streams
    out.close();
    outStream.close();

    // Attach the zip file to another record
    var incGr = new GlideRecord("incident");  // CHANGE THE TABLE NAME AS REQUIRED
    if (incGr.get("46e2fee9a9fe19810049b49dee0daf58")) {  // SYS ID OF THE KB ARTICLE
        var attachment = new GlideSysAttachment();
        attachment.write(incGr, fName, "application/zip", outStream.toByteArray());
    }
} catch (ex) {
    gs.error("Error when zipping file: " + ex);
}


Source: Create Zip File From Script 

Regards,
Siva