How to get Base64Binary format of attachment
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
4 hours ago - last edited 4 hours ago
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.
I want to ensure that the base64Binary output is correct and represents the actual attachment data.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 hours ago
Hi @SANDEEP28
You can check this article :https://www.servicenow.com/community/developer-forum/converting-the-attachment-into-base64-encoded-s....
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 hours ago
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! 🙏
Ankur
✨ Certified Technical Architect || ✨ 10x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 hours ago
@Ankur Bawiskar They confirmed that it should be base64 encoded of raw binary data
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 hours ago
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! 🙏
Ankur
✨ Certified Technical Architect || ✨ 10x ServiceNow MVP || ✨ ServiceNow Community Leader
