Skip to main content

filter

When operating on a collection of entities, the API offers filter( ... ) on the builders. It directly corresponds to the $filter parameter of the request.

Filters are built via the fields available on the schema property of the entities API class:

/*
Get all business partners that either:
- Have first name 'Alice' but not last name 'Bob'
- Or have first name 'Mallory'
*/
const { businessPartnerApi } = businessPartnerService();
const { or, and } = require('@sap-cloud-sdk/odata-v2');

businessPartnerApi
.requestBuilder()
.getAll()
.filter(
or(
and(
businessPartnerApi.schema.FIRST_NAME.equals('Alice'),
businessPartnerApi.schema.LAST_NAME.notEquals('Bob')
),
businessPartnerApi.schema.FIRST_NAME.equals('Mallory')
)
)
.execute(destination);

The EDM primitive types support all six comparison operators:

  • lessThan()
  • lessOrEqual()
  • equals()
  • notEquals()
  • greaterOrEqual()
  • greaterThan()

The example above will translate to this filter parameter:

$filter=(((FirstName eq 'Alice') and (LastName ne 'Bob')) or (FirstName eq 'Mallory'))

Take note of the order of and and or. As or is invoked on the result of and it will form the outer expression while and is an inner expression in the first branch of or.

In addition, the negation operator not can also be used for wrapping any filter expressions.

/*
Get all business partners that do not match any of the cases:
- Have first name 'Alice'
- Have last name 'Bob'
*/
.filter(
not(
or(
businessPartnerApi.schema.FIRST_NAME.equals('Alice'),
businessPartnerApi.schema.LAST_NAME.equals('Bob')
)
)
)

The $filter parameter will then be generated like below:

$filter=not (FirstName eq 'Alice') or (LastName eq 'Bob'))

It is also possible to pass multiple filters to the same filter function without concatenating them with and or or. They will be concatenated with and by default. The two following examples are equal:

.filter(
and(
businessPartnerApi.schema.FIRST_NAME.equals('Alice'),
businessPartnerApi.schema.LAST_NAME.notEquals('Bob')
)
)

The example above can be shortened to:

.filter(
businessPartnerApi.schema.FIRST_NAME.equals('Alice'),
businessPartnerApi.schema.LAST_NAME.notEquals('Bob')
)