Issue
Certain characters, if used in a JavaScript string, can generate unexpected","articleBody":"
ServiceNow JavaScript escape characters
Issue
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.
\r\nTo 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).
\r\nMethod
\r\nThe 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 (\\).
\r\nThe 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.
\r\nEscaping 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.
\r\nExample
\r\nAs an example, the following two JavaScript statements will each generate an error due to of these special characters included within a string:
\r\nvar userName = "Jerome "The Race" Anderson";
\r\nThis statement would produce the following error message output:
\r\nJavascript compiler exception: missing ; before statement (null.null.script; line 1) in:\r\nvar userName = "Jerome "The Race" Anderson";
\r\nAnd, the following JavaScript statement, containing a special character in the string:
\r\ngs.log('Please retry the operation, the expected button wasn't found on the form.');
\r\nwould generate the following error due to the special character found in the string:
\r\nJavascript compiler exception: missing ) after argument list (null.null.script; line 1) in:\r\ngs.log('Please retry the operation, the expected button wasn't found on the form.');
\r\nThe following statement would not necessarily generate an error, however, it would most probably not produce the expected results.
\r\nvar filepath = "\\\\bzavr\\extra\\open\\files\\config.ini";
\r\nThe filepath variable, as stored in memory would actually contain the following string text:
\r\n\\bzavrextraopenilesconfig.ini
\r\nTo 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.
\r\nThus, 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:
\r\nvar userName = "Jerome \\"The Race\\" Anderson";\r\n
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
\r\n\r\n
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:
\r\nEscaped Character | Character 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).
\r\nFor example the following statement,
\r\nvar textData = "This is a test string.\\n\\rNext Line.\\tShifted line.";
\r\nwhen printed, might show output similar to the following:
\r\nThis is a test string.\r\nNext Line.\tShifted line.