$inlinecount query option in sap OData service

0
3361

Hello everyone, in this tutorial we will learn how to use query option $inlinecount in SAP netweaver gateway OData service. Before proceeding further we assume that you know how to build OData service in sap gateway. Access all SAP Netweaver Gateway tutorials here.Lets get started

What $inlinecount query option does ?

Query option $inlinecount is used to get the overall count of feed/collection together with the entity set collection data.

Supported System Version

SAP NetWeaver Gateway Release 2.0 Support Package >=04

Business Example

A gateway OData service is getting all the products from the back-end system. You also want the count of the queried collection. In this scenario you can use ‘$inlinecount=allpages’ query option to get both count and entity set collection data in a single OData call.

Syntax

http://<server>:<port>/sap/opu/odata/sap/<service_name>/ProductsSet?$inlinecount=allpages

How to Implement $inlinecount query option in OData Service

In this section we will adjust the OData service to respond to $inlinecount query option. $inlinecount query option should be applied only to Entity Type sets. We assume that you have already build the OData service to get the list of products. if you have not click here.

1. After successful creation of OData service. Go back to service builder SEGW, Navigate to the ABAP workbench for the Product Entity set.

Go to ABAP Workbench

 

 

 

 

2. Code inside the method PRODUCTSSET_GET_ENTITYSET will look like below i.e before adjusting the service to $inlinecount query option.

DATA:      ls_order          TYPE /iwbep/s_mgw_sorting_order,
           lt_products       TYPE STANDARD TABLE OF bapi_epm_product_header,
           ls_products       TYPE bapi_epm_product_header,
           ls_entityset      TYPE zcl_zdemo_gw_srv_mpc=>ts_products,
           lt_entityset      TYPE zcl_zdemo_gw_srv_mpc=>tt_products,
           lv_max_rows       TYPE bapi_epm_max_rows,
           ls_filter         TYPE /iwbep/s_mgw_select_option,
           lr_product_id     TYPE TABLE OF  bapi_epm_product_id_range,
           ls_product_id     TYPE bapi_epm_product_id_range,
           ls_select_options TYPE /iwbep/s_cod_select_option.

* >> Check if $filter option is available
* Get the filter option for product ID
    READ TABLE it_filter_select_options
        INTO ls_filter
        WITH KEY property = 'ProductId'.
    IF sy-subrc = 0.
      LOOP AT ls_filter-select_options INTO ls_select_options.
        MOVE-CORRESPONDING ls_select_options TO ls_product_id.
        APPEND ls_product_id TO lr_product_id.
      ENDLOOP.
    ENDIF.

* Call the BAPI by providing the filter options
* if no filter, BAPI will get the entire list of products
    CALL FUNCTION 'BAPI_EPM_PRODUCT_GET_LIST'
      TABLES
        headerdata        = lt_products
        selparamproductid = lr_product_id.
    IF lt_products IS NOT INITIAL.
      LOOP AT lt_products INTO ls_products.
        MOVE-CORRESPONDING ls_products TO ls_entityset.
        APPEND ls_entityset TO et_entityset.
      ENDLOOP.
    ENDIF.

 

3. In the above code snippet, we have retrieved the entire list of products.Now we will adjust the code to adapt it to the $inlinecount query option to get the count of collection as well in the response.

4. Put the below code in the method PRODUCTSSET_GET_ENTITYSET to respond to the $inlinecount query option.

    DATA:  ls_order          TYPE /iwbep/s_mgw_sorting_order,
           lt_products       TYPE STANDARD TABLE OF bapi_epm_product_header,
           ls_products       TYPE bapi_epm_product_header,
           ls_entityset      TYPE zcl_zdemo_gw_srv_mpc=>ts_products,
           lt_entityset      TYPE zcl_zdemo_gw_srv_mpc=>tt_products,
           lv_max_rows       TYPE bapi_epm_max_rows,
           ls_filter         TYPE /iwbep/s_mgw_select_option,
           lr_product_id     TYPE TABLE OF  bapi_epm_product_id_range,
           ls_product_id     TYPE bapi_epm_product_id_range,
           ls_select_options TYPE /iwbep/s_cod_select_option.

* >> Check if $filter option is available
* Get the filter option for product ID
    READ TABLE it_filter_select_options
        INTO ls_filter
        WITH KEY property = 'ProductId'.
    IF sy-subrc = 0.
      LOOP AT ls_filter-select_options INTO ls_select_options.
        MOVE-CORRESPONDING ls_select_options TO ls_product_id.
        APPEND ls_product_id TO lr_product_id.
      ENDLOOP.
    ENDIF.

* Call the BAPI by providing the filter options
* if no filter, BAPI will get the entire list of products
    CALL FUNCTION 'BAPI_EPM_PRODUCT_GET_LIST'
      TABLES
        headerdata        = lt_products
        selparamproductid = lr_product_id.
    IF lt_products IS NOT INITIAL.
      LOOP AT lt_products INTO ls_products.
        MOVE-CORRESPONDING ls_products TO ls_entityset.
        APPEND ls_entityset TO et_entityset.
      ENDLOOP.
    ENDIF.

* $inlinecount query option
    IF io_tech_request_context->has_inlinecount( ) = abap_true.
      DESCRIBE TABLE et_entityset LINES es_response_context-inlinecount.
    ELSE.
      CLEAR es_response_context-inlinecount.
    ENDIF.

5. What we did in the above step.First we got all the products and then check $inlinecount query option is requested if so then provide the count in the response ES_RESPONSE_CONTEXT-INLINECOUNT of the OData service .

6. From code perspective we are ready, so lets test the service by using the sap gateway client /IWFND/GW_CLIENT.

7. If we execute the service without adding $inlinecount query option, we should get all the products available in the system.

If we add the $inlinecount=allpages query option to the service we should get the count in the response with collection data as well. Test the service by providing the following URI with $inlinecount=allpages.

/sap/opu/odata/sap/ZDEMO_GW_SRV_SRV/ProductsSet?$inlinecount=allpages’.

$inlinecount Query Option

Stay tuned to us for more SAP Netweaver Gateway tutorials.

Thank you. Please feel free to comment and let us know your feedback.