
Use CREATE_ENTITY to create a BusinessPartner entity and CREATE_DEEP_ENTITY to create a SalesOrder along with SalesOrderItems.
method BUSINESSPARTNERS_CREATE_ENTITY.
data: lv_id type snwd_partner_id,
ls_id type bapi_epm_bp_id.
data: ls_headerdata type bapi_epm_bp_header,
lt_return type table of bapiret2,
ls_return type bapiret2,
err_msg type string,
lo_message_container type ref to /iwbep/if_message_container.
data: ls_message type scx_t100key,
lt_keys type /iwbep/t_mgw_tech_pairs,
ls_snwd_bpa type snwd_bpa.
DATA: lv_timestamp TYPE timestamp.
field-symbols: <ls_key> type /iwbep/s_mgw_tech_pair.
io_data_provider->read_entry_data( importing es_data = ls_headerdata ).
call function 'BAPI_EPM_BP_CREATE'
exporting
headerdata = ls_headerdata " EPM: BP header data
importing
businesspartnerid = ls_id
tables
return = lt_return. " Return Parameter
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 = err_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
* TABLES
* CONTACTDATA =
* RETURN = lt_return
.
endmethod.
The body of the request is passed in through the es_data structure and captured in ls_headerdata. The data is then used as input for the BAPI function BAPI_EPM_BP_CREATE. The OData standard specifies that for a POST/CREATE operation, the created entity data should be returned in the body of the response. This is why in the above implementation, a call to function BP_EPM_BP_GET_DETAIL is made, using the BP_ID resulting from the create call, to capture and return the BusinessPartner entry data in the er_entity structure, which is used to pass back the data in the response body.
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.
* Represents full Sales Order structure - header with one or more items
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_so type ty_s_so,
ls_item type zcl_z_epm_rkt_mpc=>ts_salesorderitem,
lv_compare_result type /iwbep/if_mgw_odata_expand=>ty_e_compare_result.
data: lv_so_id type BAPI_EPM_SO_ID,
ls_sohdr type BAPI_EPM_SO_HEADER,
ls_sohdr2 type BAPI_EPM_SO_HEADER,
ls_soitem type BAPI_EPM_SO_ITEM,
lt_soitem type standard table of BAPI_EPM_SO_ITEM,
lt_soitem2 type standard table of BAPI_EPM_SO_ITEM,
lt_return type bapirettab,
ls_return TYPE bapiret2,
lx_busi_exc TYPE REF TO /iwbep/cx_mgw_busi_exception,
lo_meco TYPE REF TO /iwbep/if_message_container,
ls_message type scx_t100key,
err_msg type string,
ls_snwd_so type snwd_so.
constants: lc_soitems TYPE string VALUE 'Items'.
* Validate whether the current request including the inline SO Item data matches
lv_compare_result = io_expand->compare_to( lc_soitems ).
* Upon match, 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_so ).
* Move header SO data into BAPI structure
move-corresponding ls_so to ls_sohdr.
* Move SO line items into BAPI table structure
loop at ls_so-items into ls_soitem.
append ls_soitem to lt_soitem.
endloop.
CALL FUNCTION 'BAPI_EPM_SO_CREATE'
EXPORTING
HEADERDATA = ls_sohdr
IMPORTING
SALESORDERID = lv_so_id
TABLES
ITEMDATA = lt_soitem
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 = err_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_sohdr2
TABLES
ITEMDATA = lt_soitem2
* RETURN =
.
move-corresponding ls_sohdr2 to ls_so.
ls_so-items = lt_soitem2.
copy_data_to_ref(
EXPORTING
is_data = ls_so
CHANGING
cr_data = er_deep_entity ).
endif.
endif.
endmethod.
One of the unique aspects of this method is the structure of the incoming data. Since it consists of two entity types, arranged in a parent-child nested structure, you must declare a similar structure to capture such data. This is done by defining a type ty_s_so.
The type ty_s_so consists of a structure of type zcl_z_epm_rkt_mpc=>ts_salesorder (which in turn is of type BAPI_EPM_SO_HEADER) and a table type of made up of structure zcl_z_epm_rkt_mpc=>ts_salesorderitem (which is turn is of type BAPI_EPM_SO_ITEM). Declare a structure variable ls_so using type ty_s_so, which is used to capture the sales order header data along with the multiple line items passed into the method. Then the data is broken up into the required header and item data structures needed as input for function BAPI_EPM_SO_CREATE.
Upon success, the created sales order entity along with its corresponding line items are read and sent back to the consumer in the response body.