Screenshot_9_20_14__12_01_PM

 

Base 64 encoding is a method used to convert binary data into ASCII strings. This is especially helpful if you want to convert a file, picture, or other object into a string in order to distribute it through mechanisms such as web services or database storage.

This type of encoding will convert any sort of data into a string of case-sensitive letters and numbers. While the result will generally be a longer string of text than its original counterpart, it  offers benefits such as taking a variety of types of data and converting it to something that can be passed over a URL, or stored in a text field.

While there is little documentation on converting data to base 64 format in ServiceNow, it can actually be done with a single line of code. The following code snippet will take a string and convert it to its base 64 equivalent and printed to the log. It will then take that new base64 string and decode back it its original format and write it to the system log.

1
2
3
4
5
6
7
var base64string = GlideStringUtil.base64Encode("^.^ JOHN ANDERSEN!");
gs.log(base64string);
//yields: Xi5eIEpPSE4gQU5ERVJTRU4h

orig = GlideStringUtil.base64Decode(base64string);
//yields: ^.^ JOHN ANDERSEN!
gs.log(orig);

For Scoped Applications, you will want to use the following functions available to ONLY scoped applications:

1
2
gs.base64Encode("My String");
gs.base64Decode("ENCODEDSTRINGGOESHERE");

If you ever need to do this manually, I do recommend using a tool my brother wrote at:

String Encode/Decode Tool