OBLYTECH Logo
OBLYTECH Logo

GlideStringUtil in ServiceNow scripts: the string helper methods

GlideStringUtil is a static helper object for handling strings in ServiceNow server-side scripts. It runs in both scoped and global scripts, and its fifteen methods cover HTML escaping, character cleanup, URL encoding, query-safe output, and two checks that return true or false. This page covers the whole class, not one method.

GlideStringUtil is one static object that works in scoped and global scripts

You do not create a GlideStringUtil object yourself. You call its methods directly on the object, for example GlideStringUtil.escapeHTML(value). Each example on the class page passes one value to a method and shows the result. Because the object is available in scoped applications and in global scripts, the same helper can sit in an application you ship and in a global business rule on the instance.

Scope affects a few methods. The HTML decoding method is named unescapeHTML in scoped scripts and unEscapeHTML, with a capital E, in global scripts. A snippet that calls the HTML decoding method or urlEncode will not run unchanged across scopes, so check the scope of the script before copying code from another example.

The one method limited to global scope is urlEncode. A scoped application that needs URL encoding has to move that step into a global script or find another route. Everything else on the class is available in both scopes.

Escaping methods that prepare text for HTML

Text that comes from a user or from a record field can contain characters that HTML reads as markup. escapeHTML replaces those illegal characters with entity codes. The purpose is to prevent UI rendering problems and client-side injection. Its example takes a double quote, two angle brackets and an ampersand and returns "<>&. Typically this is the method to reach for when a value goes into a page, a message or a UI element as markup.

getHTMLValue also converts illegal HTML characters into HTML notation. Its example is a single ampersand, which becomes &. Read the two methods as close relatives: escapeHTML is the one whose example covers quotes and brackets alongside the ampersand, so it is the better choice for general output.

The reverse direction belongs to unescapeHTML in scoped scripts, or unEscapeHTML in global ones. It replaces HTML escape sequences with their characters. It turns entity codes back into their characters, so "<>& becomes a double quote, two angle brackets and an ampersand. Use it when a stored value has been escaped and must be read as plain text again, such as before it goes into a plain-text export.

newLinesToBreaks handles a different HTML problem. It replaces newline characters with the line break tag in its self-closing form. A multi-line value shown in an HTML view will otherwise collapse onto one line, because browsers ignore raw newlines. The example turns a newline between two phrases into a break tag and leaves the rest of the text untouched.

Method Input Output
escapeHTML double quote, two angle brackets, ampersand "<>&
getHTMLValue ampersand &
unescapeHTML (scoped) or unEscapeHTML (global) "<>& double quote, two angle brackets, ampersand
newLinesToBreaks text with a newline between two phrases same text with a line break tag in place of the newline

Applying escapeHTML twice to the same text is a common slip. The ampersand from the first pass becomes an ampersand inside a new entity on the second pass, so the reader sees the escape code instead of the character. Escape once, at the point where the value leaves the script for the browser.

Cleanup methods that remove or rewrite characters

Five methods change characters without producing markup, and they differ in how much they keep.

escapeAllQuotes removes quote characters from the string entirely. The example turns the phrase with an apostrophe into the same words with the apostrophe gone. This suits values where the quote carries no meaning and would break surrounding syntax.

escapeTicks keeps the quote and places a backslash in front of it. The example turns an apostrophe into a backslash followed by an apostrophe. Use this when the quote has to survive, for example when the value is placed inside a single-quoted string in a script.

escapeNonPrintable makes invisible characters visible. Tab, null and bell characters become printable notation: the tab code shows as backslash-t and the null and bell codes show as Unicode escape forms.

normalizeWhitespace converts carriage returns, line feeds and tabs into spaces, then removes leading, trailing and repeated spaces. The example takes a string with a newline and a tab between words and returns the same words separated by single spaces. Typically this is the step to run before comparing two strings typed by people, because a trailing tab or a doubled space makes two otherwise identical values look different.

