I've seen two post about the URL convention, but my question is specific to the SAP's Gateway implementation for OData web services assuming. When trying to use $filter
in combination with $expand
we get the error message:
Left hand expression of memberaccess operation has wrong cardinality
Assuming I have two simple entities:
Foo
* Key
- Value
Bar
* Key
* Id
- Value
Foo has a 1:n association to Bar
. The following URL works as intended.
/sap/opu/odata/sap/ZTEST_SRV/Foo?$expand=Bar
As does
/sap/opu/odata/sap/ZTEST_SRV/Foo?$filter=Key gt 10&$expand=Bar
When trying to using $filter
on entity Bar
property Id
we get the error message.
/sap/opu/odata/sap/ZTEST_SRV/Foo?$filter=Key gt 10 and Bar/Id gt 2&$expand=Bar
Is it possible to use a $filter
in this way with SAP? Related articles below.
ODATA / SAP Gateway: About Query with $filter and $expand simultaneously
Filter on Expanded entities in OData
OData query option $expand is used to read multiple entities or entity sets in a single service call instead of two different calls. Prerequisite, entity sets which are used should be associated. To know about Association in OData service click here.
I created simple oData model for SAP user details, I implemented two methods: get_entityset - receives list of users with personal number and full name. get_entity - receives more details of single user (by username).
Go to Runtime Artifacts node, open the ZCL_ZGW_PRACTICE006_MPC_EXT class in ABAP Workbench(Right-Click: Go to ABAP Workbench) & click on the Types tab. Click on the Change(Ctrl+F1) button for editing. Click on the Direct Type Entry button. Then, create the deep structure & activate.
I am also facing similar problems, there is this sap note: https://apps.support.sap.com/sap/support/knowledge/en/3008698
Copy of the note details:
After adding a filter
$filter = To_Employees/Name eq 'Hugo' to an Odata service, there is an error:
"Left hand expression of memberaccess operator has wrong cardinality (to many not allowed)"
SAP_GWFND 750
Access the odata service: /sap/opu/odata/sap/API_XXX_SRV/Orgnization$select=Orgnization,to_Employees/Name&$expand=to_Employees&$filter=to_Employees/Name eq 'Hugo'
There is an error: "Left hand expression of memberaccess operator has wrong cardinality (to many not allowed)"
"Left hand expression of memberaccess operator has wrong cardinality (to many not allowed)" indicates that "to_Employees"is a to-many navigation, and this is not supported in combination with a filter expression (e.g.
$filter = To_Employees/Name eq 'Hugo',
when "To_Employees" points to more than one employee ).
Therefore, the whole request is invalid.
Remove the filter, do not combine to many results with filter eq
I don't like the solution presented :)
The workaround solution I have implemented was to create an entity which has cardinality 1:1 just for filtering. In your example, I assume you need the cardinaliy of the relationship from Foo to Bar to be 1:n in order to be able to receive multiple Bar records for each Foo found. If the relationship Foo to Bar is 1:1, you can simply change the cardinality and you are good to go. If you need cardinality 1:n, you may create an entity like BarFilter which is related to Foo with 1:1 cardinality. Then you would be able to execute this request without errors and can receive the filters to make the corresponding query on your backend system. The request would be something like:
New simple entity:
BarFilter
Foo has a 1:1 association to new entity BarFilter.
Request:
/sap/opu/odata/sap/ZTEST_SRV/Foo?$filter=Key gt 10 and BarFilter/Id gt 2&$expand=Bar
I hope this is clear and that it helps you. I am very interested in this topic so if you find something interesting, please share it.
Have a nice weekend.
Cheers
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With