String field not populating the special characters(/)(\\)

Vamshi_ch123
Tera Contributor

Hi i have string variable in a catalog item and I'm setting the value by using below code

g_form.setValue('home_directory' , '\\XXXXX.XXX.XXXX.XXX\share\homedrive');

but it is populating like a \XXXXX.XXX.XXXX.XXXsharehomedrive. without forward slash and backward slash.

How to fix this?

 

Thanks in Advance

Sravani

1 ACCEPTED SOLUTION

Mike_R
Kilo Patron
Kilo Patron

You'll need to add another \

g_form.setValue('home_directory' , "\\\\XXXXX.XXX.XXXX.XXX\\share\\homedrive");

 

see this

https://support.servicenow.com/kb?id=kb_article_view&sysparm_article=KB0721958

Description

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.

View solution in original post

2 REPLIES 2

Mike_R
Kilo Patron
Kilo Patron

You'll need to add another \

g_form.setValue('home_directory' , "\\\\XXXXX.XXX.XXXX.XXX\\share\\homedrive");

 

see this

https://support.servicenow.com/kb?id=kb_article_view&sysparm_article=KB0721958

Description

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.

Pratik Malviya
Tera Guru

Hi @Vamshi_ch123 ,

 

Use below code:- 

g_form.setValue('home_directory' , "\\\\XXXXX.XXX.XXXX.XXX\\share\\homedrive");

 

Note: use 4 slash (\\\\);

 

if it helps you, please mark it correct answer.

 

Please mark the appropriate response as correct answer and helpful, This may help other community users to follow correct solution.
Thanks,
Pratik Malviya