Netweaver Gateway Development

how to implement $filter query option in SAP OData service?

            $filter query option is the most used query option whil accessing OData service.The framework does not provide default filtering.We need to implement it ourself.$filter query option allows us to filter the result set of GET_ENTITYSET implementation.
          In this tutorial we will use GET_ENTITYSET method from our earlier tutorial mentioned above.


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.Our filter query passed from service call is inside import paramete it_filter_select_options .We will loop at this table and fetch the filter parameter into a table of structure SELOPT(SELECT-OPTIONS IN ABAP).
  3.     DATA:ls_filter_select_options TYPE /iwbep/s_mgw_select_option,
             ls_select_option         TYPE /iwbep/s_cod_select_option.
        DATA:ls_selectparams TYPE selopt,
             lt_selectparams TYPE STANDARD TABLE OF selopt.
        LOOP AT it_filter_select_options INTO ls_filter_select_options.
          IF ls_filter_select_options-property EQ 'Carrid'.
            LOOP AT ls_filter_select_options-select_options INTO ls_select_option.
              ls_selectparams-sign = ls_select_option-sign.
              ls_selectparams-option = ls_select_option-option.
              ls_selectparams-low = ls_select_option-low.
              ls_selectparams-high = ls_select_option-high.
              APPEND ls_selectparams TO lt_selectparams.
            ENDLOOP.
    
          ENDIF.
    
        ENDLOOP.
    


  4. Fetch data based on the filter query.
  5.       SELECT * FROM SCARR INTO CORRESPONDING FIELDS OF TABLE ET_ENTITYSET
          WHERE Carrid in lt_selectparams.
    
  6. Now we can test our filter query in gateway client( /IWFND/GW_CLIENT).Our service url look like this.http://<host>:<port>/sap/opu/odata/SAP/ZTEST_ODATA_SRV_01/FlightSet?$filter=Carrid eq 'AC'.Press execute button.
Note:Some times filter query can become complex.With abap SELECT-OPTIONS structure we cannot cover all scenarios like  a filter such as FieldA eq ‘A’ or FieldB eq ‘B’ can’t be put into our table because of OR in the filter.In suchcases we can use import parameter IV_FILTER_STRING .Then our select statement should change like this.
          SELECT * FROM SCARR INTO CORRESPONDING FIELDS OF TABLE ET_ENTITYSET
          WHERE (IV_FILTER_STRING).
    
If you enjoyed this post, Please Share!!

4 comments :

  1. Very nice tutorial..
    Can you please elaborate more on applying filter for multiple columns? I did not get what this query does..
    SELECT * FROM SCARR INTO CORRESPONDING FIELDS OF TABLE ET_ENTITYSET
    WHERE (IV_FILTER_STRING).

    Can you please pick an example and explain similar to this blog..

    ReplyDelete
    Replies
    1. Hi,
      IV_FILTER_STRING will have the search string as it is at run time.You can check this by putting an external break point and find the difference.
      For multiple columns you can try the same.just check what it_filter_select_options holds?.For this use this url
      http://:/sap/opu/odata/SAP/ZTEST_ODATA_SRV_01/FlightSet?$filter=Carrid eq 'AC' and Carrname eq 'yourValue'

      Delete
    2. HI , can you please take a sample for this IV_FILTER_STRING . It seem to be different than singe filter operation.
      How can I convert IV_FILTER_STRING to it_filter_select_options ?

      Delete
    3. Hi Hoang Thai,
      You can put an external break-point in segw and check the variable yourself.

      Delete

Powered by Blogger.