servicenow

ServiceNow Encoded Query validation


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?


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'.