Escaping / encode / decode string within a hint

jamesmcwhinney
Giga Guru

Below is the server side script for a widget.

I am receiving the shortDescription from the client controller.

All works well until the supplied input.shortDescription contains a single quote (eg. "James's new laptop and phone").

What is the best way to escape/encode/decode the string in this situation?

 

var cart_item = new GlideRecord('sc_cart_item');
cart_item.addQuery('cart', "" + cartJS.getCartID());
gs.log('looking for items in cart:' + cartJS.getCartID());
cart_item.query(); 
if(cart_item.next()) {
	cart_item.hints = "<hints><entry key='sysparm_processing_hint' value='setfield:request.short_description=" + input.shortDescription + "'/></hints>";
	cart_item.update(); 
}

Thanks!

 

1 ACCEPTED SOLUTION

jamesmcwhinney
Giga Guru

Looking at https://community.servicenow.com/community?id=community_question&sys_id=35298be1db5cdbc01dcaf3231f961948 it seems like I have to write a business rule to decode it on insert..

View solution in original post

5 REPLIES 5

dvp
Mega Sage
Mega Sage

You need to use escape(\) character when there is ' character.

Replace ' with \'

Ex: James\'s new laptop and phone

Thanks, yeah I tried that but ended up with the following in the resulting service request:

 

find_real_file.png

I end up with %27 instead of the single quote.

Can you share the script that you are using for encoding?

jamesmcwhinney
Giga Guru

Looking at https://community.servicenow.com/community?id=community_question&sys_id=35298be1db5cdbc01dcaf3231f961948 it seems like I have to write a business rule to decode it on insert..