The Zurich release has arrived! Interested in new features and functionalities? Click here for more

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

4 REPLIES 4

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
Mega 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
Kilo 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

Ankur Bawiskar
Tera Patron
Tera Patron

@nirwan_ritik 

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

AnkurBawiskar_0-1758719018435.png

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.

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader