Vishal
Vishal

Reputation: 2195

ServiceNow REST API - Querying tables - grouping conditions

I need to query tables in ServiceNow via its REST API while using multiple conditions grouped as following:

( (Condition1 AND Condition2) OR (Condition3 and Condition4) ) AND Condition5 AND Condition6

Does anyone know if this is possible, and if so, how? I've looked into the documentation but I'm not able to understand if it explains how to solve my problem.

Edit 1: I forgot to mention that I did try using parenthesis in my REST calls but it has not worked.

Thanks,

Upvotes: 0

Views: 4429

Answers (2)

vladzzoni
vladzzoni

Reputation: 11

The easiest method that I found out is to go to Studio and go to your table.

Show all records from table.

There you create the filter with your conditions by clicking filter icon.

Run the filter.

Then copy query from created hyperlink (next to filter icon) with right click -> Copy query. You can also copy the whole URL. It will look like this:

sys_id=10005^devicetype=test^sys_created_by=admin^ORsys_created_by=user

Upvotes: 1

tolsen64
tolsen64

Reputation: 999

The only way I've figured out how to do it is using the ^NQ (New Query) operator.

In SQL the query would look like this:

WHERE [state] IN (16,17) AND ([assigned_to] = 'value1' OR [assignment_group] IN ('x','y','z'))

So my query looks like this:

sysparm_query=stateIN16,17^assigned_to=value1^NQstateIN16,17^assignment_groupINx,y,z

So you're actually duplicating the common filter (state) and using different filters on each side of the ^NQ.

I guess you could say it would be the equivalent of a UNION in SQL.

SELECT * FROM [wm_task] WHERE [state] IN (16,17) AND [assigned_to] = 'value1'
UNION
SELECT * FROM [wm_task] WHERE [state] IN (16,17) AND [assignment_group] IN ('x','y','z')

Upvotes: 2

Related Questions