Application Development and Automation Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

how to implement logical OR filter with $batch in OData

gbalto
Discoverer
0 Kudos
1,299

I'm trying to filter on two different properties and I've read this is not possible using just the $filter operator. I tried to concatenate the $filter with "&" like so:

https://myNNNNNN.crm.ondemand.com/sap/c4c/odata/v1/ticket/ServiceRequestCollection?$filter=LastChang... gt datetimeoffset'2023-09-29T00:00:00Z'&$filter=ServiceSupportTeamPartyID eq 'CT_CS_DE' or ServiceSupportTeamPartyID eq 'CT_CS_EN' or ServiceSupportTeamPartyID eq 'CT_CS_FR' or ServiceSupportTeamPartyID eq 'CT_CS_IT' or ServiceSupportTeamPartyID eq 'CT_CS_ES' or ServiceSupportTeamPartyID eq 'CT_CS_NL'

But then, I found out this is not possible from reading the dev guide: Logical OR only works for the same property. E.g. "...$filter=PartyID eq '1001' or PartyID eq '1002'" works. "...$filter=PartyID eq '1001' or TerritoryID eq 'CA'" not supported.
Suggested workaround: Each or segment can be executed as a seperate query, and the results can be collated. E.g. : 1st Query - " ...$filter=PartyID eq '1001'" 2nd Query - "$filter=TerritoryID eq 'CA'". In order to reduce round trips to the server, multiple queries can be executed as part of a $batch query.

So I'm trying to implement this with a $batch query in Postman following this blog post. I'm doing a POST request on "https://myNNNNNN.crm.ondemand.com/sap/c4c/odata/v1/ticket/$batch" and the body of the request is:

--batch
Content-Type: application/http
Content-Transfer-Encoding: binary
Accept: application/json


GET ServiceRequestCollection?$filter=LastChangeDateTime gt datetimeoffset'2023-09-30T00:00:00Z' HTTP/1.1


--batch
Content-Type: application/http
Content-Transfer-Encoding: binary
Accept: application/json


GET ServiceRequestCollection?$filter=ServiceSupportTeamPartyID eq 'CT_CS_DE' or ServiceSupportTeamPartyID eq 'CT_CS_EN' or ServiceSupportTeamPartyID eq 'CT_CS_FR' or ServiceSupportTeamPartyID eq 'CT_CS_IT' or ServiceSupportTeamPartyID eq 'CT_CS_ES' or ServiceSupportTeamPartyID eq 'CT_CS_NL'&$format=json HTTP/1.1


--batch--

The response is:

<?xml version="1.0" encoding="utf-8"?><error xmlns="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata"> <code/> <message xml:lang="en">The server is refusing to process the request because the entity has a unsupported format</message></error>
1 ACCEPTED SOLUTION
Read only

RalfHandl
Product and Topic Expert
Product and Topic Expert
1,197

You can't specify more than one $filter query option.

You may try using "and" between the the two parts of the filter expression assuming you want entities matching one of the party ids and are newer than the timestamp:

$filter=LastChangeDateTime gt datetimeoffset'2023-09-29T00:00:00Z' and (ServiceSupportTeamPartyID eq 'CT_CS_DE' or ServiceSupportTeamPartyID eq 'CT_CS_EN' or ServiceSupportTeamPartyID eq 'CT_CS_FR' or ServiceSupportTeamPartyID eq 'CT_CS_IT' or ServiceSupportTeamPartyID eq 'CT_CS_ES' or ServiceSupportTeamPartyID eq 'CT_CS_NL')

This would not require a batch request.

3 REPLIES 3
Read only

evanireland
Product and Topic Expert
Product and Topic Expert
0 Kudos
1,197

Maybe check OData Version 4.01. Part 1: Protocol (oasis-open.org) (Batch Request Headers)

Read only

RalfHandl
Product and Topic Expert
Product and Topic Expert
1,198

You can't specify more than one $filter query option.

You may try using "and" between the the two parts of the filter expression assuming you want entities matching one of the party ids and are newer than the timestamp:

$filter=LastChangeDateTime gt datetimeoffset'2023-09-29T00:00:00Z' and (ServiceSupportTeamPartyID eq 'CT_CS_DE' or ServiceSupportTeamPartyID eq 'CT_CS_EN' or ServiceSupportTeamPartyID eq 'CT_CS_FR' or ServiceSupportTeamPartyID eq 'CT_CS_IT' or ServiceSupportTeamPartyID eq 'CT_CS_ES' or ServiceSupportTeamPartyID eq 'CT_CS_NL')

This would not require a batch request.

Read only

0 Kudos
1,197

wow okay - this is the first thing I tried to do but I was missing the brackets and couldn't make it work. thanks so much for the help, your solution works like a charm.