Different approaches to handle Filters/UI Queries/Select options in SAP ABAP OData
I have cumulated different approaches to filter out the data from SAP and send it back to UI through OData service.
1. Converting Filters into WHERE clause
We use input structure io_tech_request_context to get filters converted into where clause. Later we can retrieve data from source using where clause.
DATA(lv_conv) = io_tech_request_context->get_osql_where_clause_convert( ).
SELECT * FROM zdemo_sales " data source
INTO CORRESPONDING FIELDS OF TABLE @et_entityset " output table
WHERE (lv_conv). " where clause
IF sy-subrc = 0.
* Implement error handling here
ENDIF.
Lets Test
Set external debugger at query and execute below URI
/sap/opu/odata/SAP/ZSALESORDER_BASIC_SRV/zdemo_sales?$filter=SalesDocument eq '0000000101' and SalesDocumentItem eq '000010'
Recommended by LinkedIn
2. Using SUPER class method
This method can handle every single input parameters to filter out the data.
We can find IV_ENTITY_NAME , IV_ENTITY_SET_NAME and IV_SOURCE_NAME in debug mode .
3. Get Filter data in range table
Most common and popular way to get filter data of each column/key field into range table and retrieve result from data source using it.
IF it_filter_select_options IS NOT INITIAL.
lt_select_options = it_filter_select_options.
DATA(lrt_salesdoc) = VALUE /iwbep/t_cod_select_options(
lt_select_options[ property = 'SalesDocument' ]-select_options OPTIONAL ).
IF lrt_salesdoc IS NOT INITIAL.
SELECT * FROM zdemo_sales " data source
INTO TABLE @et_entityset
WHERE salesdocument IN @lrt_salesdoc.
ENDIF.
This method is useful if we have very less number of key field as we need to get data into range table for every key field .