Create Deep Insert Odata for SAPUI5   


Hello everyone, in this Netweaver Gateway, we will learn how to create  Deep Insert OData service for SAPUI5 Using BAPI_SALESORDER_CREATEFROMDAT2.

Prerequisite: You should create a OData service to support $expand. At first We need to create $expand Query : 

Goto SEGW Tcode : Create Project. Then Create Entity Type and Entity Set for Sales Order and SalesItem :

SalesOrder Entity:


SalesItem Entitiy


Create Associations given below 


Create Navigation given below 


First, it calls the GET_ENTITY / GET_ENTITYSET for sales order header data:

 METHOD salesorderset_get_entity.


    DATAlwa_key_tab TYPE /iwbep/s_mgw_name_value_pair,
          w_vbak      TYPE vbak,
          salesno     TYPE vbak-vbeln.

    READ TABLE it_key_tab INTO lwa_key_tab WITH KEY name 'Salesdocument'.

    IF sy-subrc 0.
      salesno lwa_key_tab-value.
    ENDIF.

    CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'
      EXPORTING
        input  salesno
      IMPORTING
        output salesno.

    SELECT SINGLE FROM vbak  INTO w_vbak WHERE  vbeln salesno .

    er_entity-salesdocument w_vbak-vbeln.
    er_entity-sales_org w_vbak-vkorg .
    er_entity-distr_chan w_vbak-vtweg .
    er_entity-division w_vbak-spart .

ENDMETHOD.

Second, it calls the GET_ENTITYSET of order items in a Loop for each above sales orders retrieved : 

  method SALESITEMSET_GET_ENTITYSET.

     DATAlwa_key_tab TYPE /iwbep/s_mgw_name_value_pair,
          w_vbak      TYPE vbak,
          lt_itemdata        TYPE STANDARD TABLE OF  vbap,
          ls_itemdata        TYPE   vbap,
           w_item             TYPE zcl_zsd_deep_enty_mpc=>ts_salesitem,
          salesno     TYPE vbak-vbeln.

    READ TABLE it_key_tab INTO lwa_key_tab WITH KEY name 'Salesdocument'.

    IF sy-subrc 0.
      salesno lwa_key_tab-value.
    ENDIF.
*    salesno = '4030000231'.
    CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'
      EXPORTING
        input  salesno
      IMPORTING
        output salesno.

    SELECT FROM vbap  INTO TABLE lt_itemdata WHERE  vbeln salesno .

      LOOP AT lt_itemdata INTO ls_itemdata.
          w_item-salesdocument salesno.
          w_item-req_qty ls_itemdata-klmeng.
*           w_item-PARTN_ROLE = ls_itemdata-
*           w_item-PARTN_NUMB = ls_itemdata-
          w_item-itm_number ls_itemdata-posnr.
          w_item-material ls_itemdata-matnr.
          w_item-plant ls_itemdata-werks.
          w_item-target_qty ls_itemdata-zmeng.
          APPEND w_item TO  et_entityset.
        ENDLOOP.
endmethod.

After successful creation of OData service. Go to the DPC_EXT class and identity the method /IWBEP/IF_MGW_APPL_SRV_RUNTIME~GET_EXPANDED_ENTITY and redefine it : 

  METHOD /iwbep/if_mgw_appl_srv_runtime~get_expanded_entityset.

    DATA BEGIN OF ls_order_item_data.
        INCLUDE         TYPE zcl_zsd_deep_enty_mpc=>ts_salesorder.
    DATAitemdata TYPE zcl_zsd_deep_enty_mpc=>tt_salesitem,
          END OF ls_order_item_data.

    CONSTANTSlc_expand_tech_clause TYPE string VALUE 'ToItem'.

    DATAlv_entity_set_name TYPE /iwbep/mgw_tech_name,
          ls_headerdata      TYPE vbak,
          lwa_key_tab        TYPE /iwbep/s_mgw_name_value_pair,
          ls_key_tab         TYPE /iwbep/s_mgw_name_value_pair,
          lt_itemdata        TYPE STANDARD TABLE OF  vbap,
          salesno            TYPE vbak-vbeln,
          w_item             TYPE zcl_zsd_deep_enty_mpc=>ts_salesitem,
          w_vbak             TYPE vbak,
          ls_itemdata        TYPE vbap.

    lv_entity_set_name     io_tech_request_context->get_entity_set_name).

    CASE lv_entity_set_name.

      WHEN 'SalesOrderSet'.

        READ TABLE it_key_tab INTO lwa_key_tab WITH KEY name 'Salesdocument'.

        IF sy-subrc 0.
          salesno lwa_key_tab-value.
        ENDIF.

        CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'
          EXPORTING
            input  salesno
          IMPORTING
            output salesno.

        SELECT SINGLE FROM vbak  INTO w_vbak WHERE  vbeln salesno .

        ls_order_item_data-salesdocument w_vbak-vbeln.
        ls_order_item_data-sales_org w_vbak-vkorg .
        ls_order_item_data-distr_chan w_vbak-vtweg .
        ls_order_item_data-division w_vbak-spart .

        SELECT FROM vbap  INTO TABLE lt_itemdata WHERE  vbeln salesno .

        LOOP AT lt_itemdata INTO ls_itemdata.
          w_item-salesdocument salesno.
          w_item-req_qty ls_itemdata-klmeng.
