Converting CSV/excel file into ZIP file in Business Rule
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-19-2025 03:01 AM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-19-2025 03:12 AM
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
If you want to use script then check this link, remember it will work only in global scope as it uses package calls
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-19-2025 03:16 AM
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