How to convert attachment to Base64 format?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 hours ago
Hi Community,
I am working on an integration where the 3rd party system requires ServiceNow attachments to be sent in Base64 format.
How to achieve this?
Regards,
Ritik
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 hours ago
Hi @nirwan_ritik ,
Check below code
var gsa = new GlideSysAttachment();
var attachmentSysId = "attachment_sys_id";
var fileBytes = gsa.getBytes(attachmentSysId);
var base64Encoded = GlideStringUtil.base64Encode(fileBytes);
gs.info("Base64 Encoded String: " + base64Encoded);
Please check below links
Please mark my answer correct and helpful if this works for you
Thanks,
Sarthak
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 hours ago
Hi @nirwan_ritik ,
Hope you are doing good!!
Please review the below code-
var table = 'incident'; // or your table name
var recordSysId = 'abc123...'; // sys_id of the record
var fileName = 'example.pdf'; // name of the attachment file
var attachment = new GlideSysAttachment();
var fileBytes = attachment.getBytes(table, recordSysId, fileName);
// Convert to Base64
var base64 = Packages.java.util.Base64.getEncoder().encodeToString(fileBytes);
// Log or use the Base64 string
gs.info("Base64 content: " + base64);
If my response is helpful please mark as helpful
Thank you!!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
2 hours ago
You can use below article to convert attachment to base 64 format and base64 to attachment
https://www.servicenow.com/community/developer-articles/base64-encoded-attachments/ta-p/3045771
You can also check below relevant property to handle maximum file size limit for base 64 encoding
https://support.servicenow.com/kb?id=kb_article_view&sysparm_article=KB0747638
As per community guidelines, you can accept more than one answer as accepted solution. If my response helped to answer your query, please mark it helpful & accept the solution.
Thanks,
Bhuvan
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
2 hours ago
are you writing the login in global scope or scoped app?
Remember there are limitations on file size when you get base 64 encoded data for attachment.
What did you start with and where are you stuck?
This is for scoped app
For global scope script refer response from @Sarthak Kashyap
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader