Querying SAP entities

Server-side filtering, field selection, related entities and paging through large SAP result sets.

SAP systems push filtering, sorting and shaping of results to the server - the client describes what it wants in query options and only the matching data travels over the wire. SAP connections run on the OData implementation, so everything in the OData query options chapter applies to them - this page shows the options in SAP terms.

Keyword arguments

Each query option is a keyword argument to .read, .iter and .get, without the dollar prefix:

conn = self.sap['SAP.Sample']

partners = conn.read('A_BusinessPartner',
    filter="CityName eq 'London' and BusinessPartnerCategory eq '1'",
    select='BusinessPartner,BusinessPartnerName,CityName',
    orderby='BusinessPartnerName desc',
    top=20,
    skip=40,
)

The client encodes each option correctly for the connection's OData version - for instance, inline counts are requested with $inlinecount=allpages in V2 services and $count=true in V4 ones.

Selecting fields

SAP entities are wide - a business partner has dozens of properties. The select option keeps the payloads small by naming only the fields the service needs:

partners = conn.read('A_BusinessPartner',
    select='BusinessPartner,BusinessPartnerName',
)

Navigation properties - order items, partner addresses, employment records - come inline with the expand option, saving one round trip per relation:

orders = conn.read('A_SalesOrder',
    filter="SoldToParty eq '1000001'",
    expand='to_Item',
)

Paging

.read returns one page and .iter follows the server's paging links until the whole result set is exhausted - Gateway's skiptokens and SuccessFactors' __next links alike:

for partner in conn.iter('A_BusinessPartner', filter="Country eq 'DE'"):
    self.logger.info('Received -> %s', partner['BusinessPartner'])

A connection-wide page size can be set in the connection's form - it travels as Prefer: odata.maxpagesize and the server keeps each page at or below it.

SAP-specific parameters

Parameters outside of the OData standard, such as sap-client or sap-language, travel through the custom option as they are:

partners = conn.read('A_BusinessPartner',
    top=10,
    custom={'sap-client': '100', 'sap-language': 'EN'},
)

Learn more