Notifications

801 views

Description

 

Overview


Certain characters, if used in a JavaScript string, can generate unexpected and sometimes hard to identify errors in a script.  These Javascript strings can be used within various object types that support scripting in the ServiceNow platform, such as Business Rules, Workflow Scripts, ACL records and many other places in the application.  These special characters generally cause an issue because that character has a special significance to the JavaScript language when included in a command.

To allow these characters to thus be used in a String when necessary in the Javascript supported objects in ServiceNow, the character must be escaped (preceded by special characters telling the JavaScript compiler to handle the next character literally rather than attempt to interpret the character in the usual manner).

Method


The three main characters that users will probably need to use and which will require the escape character are the single quote character ('), the double quote character (") and the backslash character (\).

The reason these characters need special handling is because the single and double quote characters are used to begin and terminate a string in JavaScript and the backslash character is the escape character itself.

Escaping these characters is easy, simply place a preceding backlash character (\) in front of the character within the string which you want to have treated literally.  Thus, \' would allow usage of a single quote character, \" would allow usage of the double quote character and \\ would allow literal use of a backslash character in a string.

Example


As an example, the following two JavaScript statements will each generate an error due to of these special characters included within a string:

var userName = "Jerome "The Race" Anderson";

This statement would produce the following error message output:

Javascript compiler exception: missing ; before statement (null.null.script; line 1) in:
var userName = "Jerome "The Race" Anderson";
And, the following JavaScript statement, containing a special character in the string:
gs.log('Please retry the operation, the expected button wasn't found on the form.');

would generate the following error due to the special character found in the string:

Javascript compiler exception: missing ) after argument list (null.null.script; line 1) in:
gs.log('Please retry the operation, the expected button wasn't found on the form.'); 

The following statement would not necessarily generate an error, however, it would most probably not produce the expected results.

var filepath = "\\bzavr\extra\open\files\config.ini";

 The filepath variable, as stored in memory would actually contain the following string text:

\bzavrextraopenilesconfig.ini

To rectify these issues and allow these statements to work correctly, we would need to precede each of the special characters in the strings with the Javascript backslash escape character.

Thus, the previous statements could be re-written as the following to prevent any issues which might be introduced by including these special characters in a string:

var userName = "Jerome \"The Race\" Anderson";

gs.log('Please retry the operation, the expected button wasn\'t found on the form.');

var filepath = "\\\\bzavr\\extra\\open\\files\\config.ini";

Additional Information


Note also that the use of the backslash character can also be used to allow the inclusion of special, non-printable characters within a Javascript string.  The following shows the character combination and the non-printable character represented:

Escaped CharacterCharacter Representation 
 \b Backspace
 \f Form Feed
 \n New Line
 \r Carriage Return
 \t Horizontal Tab
 \v Vertical Tab

Thus, to include any of these non-printable characters in a Javascript string, the escaped character should be added (i.e. \n to include a new line).

For example the following statement,

var textData = "This is a test string.\n\rNext Line.\tShifted line.";

when printed, might show output similar to the following:

This is a test string.

Next Line.	Shifted line.

Article Information

Last Updated:2019-08-02 20:56:32
Published:2019-02-06