Netweaver Gateway Development

how to implement Sorting($orderby) in SAP OData service?

             OData developers can make use of query option $orderby for sorting the resultset obtained by GET_ENTITYSET implementation.Order-by parameters are provided by the framework in Get_EntitySet method via the import table IT_ORDER. The table contains an entry for each property provided in the $orderby query option.
             For each property, you can decide if you want the collection to be sorted as ascending (suffix asc) or descending (suffix desc). Ascending is the default setting, so the  asc doesn’t need to be provided explicitly.
            In this tutorial we will sort the result of our GET_ENTITYSET result set mentioned above.
            
Syntax:
   https://<server>:<port>/.../<service_name>/Products?$orderby=Category desc  




Steps.

  1. Open our project and expand service implementation node and right click on GetEntitySet(Query) and click on Goto ABAP Workbench.
  2. Open our FLIGHTSET_GET_ENTITYSET method.Inside this method paste below code. it_order at runtime contain $orderby values.We can either read this internal table or acces it from io_tech_request_context.
  3.        data:lt_orderby type /iwbep/t_mgw_tech_order,
           ls_orderby type /iwbep/s_mgw_tech_order.
    
           lt_orderby = io_tech_request_context->get_orderby( ).
    
  4. Fetch data based on the $orderby parameter.Apply your own logic for better performance.
  5.    read table lt_orderby into ls_orderby index 1.
        if sy-subrc eq 0.
          if ls_orderby-order eq 'desc'.
            case ls_orderby-property.
              when 'CARRID'.
                select * from scarr into corresponding fields of table et_entityset order by carrid descending.
              when 'CARRNAME'.
                select * from scarr into corresponding fields of table et_entityset order by carrname descending.
              when 'CURRCODE'.
                select * from scarr into corresponding fields of table et_entityset order by currcode descending.
              when 'URL'.
                select * from scarr into corresponding fields of table et_entityset order by url descending.
            endcase.
          elseif ls_orderby-order eq 'asc'.
            case ls_orderby-property.
              when 'CARRID'.
                select * from scarr into corresponding fields of table et_entityset order by carrid.
              when 'CARRNAME'.
                select * from scarr into corresponding fields of table et_entityset order by carrname.
              when 'CURRCODE'.
                select * from scarr into corresponding fields of table et_entityset order by currcode.
              when 'URL'.
                select * from scarr into corresponding fields of table et_entityset order by url.
            endcase.
          endif.
        endif.   
    
  6. Now we can test  in gateway client( /IWFND/GW_CLIENT).Our service url look like this.http://<host>:<port>/sap/opu/odata/SAP/ZTEST_ODATA_SRV_01/FlightSet?$orderby=Carrname desc.Press execute button.
If you enjoyed this post, Please Share!!

0 comments :

Post a Comment

Powered by Blogger.