getNumeric extracts the numeric characters from a string. The example takes a mix of numbers and words and returns the digits run together, with the words removed. Do not use it where separators matter.

Methods for URLs, file names and query strings

Some values travel into a URL, a file name or an encoded query, and each of those has its own special characters.

dotToUnderBar replaces periods with underscores. The example takes a path made of parent-directory segments and returns the same segments with underscores where the dots were. Use it when a user-supplied value must become a file or folder name.

escapeForHomePage replaces problem characters with escape sequences. The example turns the angle brackets around a word into percent-coded forms, and leaves the newline in the same string unchanged. Because the newline is not converted, this method does not prepare multi-line text for display. Use newLinesToBreaks or normalizeWhitespace for that job.

urlEncode encodes non-ASCII characters, unsafe ASCII characters and spaces using UTF-8 and percent encoding, so the result can be used in a web address. The example shows the characters that come out encoded: spaces become plus signs, the colon becomes %3A, the double quote becomes %22, the angle brackets become %3C and %3E, and the ampersand becomes %26.

escapeQueryTermSeparator protects the caret that ServiceNow uses as the query term separator. In the example, a value that contains a single caret comes back with two carets in its place. A lone caret in a value placed into an encoded query can be read as a separator, so escape the value before you concatenate it into the query.

Two checks that return true or false

Two methods are exceptions to the string rule. They return a Boolean, so the result is true or false rather than text.

isBase64 returns true when the string is valid base64. The example passes a string that includes an asterisk and returns false. The asterisk falls outside the base64 character set, which is why the check fails. Use this check before decoding a value that might not be base64 at all.

isEligibleSysID returns true when the string is in sys_id format. The example passes a 32-character hexadecimal string and returns true. Because the check is about format, it confirms that a value looks like a sys_id. It does not confirm that a record with that ID exists, so run a lookup when existence matters.

Choosing a method by where the value is going

Start from the destination. A value going into HTML markup needs escapeHTML. A stored value that must read as plain text again needs unescapeHTML in scoped scripts or unEscapeHTML in global ones. A value going into a web address needs urlEncode, run in a global script. A value going into an encoded query needs escapeQueryTermSeparator. A value becoming a file name needs dotToUnderBar. A value that two people typed and that must match another value needs normalizeWhitespace before the comparison.

A useful habit is to clean and check input when it arrives, keep internal logic on plain values, and escape when the value leaves the script. That keeps each method doing one job.

Frequently asked questions

Can I use all fifteen GlideStringUtil methods in a scoped application?

Fourteen of the fifteen methods are available in scoped and global scripts. urlEncode is the one limited to global scope. The HTML decoding method is also available in scoped scripts, but under the name unescapeHTML rather than unEscapeHTML.

Why does the HTML decoding method have two different names?

The method is named unescapeHTML in scoped scripts and unEscapeHTML in global scripts. The capital E is part of the global name, so a snippet copied between scopes needs its method name changed to match.

Which method should I use to make text safe for an HTML page?

Use escapeHTML. Its example covers a double quote, two angle brackets and an ampersand, and it returns entity codes for each. Escape once, at the point where the value leaves the script for the browser, so the ampersands are not escaped a second time.

Does isEligibleSysID confirm that a record exists?

No. The method checks format. It returns true when the string is in sys_id format, as the example shows. Format alone does not show that a record exists. A string can pass that check and still point to no record, so run a lookup when you need to know the record is there.

What does escapeQueryTermSeparator change?

It protects the caret that ServiceNow uses as the query term separator. The example turns a single caret into two carets, so the value cannot be read as a separator when it is placed into an encoded query.

Does normalizeWhitespace remove line breaks?

It replaces carriage returns, line feeds and tabs with spaces, so a line break becomes a single space, and then it removes repeated spaces. If you want line breaks to show as breaks in HTML, use newLinesToBreaks instead.