Interested in a ServiceNow event built for developers? Registration for now[dev]26 is officially open!

How to Convert of bytes to Base64

KeerthiPraV
Tera Contributor
var gsa = new GlideSysAttachment(); var fileBytes = gsa.getBytes( attachment ); var pdfBase64 = GlideStringUtil.base64Encode(file Bytes); //Here i am getting error as: Cannot find default value for object. i am using this code to convert attachment to base64 string conversion, as i need to pass base64 string to external system as it accepts it like that. How to do it for Global scope
2 REPLIES 2

Vishal Jaswal
Tera Sage

Hello @KeerthiPraV 

//Method#1
var gsa = new GlideSysAttachment();
var fileBytes = gsa.getBytes(attachment);
var pdfBase64 = GlideStringUtil.base64Encode(fileBytes); //your code has a whitespace in the name of this variable
gs.info(pdfBase64);

//Method#2

var gsa = new GlideSysAttachment();
var fileBytes = gsa.getBytes(attachment);
var pdfBase64 = Packages.java.util.Base64.getEncoder().encodeToString(fileBytes);
gs.info(pdfBase64);

Hope that helps!

Tanushree Maiti
Tera Patron

Hi @KeerthiPraV 

 

In your script, there is space between file and bytes.

 

You can try with it as well.

 

var gsa = GlideSysAttachment();
var fileBytes = GlideBase64.encode(gsa.getByte(attachment));
var pdfBase64 = GlideStringUtil.base64Encode(fileBytes);

 

 

Please Accept the solution if it assisted you with your question & Mark this response as Helpful.
Regards
Tanushree Maiti
ServiceNow Technical Architect
LinkedIn: https://www.linkedin.com/in/tanushreemaiti