Encoded Query not working when it contains non-URL Safe characters

pault1
Kilo Contributor

Trying to query SN table via REST API and discovered that when I pass in a sysparm_query that contains non-URL Safe special characters, matching records are not returned.

Query Example:

"u_tag_source=Hosting^u_key=!@#$%^&*() A tag"

When I try to do the same thing in the table's List view, I get the same result.

First image is with a single filter on u_tag_source:

Capture1.JPG

Second image is by right-clicking the u_key column on the first record and selecting "Show Matching".

Capture2.JPG

Notice that the filter added cuts off everything after the percentage sign. And when I add that filter, the URL is set to:
nav_to.do?uri=/u_configuration_item_tag_list.do%3Fsysparm_query%3Du_tag_source%253DHosting%255Eu_key%253D!%2540%2523%2524%2525%255E%2526*()%2520A%2520tag%26sysparm_first_row%3D1%26sysparm_view%3D

I think the filter is not being URL encoded, which explains why this doesn't work in the UI, but

When I URLEncode the filter value and submit via REST API, this is what I am submitting:

"u_tag_source=Hosting^u_key=!%40%23%24%25%5e%26*()+A+tag"

This still doesn't work. Anyone seen this behavior and found a solution?

16 REPLIES 16

bernyalvarado
Mega Sage

Hi Paul,



Thanks for sharing the context on which you're using it. Indeed it has to be an exact match.



Good news is that I found a way to make it an exact match. Just use an addQuery instead of an EncodedQuery



Here goes an example of the code:



var gr = new GlideRecord('incident');


gr.addQuery('short_description','=','!@#$%%^&*');


gr.query();


if (gr.next()){


      gs.print(gr.short_description);


}



Result:



*** Script: !@#$%%^&*



Thanks,


Berny


bernyalvarado
Mega Sage

I hope this helps!



Thanks,


Berny


divya mishra
Tera Guru

var gr = new GlideRecord('incident');


gr.addQuery('short_description','CONTAINS','!@#$%%^&*');


gr.query();


if (gr.next()){


      gs.print(gr.short_description);


}


bernyalvarado
Mega Sage

Hi pault, do you have any further questions? Were you able to apply the recommendation of using addQuery?



As mentioned previously with an addQuery you can do an equal comparison (=) so that it only matches the values that you're looking for and there's not a need to escape any characters.



Please let us know if you have any further questions. If not, we will appreciate if you can mark the responses as helpful and correct so that we can close this thread.



Thanks,


Berny


I can't use the addQuery method, since I can't use the GlideRecord query, I am doing a GET via SN REST API, so you have to pass an encoded query string.