- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-06-2024 08:40 PM
Hi everyone, I have a task to create an zip file include all attachment of record and attach to another record. I used code below to generate zip file but when download and extract it has error Invalid type. So anyone here know how to fix that. Thank you very much.
The code is:
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-06-2024 09:34 PM
Hello @Hoang Dung,
It looks like you're facing an issue while generating the ZIP file, likely due to how the attachment stream is being processed and written to the ZIP. Here’s an improved version of your script that addresses potential issues with stream handling and encoding:
var recordIds = ["ed92e8d173d023002728660c4cf6a7bc", "57af7aec73d423002728660c4cf6a71c"];
var tableName = "incident";
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");
if (incGr.get("a623cdb073a023002728660c4cf6a768")) {
var attachment = new GlideSysAttachment();
attachment.write(incGr, fName, "application/zip", outStream.toByteArray());
}
} catch (ex) {
gs.error("Error when zipping file: " + ex);
}
Changes made:
1. Stream Writing: Made sure to retrieve and correctly write each attachment as bytes before adding it to the ZIP stream.
2. GlideSysAttachment.getBytes(): Used the appropriate `getBytes` method to handle binary data from attachments.
3. Stream Closure: Correctly closed streams to avoid any stream corruption.
4. Error Handling: Improved logging for easier troubleshooting.
This script should fix your issue with ZIP extraction.
Thanks & Regards,
Siddhesh Jadhav
If this solution helps, please mark it as helpful and accepted.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-06-2024 09:34 PM
Hello @Hoang Dung,
It looks like you're facing an issue while generating the ZIP file, likely due to how the attachment stream is being processed and written to the ZIP. Here’s an improved version of your script that addresses potential issues with stream handling and encoding:
var recordIds = ["ed92e8d173d023002728660c4cf6a7bc", "57af7aec73d423002728660c4cf6a71c"];
var tableName = "incident";
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");
if (incGr.get("a623cdb073a023002728660c4cf6a768")) {
var attachment = new GlideSysAttachment();
attachment.write(incGr, fName, "application/zip", outStream.toByteArray());
}
} catch (ex) {
gs.error("Error when zipping file: " + ex);
}
Changes made:
1. Stream Writing: Made sure to retrieve and correctly write each attachment as bytes before adding it to the ZIP stream.
2. GlideSysAttachment.getBytes(): Used the appropriate `getBytes` method to handle binary data from attachments.
3. Stream Closure: Correctly closed streams to avoid any stream corruption.
4. Error Handling: Improved logging for easier troubleshooting.
This script should fix your issue with ZIP extraction.
Thanks & Regards,
Siddhesh Jadhav
If this solution helps, please mark it as helpful and accepted.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-06-2024 09:50 PM
thanks, it's worked 😁
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-23-2025 11:57 AM
Hi @Hoang Dung @Siddhesh Jadhav
Need your help. I am trying to perform a similar process with the below code
and when I execute the code I am receiving security restriction error
Security restricted: Attempted access to restricted class name java.util.zip.GZIPOutputStream.
Have you faced this error and if so please let me know what needs to be done.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-02-2025 04:17 AM
Hi @hamanthk @Hoang Dung @Siddhesh Jadhav
any solution for java error?
my script is defined in global scope still java error am facing
Generation failed: {"error":"ZIP failed","details":{"error":"ZIP creation failed","details":"\"java\" is not defined."}}