How to include range of values in Aggregate API URL query using key-value pairs?

adsch
Kilo Explorer

In querying records via the Aggregate API, it is possible to specify a range of values when using the sysparm_query parameter.

For example, a query for active incidents that are in a state of 12 or 2 would be (instance).service-now.com/api/now/stats/incidents?sysparm_count=true&sysparm_query=active=true^stateIN12,2.

However, when doing the same query using key-value pairs, this doesn't work. I thought it would be (instance).service-now.com/api/now/stats/incidents?sysparm_count=true&active=true&state=12&ORstate=2, since that's what the filter is in the ServiceNow instance. But this only filters for the first part, state=12, so it's not working.

What is the proper way to write this query using key-value pairs? I need to use key-value pairs in this case because it allows for using display values for fields, which is necessary for another part of the particular query I am using.

I know I could group by the state field and retrieve the count values for each of the desired states in my code, but it would be a lot easier to do this in one query as when using the sysparm_query.

1 ACCEPTED SOLUTION

brian_quinn
ServiceNow Employee
ServiceNow Employee

Sorry I misunderstood you.   Unfortunately as far as I can tell the REST API key/value pair filtering does not currently support OR conditions.   It looks like it is generating a list of URL parameters (separated buy the &) and passing those parameters directly into a addQuery(key, value) function call.  



So the URL key1=value1&key2=value2&ORkey3=value3 turns into:


addQuery("key1", "value1");


addQuery("key2", "value2");


addQuery("ORkey3", "value3")



You can use a combination of key/value pairs and sysparm_query to filter by the display values of some fields, but unfortunately it doesn't look like there is a way to add OR conditions using display values.


(ie api/now/stats/incident?sysparm_count=true&sysparm_query=active=true^ORactive=false&assignment_group=Software)



Thanks


Brian


View solution in original post

5 REPLIES 5

brian_quinn
ServiceNow Employee
ServiceNow Employee

The 2nd URL you provided is missing the 'sysparm_query=' and the conditions should be seperated by a '^' not a '&'.



Try this URL instead:


(instance).service-now.com/api/now/stats/incident?sysparm_count=true&sysparm_query=active=true^state=2^ORstate=6



On my instance that produces the exact same results as:


(instance).service-now.com/api/now/stats/incident?sysparm_count=true&sysparm_query=active=true^stateIN12,2



Thanks
Brian


My question is specifically about key-value pairs, which omits the sysparm_query and uses & instead of ^ for separators.



Please refer to the ServiceNow wiki article about key-value pairs. http://wiki.servicenow.com/index.php?title=Aggregate_API#Methods


brian_quinn
ServiceNow Employee
ServiceNow Employee

Sorry I misunderstood you.   Unfortunately as far as I can tell the REST API key/value pair filtering does not currently support OR conditions.   It looks like it is generating a list of URL parameters (separated buy the &) and passing those parameters directly into a addQuery(key, value) function call.  



So the URL key1=value1&key2=value2&ORkey3=value3 turns into:


addQuery("key1", "value1");


addQuery("key2", "value2");


addQuery("ORkey3", "value3")



You can use a combination of key/value pairs and sysparm_query to filter by the display values of some fields, but unfortunately it doesn't look like there is a way to add OR conditions using display values.


(ie api/now/stats/incident?sysparm_count=true&sysparm_query=active=true^ORactive=false&assignment_group=Software)



Thanks


Brian


Ah, okay. Bummer. Thank you for the answer!