Preface – This post is part of the SAP ABAP OData Tutorial series.
Table of Contents
Context
Create entity can be implemented in two ways:
1. Using CREATE_ENTITY method for single entity creation.
2. Using CREATE_DEEP_ENTITY method to create a single entity as well as parent and child entity together.
Procedure
1. Open *DPC_EXT class artifact of OData and redefine *CREATE_ENTITY method.
2. Reimplement the method.
method PARTNERS_CREATE_ENTITY.
data: lv_id type snwd_partner_id,
ls_id type bapi_epm_bp_id,
ls_header type bapi_epm_bp_header,
lt_return type table of bapiret2,
error_msg type string,
ls_message type scx_t100key,
lt_keys type /iwbep/t_mgw_tech_pairs,
ls_snwd_bpa type snwd_bpa,
lv_timestamp type timestamp.
io_data_provider->read_entry_data( importing es_data = ls_header ).
CALL FUNCTION 'BAPI_EPM_BP_CREATE'
EXPORTING
HEADERDATA = ls_header " EPM: BP header data
IMPORTING
BUSINESSPARTNERID = ls_id
TABLES
RETURN = lt_return.
if lt_return is not initial.
loop at lt_return reference into data(lr_return).
err_msg = lr_return->message .
endloop.
ls_message-msgid = 'SY'.
ls_message-msgno = '002'.
ls_message-attr1 = error_msg.
raise exception type /iwbep/cx_mgw_busi_exception
EXPORTING
textid = ls_message.
endif.
CALL FUNCTION 'BAPI_EPM_BP_GET_DETAIL'
EXPORTING
BP_ID = ls_id
IMPORTING
HEADERDATA = er_entity
endmethod.
The input data is passed in es_data structure and captured in ls_header. The structure ls_header is then used as the input parameter of BAPI ‘BAPI_EPM_BP_CREATE’ to create data in the database. Then BAPI ‘BAPI_EPM_BP_GET_DETAIL’ will retrieve the created data and capture it in er_entity which will be passed to the response body.
3. Activate the class.
4. Create Deep Entity (To create Header with Item): Redefine method /IWBEP/IF_MGW_APPL_SRV_RUNTIME~CREATE_DEEP_ENTITY
5. Reimplement the method.
method /IWBEP/IF_MGW_APPL_SRV_RUNTIME~CREATE_DEEP_ENTITY.
types: ty_t_soitem type standard table of zcl_z_epm_rkt_mpc=>ts_salesorderitem with default key.
types: begin of ty_s_so.
include type zcl_z_epm_rkt_mpc=>ts_salesorder.
types: items type ty_t_soitem,
end of ty_s_so.
data: ls_sales_order type ty_s_so,
lv_compare_result type /iwbep/if_mgw_odata_expand=>ty_e_compare_result.
data: lv_so_id type BAPI_EPM_SO_ID,
ls_so_hdr type BAPI_EPM_SO_HEADER,
ls_so_hdr2 type BAPI_EPM_SO_HEADER,
ls_so_item type BAPI_EPM_SO_ITEM,
lt_so_item type standard table of BAPI_EPM_SO_ITEM,
lt_so_item2 type standard table of BAPI_EPM_SO_ITEM,
lt_return type bapirettab,
ls_return TYPE bapiret2,
ls_message type scx_t100key,
error_msg type string.
constants: lc_so_itm TYPE string VALUE 'Items'.
* Validate whether data matches
lv_compare_result = io_expand->compare_to( lc_so_itm ).
* Access data from IO_DATA_PROVIDER
if lv_compare_result EQ /iwbep/if_mgw_odata_expand=>gcs_compare_result-match_equals.
io_data_provider->read_entry_data( IMPORTING es_data = ls_sales_order ).
* Move header Sales order data into BAPI structure
move-corresponding ls_sales_order to ls_so_hdr.
* Move Sales Order items into BAPI table structure
loop at ls_sales_order-items into ls_so_item.
append ls_so_item to lt_so_item.
endloop.
CALL FUNCTION 'BAPI_EPM_SO_CREATE'
EXPORTING
HEADERDATA = ls_so_hdr
IMPORTING
SALESORDERID = lv_so_id
TABLES
ITEMDATA = lt_so_item
RETURN = lt_return.
if lt_return is not initial.
loop at lt_return into ls_return.
err_msg = ls_return-message .
endloop.
ls_message-msgid = 'SY'.
ls_message-msgno = '002'.
ls_message-attr1 = error_msg.
raise exception type /iwbep/cx_mgw_busi_exception
exporting
textid = ls_message.
else.
CALL FUNCTION 'BAPI_EPM_SO_GET_DETAIL'
EXPORTING
SO_ID = lv_so_id
IMPORTING
HEADERDATA = ls_so_hdr2
TABLES
ITEMDATA = lt_so_item2.
move-corresponding ls_so_hdr2 to ls_sales_order.
ls_sales_order-items = lt_so_item2.
copy_data_to_ref(
EXPORTING
is_data = ls_sales_order
CHANGING
cr_data = er_deep_entity ).
endif.
endif.
endmethod.
The structure of incoming data contains parent and child entity nested structure. This is defined with type ty_s_so.
6. Activate the class
7. Execute the Create request.
- Select Entity Set
- Select HTTP method as POST
- Add the data in the request body
- Click Execute
Tutorial Video
You can watch the below video to learn implementation:
[embedyt] https://www.youtube.com/watch?v=2rEZrFj2haE[/embedyt]
Leave a Reply