*           w_item-PARTN_ROLE = ls_itemdata-
*           w_item-PARTN_NUMB = ls_itemdata-
          w_item-itm_number ls_itemdata-posnr.
          w_item-material ls_itemdata-matnr.
          w_item-plant ls_itemdata-werks.
          w_item-target_qty ls_itemdata-zmeng.
          APPEND w_item TO  ls_order_item_data-itemdata.
        ENDLOOP.

       

        copy_data_to_refEXPORTING is_data ls_order_item_data
                          CHANGING  cr_data er_entityset ).

        INSERT lc_expand_tech_clause INTO TABLE et_expanded_tech_clauses.

      WHEN OTHERS.
        TRY.
            CALL METHOD super->/iwbep/if_mgw_appl_srv_runtime~get_expanded_entityset
              EXPORTING
                iv_entity_name           iv_entity_name
                iv_entity_set_name       iv_entity_set_name
                iv_source_name           iv_source_name
                it_filter_select_options it_filter_select_options
                it_order                 it_order
                is_paging                is_paging
                it_navigation_path       it_navigation_path
                it_key_tab               it_key_tab
                iv_filter_string         iv_filter_string
                iv_search_string         iv_search_string
                io_expand                io_expand
                io_tech_request_context  io_tech_request_context
              IMPORTING
                er_entityset             er_entityset
                et_expanded_clauses      et_expanded_clauses
                et_expanded_tech_clauses et_expanded_tech_clauses
                es_response_context      es_response_context.
          CATCH /iwbep/cx_mgw_busi_exception .
          CATCH /iwbep/cx_mgw_tech_exception .
        ENDTRY.
    ENDCASE.
  ENDMETHOD.




Go to the DPC extension class and identity the method /IWBEP/IF_MGW_APPL_SRV_RUNTIME~CREATE_DEEP_ENTITY and redefine it : 

  METHOD /iwbep/if_mgw_appl_srv_runtime~create_deep_entity.

    DATA BEGIN OF ls_order_item_data.
        INCLUDE         TYPE zcl_zsd_deep_enty_mpc=>ts_salesorder.
    DATA headtoitem TYPE zcl_zsd_deep_enty_mpc=>tt_salesitem,
           END OF ls_order_item_data.

*  DATA : ls_order_item_data TYPE ZCL_ZSD_DEEP_ENTY_MPC_EXT=>ts_deep_entity.

    DATAlv_entity_set_name TYPE /iwbep/mgw_tech_name,
          lwa_key_tab        TYPE /iwbep/s_mgw_name_value_pair,
          ls_key_tab         TYPE /iwbep/s_mgw_name_value_pair,
          ls_req_itemdata    TYPE zcl_zsd_deep_enty_mpc=>ts_salesitem.


    DATAv_vbeln       TYPE bapivbeln-vbeln,
          wa_header     TYPE bapisdhd1,
          wa_headerx    TYPE bapisdhd1x,
          wa_item       TYPE bapisditm,
          it_item       TYPE TABLE OF bapisditm,
          wa_itemx      TYPE bapisditmx,
          it_itemx      TYPE TABLE OF bapisditmx,
          wa_partner    TYPE bapiparnr,
          it_partner    TYPE TABLE OF bapiparnr,
          wa_return     TYPE bapiret2,
          it_return     TYPE TABLE OF bapiret2,
          wa_text       TYPE bapisdtext,
          it_text       TYPE TABLE OF bapisdtext,
          wa_condition  TYPE bapicond,
          it_condition  TYPE TABLE OF bapicond,
          wa_conditionx TYPE bapicondx,
          it_conditionx TYPE TABLE OF bapicondx,
          wa_schedule   TYPE bapischdl,
          it_schedule   TYPE TABLE OF bapischdl,
          wa_schedulex  TYPE bapischdlx,
          it_schedulex  TYPE TABLE OF bapischdlx.


    lv_entity_set_name     io_tech_request_context->get_entity_set_name).

    CASE lv_entity_set_name.

      WHEN 'SalesOrderSet'.

