If you want to give the $orderby property via an OData service an automatically sort function, you can simply use a small piece of code, to adjust the entity set, which will be later returned to the OData gateway classes in ABAP.
You can see the IT_ORDER importing parameter in the signature of the GET_ENTITYSET method of your entity gateway class.

We can use this parameter to check, if an order is set by the request and if so, fetching it with the io_tech_request_context object.
Note that here our lt_result variable is the entityset, which we want to return before we convert it to the data reference, which will be later handled by the service.
DATA lt_order TYPE /iwbep/t_mgw_sorting_order.
IF NOT it_order IS INITIAL.
DATA(lt_orderby) = io_tech_request_context->get_orderby( ).
LOOP AT lt_orderby ASSIGNING FIELD-SYMBOL(<fs_orderby>).
APPEND INITIAL LINE TO lt_order ASSIGNING FIELD-SYMBOL(<fs_order>).
MOVE-CORRESPONDING <fs_orderby> TO <fs_order>.
ENDLOOP.
/iwbep/cl_mgw_data_util=>orderby(
EXPORTING
it_order = lt_order
CHANGING
ct_data = lt_result
).
ENDIF.
Now that you have set this snippet to the end of the your GET_ENTITYSET method, you are now ready to go and send order requests, which can be handled by your OData service.
Leave a Reply