Orderby Odata Capability: Get The List of Products

Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 3

OrderBy ODATA Capability: It Sorts the data in the given order based on a

field. For this we need to consider the parameter ‘IT_ORDER’ of the method
‘<entityset>_get_entityset’.

As part of the stub ‘GETENTITYSET’ implementation, provide the following


code at the end for considering orderby capability.
method PRODUCTSET_GET_ENTITYSET.
*  Get the  List  of  Products
data t_product_header type table of bapi_epm_product_header.
call function 'BAPI_EPM_PRODUCT_GET_LIST'
 tables
   headerdata                  = t_product_header.
*  Map  the retrieved  product details to  the corresponding return  internal tabl
e
data : wa_entityset like line of et_entityset,
      wa_product_header like line of t_product_header.
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.

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.

Testing: HTTP method  Get


/sap/opu/odata/sap/ZEPM_PROJ_SRV/ProductSet?$orderby=ProductID desc

Enhanced code for ‘$orderby’ query option to identify the orderby fields
dynamically and also for multiple ‘orderby’ fields:

  method productset_get_entityset.

*  Get the  List  of  Products


    data t_product_header type table of bapi_epm_product_header.
    call function 'BAPI_EPM_PRODUCT_GET_LIST'
      tables
        headerdata = t_product_header.

*  Map  the retrieved  product details to  the corresponding return  internal tabl


e
    data : wa_entityset like line of et_entityset,
          wa_product_header like line of t_product_header.

    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:

HTTP method  Get

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

Case 3: (For Multiple Fields)

/sap/opu/odata/sap/ZEPM_PROJ_SRV/ProductSet?$orderby=ProductID
desc,Price desc

You might also like