*        TRY.
*        CALL METHOD me->custome_create_deep_entity
*          EXPORTING
*            iv_entity_name          = iv_entity_name
*            iv_entity_set_name      = iv_entity_set_name
*            iv_source_name          = iv_source_name
*            io_data_provider        = io_data_provider
*            it_key_tab              = it_key_tab
*            it_navigation_path      = it_navigation_path
*            io_expand               = io_expand
*            io_tech_request_context = io_tech_request_context
*          IMPORTING
*            er_deep_entity          =  ls_order_item_data
*            .
*         CATCH /iwbep/cx_mgw_busi_exception .
*         CATCH /iwbep/cx_mgw_tech_exception .
*        ENDTRY.


        io_data_provider->read_entry_dataIMPORTING es_data ls_order_item_data ).
*
* Header data
        wa_header-doc_type ls_order_item_data-doc_type.
        wa_header-sales_org ls_order_item_data-sales_org.
        wa_header-distr_chan ls_order_item_data-distr_chan.
        wa_header-division ls_order_item_data-division."'00'.
*     APPEND wa_header .
*
        wa_headerx-updateflag 'i'.
        wa_headerx-doc_type 'x'.
        wa_headerx-sales_org 'x'.
        wa_headerx-distr_chan 'x'.
        wa_headerx-division 'x'.
*
        LOOP AT ls_order_item_data-headtoitem INTO ls_req_itemdata.


          wa_partner-partn_role ls_req_itemdata-partn_role."'ag'.
          wa_partner-partn_numb ls_req_itemdata-partn_numb."'0000023422'.
          APPEND wa_partner TO it_partner.
*
*     Line item number.
          wa_item-itm_number ls_req_itemdata-itm_number.  " '000013'.
          wa_item-material ls_req_itemdata-material."'cyd'.
          wa_item-plant ls_req_itemdata-plant." 'sl31'.
          wa_item-target_qty ls_req_itemdata-target_qty."'2'.
          APPEND wa_item TO it_item.
*
          wa_itemx-itm_number ls_req_itemdata-itm_number.
          wa_itemx-material 'x'.
          wa_itemx-plant 'x'.
          wa_itemx-target_qty 'x'.
          wa_itemx-updateflag 'i'.
          APPEND wa_itemx TO it_itemx.
*
          wa_schedule-itm_number ls_req_itemdata-itm_number"'000013'.
          wa_schedule-req_qty ls_req_itemdata-req_qty."'2'.
          APPEND wa_schedule TO it_schedule.
*
          wa_schedulex-itm_number ls_req_itemdata-itm_number.
          wa_schedulex-req_qty 'x'.
          wa_schedulex-updateflag 'i'.
          APPEND wa_schedulex TO it_schedulex.
*
        ENDLOOP.
*
        CALL FUNCTION 'BAPI_SALESORDER_CREATEFROMDAT2'
          EXPORTING
            order_header_in      wa_header
            order_header_inx     wa_headerx
          IMPORTING
            salesdocument        v_vbeln
          TABLES
            return               it_return
            order_items_in       it_item
            order_items_inx      it_itemx
            order_partners       it_partner
            order_text           it_text
            order_conditions_in  it_condition
            order_conditions_inx it_conditionx
            order_schedules_in   it_schedule
            order_schedules_inx  it_schedulex.

        LOOP AT it_return INTO wa_return WHERE type 'e' OR type 'a'.
          EXIT.
        ENDLOOP.
*
        IF sy-subrc EQ 0.
          WRITE'error in creating document'.
        ELSE.
* Commit the work.
          COMMIT WORK AND WAIT.
          WRITE'document 'v_vbeln' created'.
          ls_order_item_data-salesdocument v_vbeln.
          copy_data_to_refEXPORTING is_data ls_order_item_data
                          CHANGING  cr_data er_deep_entity ).

        ENDIF.

      WHEN OTHERS.

        TRY.
            CALL METHOD super->/iwbep/if_mgw_appl_srv_runtime~create_deep_entity
              EXPORTING
                iv_entity_name          iv_entity_name
                iv_entity_set_name      iv_entity_set_name
                iv_source_name          iv_source_name
                io_data_provider        io_data_provider
                it_key_tab              it_key_tab
                it_navigation_path      it_navigation_path
                io_expand               io_expand
                io_tech_request_context io_tech_request_context
              IMPORTING
                er_deep_entity          er_deep_entity.
          CATCH /iwbep/cx_mgw_busi_exception .
          CATCH /iwbep/cx_mgw_tech_exception .
        ENDTRY.
    ENDCASE.
  ENDMETHOD.


We are done with coding part. Lets test the service again in SAP Netweaver Gateway Client /IWFND/GW_CLIENT.Call the below OData service URI with HTTP GET, to get the data of any sales order using $expand you should see the below output.


/sap/opu/odata/SAP/ZSD_DEEP_ENTY_SRV/SalesOrderSet('4030000231')?$expand=HeadtoItem





In the HTTP Request section clear the Sales order number and line item numbers, as we are creating a new sales order using this data :






Comments

Popular posts from this blog

Create BP and Customer Using ABAP in SAP

Create/Edit/Delete Attachments using GOS in Sap ABAP

Create Secondary Index on SAP Table