How to convert attachment to Base64 format?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks 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 weeks 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 weeks 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
3 weeks 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
3 weeks ago
Thanks for marking the post as helpful.
If my response helped to guide you or answer your query, please mark it helpful & accept the solution.
Thanks,
Bhuvan