SOAP Direct Web Services Query

I’ve been working on integrations recently, in particular SOAP and utilising the out of the box direct web services as much as possible.

One of the things that I couldn’t figure out when querying to search for records, is how to do advanced searches without using the __encoded_query element. The reason I was looking for a simpler method was that I was automating the interface from another system and making this other system generate the encoded query wasn’t always the cleanest way of doing it.

If we look at an example incident query where we want to get all records which have a state of 3. Quite simply, the SOAP envelope would be:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:sys="http://www.service-now.com/incident">
<soapenv:Header/>
<soapenv:Body>
<sys:getRecords>
<state>3</element>
</sys:getRecords>
</soapenv:Body>
</soapenv:Envelope>

So far so good, now what if we wanted to find all incidents where state was NOT 3? Well one way is using an encoded query:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:sys="http://www.service-now.com/incident">
<soapenv:Header/>
<soapenv:Body>
<sys:getRecords>
<__encoded_query>state!=3</__encoded_query>
</sys:getRecords>
</soapenv:Body>
</soapenv:Envelope>

However what annoyed me is that for this other system where I was dynamically building these envelopes and making the call, was sometimes I needed to point to the state tag and sometimes to the encoded query tag. It just felt messy to be.

I since discovered that you can actually do these queries using the normal fields instead of the __encoded_query.

You can preprend the value you’re searching for with a number of operators. A list of these are at the bottom. If you’re searching for state equal to 3, you would do:

<state>3</state>

And now, if you’re searching for all states not equal to 3, with a slight ammendement, it would work as follows:

<state>^NE^3</state>

All we added was ^NE^ to the beginning and ServiceNow deals with the rest.

The full list of operators that you can add to the beginning of a query are:

Operator addQuery Equivalent String Replacement
Not equal to != or <> NE
Less than < LT
Less than or equal to <= LTE
Greater than > GT
Greater than or equal to >= GTE

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s