The CreatorCon Call for Content is officially open! Get started here.

How to convert attachment to Base64 format?

nirwan_ritik
Tera Contributor

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

6 REPLIES 6

Sarthak Kashyap
Tera Expert

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 

https://www.servicenow.com/community/developer-articles/base64-encoded-attachments/ta-p/3045771?stat...

https://www.servicenow.com/community/servicenow-studio-forum/base64-conversion-for-any-attachment/td...

 

Please mark my answer correct and helpful if this works for you

Thanks,

Sarthak

Nawal Singh
Giga Guru

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!!

Bhuvan
Mega Patron

@nirwan_ritik 

 

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

@nirwan_ritik 

 

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