/scripts/ahub.form.attachments.js
0

oData entity set selection with navigation

Dec 06, 2016 at 11:27 AM

243

avatar image
Former Member

In the Northwind odata model there is an association/navigation between Suppliers and Products entity sets. To get all products of supplier '1' in SAPUI5 i can use the following:

oModel.read("/Suppliers(1)/Products");

But what if I have a collection of suppliers and want to select all products for these suppliers. Should I loop at oModel.read ? What about performance? E.g.

var suppliers ["1", "2", "3", "4"];
for (var i=0;i<suppliers.length;i++){
oModel.read("/Suppliers("+ suppliers[i] +")/Products")
}

Or should I create a filter with all the suppliers and do:

oModel.read("/Products");

Along with passing filters for the entity set?

10 |10000 characters needed characters left characters exceeded
* Please Login or Register to Answer, Follow or Comment.

2 Answers

Best Answer
Krishna Kishor Kammaje
Dec 07, 2016 at 05:54 PM
0

This is what you require

http://services.odata.org/V4/Northwind/Northwind.svc/Suppliers?$filter=SupplierID eq 1 or SupplierID eq 2 or SupplierID eq 3 or SupplierID eq 4 or SupplierID eq 5&$expand=Products

Share
10 |10000 characters needed characters left characters exceeded
avatar image
Former Member Dec 06, 2016 at 11:23 PM
1

There is an OData option $expand, your code could be like this

oModel.read("/Suppliers?$expand=Products");

Show 5 Share
10 |10000 characters needed characters left characters exceeded
Former Member

The above will return all Suppliers along with their Products so two entity sets. I only want to retrieve Products entity set for Suppliers 1,2,3,4,5. Passing the filter to Products entity set would do the job but not sure if this is the best practice?

var aFilter = [];
for (var i = 0; i < oSuppliersData.entries.length; i++) {
aFilter.push(new Filter("SupplierID", FilterOperator.EQ, oSuppliersData.entries[i].SupplierID));
}
oModel.read("/Products", {filters: aFilter});
0

$expand is what you need as Seungchul Yang answered. Using $filter on parent entity is right if you want only few suppliers. That is what filters are made for.

0
Former Member
Krishna Kishor Kammaje

But his example will select all products of all suppliers. I have already selected my suppliers on one screen and on the other screen I want to select products belonging to these suppliers. So two separate read operations.

oModel.read("/Suppliers(1)/Products");

The above will only get me data from Products entity set for SupplierID '1' without returning data from the Suppliers as opposed to expand:

oModel.read("/Suppliers?$expand=Products");

which selects me data from both Suppliers and Products entity. So I want to achieve the same as the above code but for multiple already selected Suppliers.

Or is my understanding wrong?

0

This is what you require

http://services.odata.org/V4/Northwind/Northwind.svc/Suppliers?$filter=SupplierID eq 1 or SupplierID eq 2 or SupplierID eq 3 or SupplierID eq 4 or SupplierID eq 5&$expand=Products

0
Former Member
Krishna Kishor Kammaje

Thanks. Can you make it as an answer rather than a comment and I will mark it answered.

0
Skip to Content