Orderby Odata Capability: Get The List of Products
Orderby Odata Capability: Get The List of Products
Orderby Odata Capability: Get The List of Products
field. For this we need to consider the parameter ‘IT_ORDER’ of the method
‘<entityset>_get_entityset’.
if it_order is not initial.
* Logic for Orderby Query option
data wa_order like line of it_order.
read table it_order into wa_order
with key property = 'ProductID'.
if sy-subrc eq 0.
case wa_order-order.
when 'asc'.
sort et_entityset by productid.
when 'desc'.
sort et_entityset by productid descending.
endcase.
endif.
endif.
endmethod.
Enhanced code for ‘$orderby’ query option to identify the orderby fields
dynamically and also for multiple ‘orderby’ fields:
method productset_get_entityset.
loop at t_product_header into wa_product_header.
clear wa_entityset.
wa_entityset-productid = wa_product_header-product_id.
wa_entityset-typecode = wa_product_header-type_code.
wa_entityset-category = wa_product_header-category.
wa_entityset-name = wa_product_header-name.
wa_entityset-description = wa_product_header-description.
append wa_entityset to et_entityset.
endloop.
* Retrieve Order By Fields Dynamically
data : t_order type /iwbep/t_mgw_tech_order,
wa_order like line of t_order.
call method io_tech_request_context->get_orderby
receiving
rt_orderby = t_order.
if t_order is not initial.
* extract orderby fields
data : t_sortorder type abap_sortorder_tab,
wa_sortorder like line of t_sortorder.
loop at t_order into wa_order.
wa_sortorder-name = wa_order-property.
if wa_order-order = 'desc'.
wa_sortorder-descending = 'X'.
else.
wa_sortorder-descending = ' '.
endif.
append wa_sortorder to t_sortorder.
endloop.
* sort final internal table by dynamically identified orderby fields
sort et_entityset by (t_sortorder).
endif.
endmethod.
Testing:
Case 1:
/sap/opu/odata/sap/ZEPM_PROJ_SRV/ProductSet?$orderby=Name desc
Case 2:
/sap/opu/odata/sap/ZEPM_PROJ_SRV/ProductSet?$orderby=ProductID desc
/sap/opu/odata/sap/ZEPM_PROJ_SRV/ProductSet?$orderby=ProductID
desc,Price desc