Some PDIs are currently unavailable, and PDI actions are paused. View the latest updates here. Read More

File Attachment Corrupted When Sent to External Application

yana7
Tera Contributor

I want to ask a question about sending file attachments to another application. All the data is transferred, and the file appears to be transferred as well, but when opened, the file is corrupt. Here's my code:

// ATTACHMENT HANDLING using GlideRecord and getAttachment()
var attachmentModelList = [];
var grAttachment = new GlideRecord("sys_attachment");
grAttachment.addQuery("table_sys_id", current.sys_id);
grAttachment.addQuery("table_name", current.getTableName());
grAttachment.query();

while (grAttachment.next()) {
var attachmentSysID = grAttachment.getValue("sys_id");
var attachmentData = {};

try {
var base64Content = GlideStringUtil.base64Encode(attachmentSysID); // Encode using GlideStringUtil
if (!base64Content || base64Content == "") {
gs.error("Base64 error for: " + grAttachment.file_name + " on: " + current.number +
". Size: " + grAttachment.size + ", sys_id: " + grAttachment.sys_id);
continue; // Skip this attachment
}

attachmentData.Attachment = base64Content;
attachmentData.AttachmentName = grAttachment.file_name.toString();
attachmentData.mime_type = grAttachment.content_type.toString();
attachmentModelList.push(attachmentData);
gs.info("Attachment: " + attachmentData.AttachmentName + ", Size: " + base64Content.length);

} catch (ex) {
gs.error("Error encoding attachment: " + grAttachment.file_name + " on: " + current.number +
". Error details: " + ex.getMessage());
}
}
requestBody.Attachment_ModelList = attachmentModelList;

 

0 REPLIES 0