We've updated the ServiceNow Community Code of Conduct, adding guidelines around AI usage, professionalism, and content violations. Read more

ServiceNow EncodedURIComponent not working

Swapna Patiband
Tera Contributor

Hello,

Need help with generating preview link with special characters(/,%,-,/,_ etc.,) on a custom form.

Preview link is not opening when condition is having special characters.

 

Thanks,

Swapna.

7 REPLIES 7

@Swapna Patiband Haven't checked if this works in your situtation or not, but instead of encodedURIComponent() try using GlideStringUtil.urlEncode().

var encodedCondition = GlideStringUtil.urlEncode(condition);

 

Thanks @Hitoshi Ozawa 

Didn't work when I tried with GlideStringUtil.urlEncode.

Not working when condition have "%" and space.

@Swapna Patiband Tried it.

var str = "abc%def ghi";
var encodedStr = GlideStringUtil.urlEncode(str);
var jsencode = encodeURIComponent(str);
gs.print("original string:   " + str);
gs.print("encoded string:    " + encodedStr);
gs.print("encodeURIComponent:" + jsencode);

Execution result:

*** Script: original string:   abc%def ghi
*** Script: encoded string:    abc%25def+ghi
*** Script: encodeURIComponent:abc%25def%20ghi

GlideStringUtil.urlEncode() replaces "%" with "%25" and a space with "+".

encodeURIComponent() replaces "%" with "%25" and a space with "%20".

 

Would need a concrete example of where it is not encoding properly.

 

FYR:

https://stackoverflow.com/questions/17342671/pass-a-percent-sign-in-a-url-and-get-exact-value-of-it-...

https://stackoverflow.com/questions/497908/is-a-url-allowed-to-contain-a-space