Decoding Base64 in ServiceNow: Leveraging Built-In Functionality
- nathanlee142
- 1 day ago
- 3 min read

For ServiceNow developers and administrators working with integrations or data manipulation, encountering Base64 encoded strings is not uncommon. Base64 is a widely used encoding scheme that converts binary data into an ASCII string format, often used for transmitting data over the internet or storing it in text-based formats. If you're working within ServiceNow and need to decode these strings back to their original format using scripting, you might be wondering if there's a built-in way to handle this. Fortunately, ServiceNow provides native functionalities to both encode and decode Base64 strings, which we'll explore in this article. This is particularly relevant for those dealing with inbound or outbound integrations that utilize Base64 encoding.
When faced with a Base64 encoded string in ServiceNow, the first instinct might be to write custom code or a script include to handle the decoding process. However, ServiceNow offers built-in utilities that can simplify this task significantly. The availability of these utilities depends on whether you are working within the global scope or a scoped application, and the version of your ServiceNow instance.
Decoding in the Global Scope:
If your script operates within the global scope, you can leverage the GlideStringUtil class. This utility class provides various string manipulation functionalities, including Base64 encoding and decoding.
How to Decode using GlideStringUtil:
Identify the Encoded String: Locate the Base64 encoded string you need to decode.
Use the base64Decode() Method: Call the static method base64Decode() of the GlideStringUtil class, passing the encoded string as an argument. This method will return the decoded string.
JavaScript
var encodedString = 'bXlfc3RyaW5n';
// This is a Base64 encoded string for 'my_string'
var decodedString = GlideStringUtil.base64Decode(encodedString);
gs.info('Decoded String: ' + decodedString);
// Output: Decoded String: my_string
Decoding in Scoped Applications (Istanbul and Later):
For those working within scoped applications on Istanbul or later versions of ServiceNow, the GlideSystem object (gs) provides dedicated methods for Base64 encoding and decoding.
How to Decode using GlideSystem:
Identify the Encoded String: Locate the Base64 encoded string you need to decode.
Use the gs.base64Decode() Method: Call the base64Decode() method of the gs object, passing the encoded string as an argument. This method will return the decoded string.
JavaScript
var encodedString = 'bXlfc3RyaW5n';
// This is a Base64 encoded string for 'my_string'
var decodedString = gs.base64Decode(encodedString);
gs.info('Decoded String: ' + decodedString);
// Output: Decoded String: my_string
Encoding Base64:
It's also worth noting that both GlideStringUtil and GlideSystem offer corresponding methods for encoding strings into Base64:
Global Scope (GlideStringUtil): GlideStringUtil.base64Encode('your_string_to_encode');
Scoped Application (gs - Istanbul and Later): gs.base64Encode('your_string_to_encode');
Practical Examples and Use Cases:
Integration with External Systems: When receiving data from external systems that use Base64 encoding for transmitting binary data or sensitive information, you can use these methods to decode the data within your ServiceNow scripts.
Parsing Encoded Payloads: If you encounter encoded data within API responses or other data sources, these functions allow you to easily access the original information.
Storing Encoded Data: While decoding is the focus here, the encoding functionality can be useful for scenarios where you need to store data in a Base64 format within ServiceNow.
Alternative Solutions:
While ServiceNow provides these native methods, in very specific edge cases or older versions, you might consider using JavaScript libraries for Base64 encoding and decoding. However, for most common scenarios within supported ServiceNow versions, the built-in functionalities are the most efficient and recommended approach.
Conclusion
ServiceNow offers robust and convenient ways to handle Base64 encoding and decoding directly within your scripts. For global scope scripts, the GlideStringUtil class provides the necessary methods. If you are working within a scoped application on Istanbul or a later release, the GlideSystem object offers similar functionality. Leveraging these built-in tools simplifies your code and ensures efficient handling of Base64 encoded data within your ServiceNow environment. The next step for ServiceNow developers is to utilize these methods whenever they encounter Base64 encoded strings, streamlining their integration and data processing tasks.