cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

S/4HANA API for A_JournalEntryItemBasic changes order of results if $select fields

litox
Explorer
0 Kudos
615

When I query using the REST API the A_JournalEntryItemBasic resource of the API_JOURNALENTRYITEMBASIC_SRV service, if I change the list of fields to retrieve, using the $select parameter, then the order of the results change. Even if I add an $orderby !

For example, I expect that a GET request to this two URLs return the same record (same ID, same URI) but represented with a different set of fields. Because I'm passing `$top=1` so only the first result should be returned:

  1. GET https://sandbox.api.sap.com//s4hanacloud/sap/opu/odata/sap/API_JOURNALENTRYITEMBASIC_SRV/A_JournalEntryItemBasic?%24top=1&%24select=ID%2CLedger%2CLedgerName%2CSourceLedger%2CLedg
    erFiscalYear%2CControllingArea%2CControllingAreaName%2CCompanyCode%2CCompanyCodeName%2CGLAccount%2CGLAccountName&%24orderby=Ledger
  2. GET https://sandbox.api.sap.com//s4hanacloud/sap/opu/odata/sap/API_JOURNALENTRYITEMBASIC_SRV/A_JournalEntryItemBasic?%24top=1&%24select=WBSElement%2CWBSElementDescription%2CPartnerCo
    mpanyCode%2CPartnerCompanyCodeName%2CCostCtrActivityType%2CCostCtrActivityTypeName%2COrderID%2CLedger&%24orderby=Ledger

But two different URIs are returned. First:

{'__metadata': {'id': "https://sandbox.api.sap.com/s4hanacloud/sap/opu/odata/sap/API_JOURNALENTRYITEMBASIC_SRV/A_JournalEntryItemBasic('.1~0C.3~0L.4~2018.5~A000.7~1010.9~0000000001')",
  'uri': "https://sandbox.api.sap.com/s4hanacloud/sap/opu/odata/sap/API_JOURNALENTRYITEMBASIC_SRV/A_JournalEntryItemBasic('.1~0C.3~0L.4~2018.5~A000.7~1010.9~0000000001')",
  'type': 'API_JOURNALENTRYITEMBASIC_SRV.A_JournalEntryItemBasicType'},
 'ID': '.1~0C.3~0L.4~2018.5~A000.7~1010.9~0000000001',
 'Ledger': '0C',
 'LedgerName': 'Management Accounting',
 'SourceLedger': '0L',
 'LedgerFiscalYear': '2018',
 'ControllingArea': 'A000',
 'ControllingAreaName': 'Controlling Area A000',
 'CompanyCode': '1010',
 'CompanyCodeName': 'BestRun DE',
 'GLAccount': '1',
 'GLAccountName': 'Paybls Domestic'}

And second:

{'__metadata': {'id': "https://sandbox.api.sap.com/s4hanacloud/sap/opu/odata/sap/API_JOURNALENTRYITEMBASIC_SRV/A_JournalEntryItemBasic('.1~0C.71~.73~.75~.77~')",
  'uri': "https://sandbox.api.sap.com/s4hanacloud/sap/opu/odata/sap/API_JOURNALENTRYITEMBASIC_SRV/A_JournalEntryItemBasic('.1~0C.71~.73~.75~.77~')",
  'type': 'API_JOURNALENTRYITEMBASIC_SRV.A_JournalEntryItemBasicType'},
 'Ledger': '0C',
 'WBSElement': '',
 'WBSElementDescription': '',
 'PartnerCompanyCode': '',
 'PartnerCompanyCodeName': '',
 'CostCtrActivityType': '',
 'CostCtrActivityTypeName': '',
 'OrderID': ''}

The OData specification says:

Select System Query Option ($select): A data service URI with a $select System Query Option identifies the same set of entities as a URI without a $select query option; however, the presence of a $select query option specifies that a response from the data service SHOULD return a subset, as identified by the value of the $select query option, of the properties that would have been returned had the URI not included a $select query option.

What am I missing?

 

UPDATE:
Apparently what I take as a row ID is actually a GENERATED_ID : a special property name used in OData services to uniquely identify aggregated or grouped data records. When you request aggregated data in an OData service, a GENERATED_ID property is automatically created for each resulting group. This ID serves as a unique key for that aggregated record, allowing you to request the same aggregated data again by referencing this ID.

So now, I don't know how to identify rows and thus how to match two rows from different queries. Can I rely on the order of the results?

View Entire Topic
Chuma
Active Contributor
0 Kudos

Hello @litox 

Your  first row changes because your $orderby is not unique, so $stop=1 can select a different tied row when $select changes. To make the sorting consistent, include the entity’s key fields or the generated ID) in both $select and $orderby,such as &$orderby=CompanyCode,FiscalYear,AccountingDocument,Ledger,AccountingDocumentItem. Even better, filter directly by the business key instead of relying on $top=1.

Check your inbox for detailed code

With regards

Chuma

litox
Explorer
0 Kudos
How can I know what's the entity fields that form a primary key?