ServiceNow EncodedURIComponent not working
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-06-2024 08:54 AM - edited 11-06-2024 09:35 AM
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.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-07-2024 03:41 PM
@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);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-08-2024 09:34 AM
Thanks @Hitoshi Ozawa
Didn't work when I tried with GlideStringUtil.urlEncode.
Not working when condition have "%" and space.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-10-2024 03:06 PM
@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/497908/is-a-url-allowed-to-contain-a-space