SAP GW – Implement a better OrderBy for Cust EXT Class
If you have defined your OData Service in transaction SEGW and use the own class (* _DPC_EXT) for Service Implementation,
you may have asked yourself how to use the parameter IT_ORDER of Type /IWBEP/T_MGW_SORTING_ORDER to implement sorting of your entity table.
One way could be to use standard GW util class /IWBEP/CL_MGW_DATA_UTIL and its method orderBy.
But by using this, it might happen that you stumble upon a constraint where you get an exception if you use a parameter in your orderBy clause which has a underline in its name definition.
For example you would use this URL where StartDate is the attribute you want to sort descending:
The Problem is that ‘StartDate’ is the attribute name of your gateway service entity and in you backend table it could have the name ‘START_DATE’.
so ‘StartDate’ <> ‘START_DATE’
Because of the standard implementation of method orderBy you will get an Error : “RFC Error: Incorrect value in the dynamic table.”
To get arround this pittfall you could use your own util class to sort the table.
Where you can use o_tech_request_context object.
This object does the mapping to get the corresponding database field name (see comment of Ron).
It could look like this:
IT_ORDER TYPE /IWBEP/T_MGW_TECH_ORDER "*the sorting order CT_DATA TYPE STANDARD TABLE " * CT_DATA is your Entetyset table returning just after the select from db.
METHOD orderby. DATA: lt_otab TYPE abap_sortorder_tab, ls_oline TYPE abap_sortorder. DATA: ls_order LIKE LINE OF it_order. CONSTANTS:BEGIN OF lcs_sorting_order, descending TYPE string VALUE 'desc', ascending TYPE string VALUE 'asc', END OF lcs_sorting_order. LOOP AT it_order INTO ls_order. ls_otab-name = ls_order-property. IF ls_order-order = lcs_sorting_order-descending. ls_otab-descending = abap_true. ELSE. ls_otab-descending = abap_false. ENDIF. APPEND ls_otab TO lt_otab. ENDLOOP. SORT ct_data BY (lt_otab). ENDMETHOD.
And you can use this method like this:
IF it_order IS NOT INITIAL. "* get technical names for order table DATA: lt_tech_order TYPE /iwbep/t_mgw_tech_order. lt_tech_order = io_tech_request_context->get_orderby( ). zcl_cl_util=>orderby( EXPORTING it_order = lt_tech_order " the sorting order CHANGING ct_data = et_feed ). ENDIF.