How to get Base64Binary format of attachment
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Friday - last edited Friday
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
Friday - last edited Friday
@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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Friday
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! 🙏
Ankur
✨ Certified Technical Architect || ✨ 10x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
yesterday
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! 🙏
Ankur
✨ Certified Technical Architect || ✨ 10x ServiceNow MVP || ✨ ServiceNow Community Leader
