Question

I intend to let users specify encoded query in ServiceNow SOAP requests.

The problem is that if user specifies invalid query string (e.g. "?#@" or "sometext"), ServiceNow do not return any exception, but every row or no rows at all.

Is there a way to check validity of encoded query via webservice?

Was it helpful?

Solution

Fandic,

If you know ahead of time which table the SOAP query will be running against (or can get that information from the user when they submit the query) you can set up your own web service exclusively for checking the validity of a query string.

GlideRecord has the "addedEncodedQuery" method for putting in encoded queries, and it has a getEncodedQuery for turning the current query into an encoded string. If you instantiate a GlideRecord object, and pass it an invalid query string like this:

var myGR = new GlideRecord('incident');
    myGr.addEncodedQuery('invalid_field_foo=BAR');

You can then call getEncodedQuery out to see if it is valid:

var actual_query = myGR.getEncodedQuery(); //sys_idNotValidnull

You shouldn't do simple comparison between the input and the output, as the API doesn't guarantee that a valid query will be returned as the exact same string as entered. It's better to simply validate that the actual_query does not equal 'sys_idNotValidnull'.

OTHER TIPS

I always recommend enabling the system property 'glide.invalid_query.returns_no_rows' to avoid this effect. The property does just what it says. I never understood why you'd ever want to return all rows for an invalid query. I've seen a number of cases where developers had a query defect and never knew it since rows were coming back.

You can use the below code so that query will return the result only when it correct.

gs.getSession().setStrictQuery(boolean);

This overrides the value of system property :

glide.invalid_query.returns_no_rows

Reference : https://docs.servicenow.com/bundle/istanbul-platform-administration/page/administer/reference-pages/reference/r_AvailableSystemProperties.html

Check the property value glide.invalid_query.returns_no_rows. If it's true, the invalid query returns no rows. If false (or not available) it is ignored.

This can be overridden with gs.getSession().setStrictQuery(boolean); on a script-by-script basis.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top