Get a first look at what's coming. The Developer Passport Australia Release Preview kicks off March 12. Dive in! 

How to get Base64Binary format of attachment

SANDEEP28
Mega Sage

Hello Experts,

 

I need to send a ServiceNow record attachment to a third-party document API. This API only accepts data in base64Binary format. Is there a method available to retrieve the binary content of an attachment and then convert it into base64?

 

I referred to a thread that explains how to fetch the binary using a REST API call and then convert it to base64 using the GlideStringUtil API. After obtaining the content in base64Binary format, I tested it by uploading to a “base64-to-file” website. While the file was created, the content turned out to be empty.

 

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

 

I want to ensure that the base64Binary output is correct and represents the actual attachment data.

 

@Ankur Bawiskar 

7 REPLIES 7

@Ankur Bawiskar Thanks for the code. I tested it and it only generate base64 if file is 5MB or below. If file size if more that 5 MB then doesn't print anything in output. We would be sending files more that 10 MB to external API. Looks like above code methods has limitations

@SANDEEP28 

yes it has limitation for 5MB

see if this helps

var gr = new GlideRecord('sys_attachment');
	gr.get('d7ff2c044f96cc90fc11fa218110c746');

	var StringUtil = new GlideStringUtil();
	var gsis = GlideSysAttachmentInputStream(gr.sys_id.toString());
	var ba = new Packages.java.io.ByteArrayOutputStream();
	gsis.writeTo(ba,0,0);
	ba.close();
	var base64EncodedData = StringUtil.base64Encode(ba.toByteArray());
gs.info(base64EncodedData);

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

Regards,
Ankur
Certified Technical Architect  ||  10x ServiceNow MVP  ||  ServiceNow Community Leader

@SANDEEP28 

Hope you are doing good.

Did my reply answer your question?

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

Regards,
Ankur
Certified Technical Architect  ||  10x ServiceNow MVP  ||  ServiceNow Community Leader