首页 > SAP > ODATA > $orderby Query Option in SAP OData Service
2019
08-29

$orderby Query Option in SAP OData Service

Hello everyone, in this tutorial we will learn how to use query option $orderby in SAP netweaver gateway OData service. Before proceeding further we assume that you know how to build OData service in sap gateway. Access all SAP Netweaver Gateway tutorials here.Lets get started

1.What $orderby query option does ?

Query option $orderby adds sorting capabilities to the SAP gateway OData service. So that you can able to order the OData service feed/collection based on the fields available in the Entity Type set.

2.Supported System Version

SAP NetWeaver Gateway Release-采购向厂商下P/O之后,必须透过一定权限经由主管对此张P/O做 release后才能收料 2.0 Support Package >=03

3.Business Example

A gateway OData service is getting all the products from the back-end system, but you don’t want to see the products in the order they retrieved and you want to apply orderby on the OData service so that you get required products in required order. For example you want to  order the all products by its Price in descending.

4.Syntax

http://<server>:<port>/sap/opu/odata/sap/<service_name>/ProductsSet?$orderby=Price desc

$orderby = <fieldname> <sortorder> , where:

<fieldname> is the name of the field.

<sortorder> desc/asc

5.How to Implement $orderby query option in OData Service

In this section we will adjust the OData service to respond to $orderby query option. $orderby query option should be applied only to Entity Type sets. We assume that you have already build the OData service to get the list of products. if you have not click here.

1. After successful creation of OData service. Go back to service builder SEGW, Navigate to the ABAP workbench for the Product Entity set.

2. Code inside the method PRODUCTSSET_GET_ENTITYSET will look like below i.e before adjusting the service to $orderby query option.

3. In the above code snippet, we have retrieved the list of products generally by providing the max rows.Now we will adjust the code to adapt it to the $orderby query option. Orderby query option is available in the method by accessing the importing parameter IT_ORDER.

4. Put the below code in the method PRODUCTSSET_GET_ENTITYSET to respond to the $orderby query option.

5. What we did in the above step.First we got all the products and get orderby fieldname, sortorder and pass apply them on to the BAPI result data to get the required sort order of the data.

6. From code perspective we are ready, so lets test the service by using the sap gateway client /IWFND/GW_CLIENT.

7. If we execute the service without adding $orderby query option, then we will get all the products available in the system. If we add the $orderby query option to the service our service will respond to that and it will give the results based on the sort order provided. Test the service by providing the following URI with $orderby=Price desc.

/sap/opu/odata/sap/ZDEMO_GW_SRV_SRV/ProductsSet?$orderby=Price desc’.

8. Check the collection/feed, now you should get the products in descinding order of the Price . You can add N number fields with $orderby query option.

Stay tuned to us for more SAP Netweaver Gateway tutorials. Please feel free to comment and let us know your feedback.

In step-4 i statically declaring the sort field name and order, you can also do it more dynamically in this way. Replace the code in step-4 with the below code.

最后编辑:
作者:yangsen
本站为个人博客网站,全由我个人维护,我从事SAP开发13年,其它ERP开发7年,基本都是零售行业。本站记录工作学习的过程, 有SAP相关询问专、兼职工作可随时联系我。 有网站相关的问题可直接在文章下方留言,或者联系我。 邮件:yan252@163.com给我。 QQ:415402519