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 

6 REPLIES 6

Tanushree Maiti
Tera Sage

Hi @SANDEEP28 

 

You can check this article :https://www.servicenow.com/community/developer-forum/converting-the-attachment-into-base64-encoded-s....

 

Please mark this response as Helpful & Accept it as solution if it assisted you with your question.
Regards
Tanushree Maiti
ServiceNow Technical Architect
Linkedin:

Ankur Bawiskar
Tera Patron

@SANDEEP28 

usually attachments are sent with their base64encoded data

what do you mean by base64Binary?

did they give any example for this?

💡 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
Mega Sage

@Ankur Bawiskar They confirmed that it should be base64 encoded of raw binary data

@SANDEEP28 

you can use this sample script to get bas64encoded data of file in global scope

var attGr = new GlideRecord('sys_attachment');
  attGr.addQuery('sys_id', '3fd6017407d3dc50540bf2508c1ed027');
  attGr.query();
  if(attGr.next())
        {
   var gsu = (typeof GlideStringUtil != 'undefined') ? (GlideStringUtil) : (Packages.com.glide.util.StringUtil); //few versions support the first one, other supports second
   var gsa = (typeof GlideSysAttachment != 'undefined') ? (new GlideSysAttachment()) : (new Packages.com.glide.ui.SysAttachment());
   var attachmentData = gsa.getBytes(attGr);
   var attachment = String(Packages.java.lang.String(attachmentData));
   gs.info(attachment); //the data in the file will be printed as String
   var encData = GlideStringUtil.base64Encode(attachmentData); // data of one attachment at a time
   gs.info(encData);
}

💡 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