The CreatorCon Call for Content is officially open! Get started here.

Convert binary attachment file to base64 and attach in the attachment table

Vaibhav Sharma2
Tera Contributor

Hi All,

 

I am encountering an issue where the response is coming in Binary format (as shown in the attached file) but when we are trying to convert it into base64 and attaching in the sys_attachment table, it is getting attached as a corrupt file. I am using below code in the action of flow designer.

 

Binary Response screenshot

 

VaibhavSharma2_0-1717154021722.png

 

 

VaibhavSharma2_0-1717153095713.png

var respBody = inputs.responseBody;

//var contentType = headerVal.content-type;
//var contentType = 'application/vnd.openxmlformats-officedocument.wordprocessingml.document';
var contentType = 'application/vnd.openxmlformats-officedocument.wordprocessingml.document';
var attachmentContent = attachment.getContentBase64(respBody);
var StringUtil = new GlideStringUtil();
var base64EncodedData = StringUtil.base64Encode(respBody.toByteArray());//.toByteArray()
//var content = attachmentContent;
gs.log(attachmentData,'vaibhavheader');
var eccGr = new GlideRecord('ecc_queue');
eccGr.initialize();
eccGr.setValue('agent', 'AttachmentCreator');
eccGr.setValue('topic', 'AttachmentCreator');
eccGr.setValue('name', 'testdocument.docx' + ':' + contentType); // give here the file name and content type
eccGr.setValue('source', 'incident' + ':' + '3de8e6ee83560a108d1133326daad3ad'); // give here the table name and record sysId to which you need to attach
eccGr.setValue('payload',attachmentData );
eccGr.insert();
})(inputs, outputs);

 

 

 

 

Ask is to convert the response body in the base64 format and attach the file in the attachment table. I have attached the file "testdocument.docx" and response

2 REPLIES 2

dgarad
Giga Sage

Hi @Vaibhav Sharma2 

Refer to the below link.

https://www.servicenow.com/community/developer-forum/attachments-api-converting-binary-files-to-base...

 

If my answer finds you well, helpful, and related to the question asked. Please mark it as correct and helpful.

Thanks
dgarad

Vaibhav Sharma2
Tera Contributor

Thanks. I have already tried the given code but it is not working.