Create Deep Entity
Hello everyone,
I have faced an issue where I am trying to push header and line items together to the back-end from the SAPUI5 / fiori application through OData, I have found a solution for it so though of sharing.
Scenario: when we have to avail a process which involves more than one entity involved from the OData model, As in If we have to create or save Workorder which requires line items involved then we should have Create_Deep_Entity approach which is explained in brief below:
Create Deep Entity
- Go to the Model Provider extension class: MPC_EXT
- Go to the Types and declare a type:
types:
BEGIN OF ts_deep_entity.
INCLUDE TYPE workorder_structure.
TYPES:
line_item TYPE STANDARD TABLE OF ts_line_item_structure WITH DEFAULT
END OF ts_deep_entity .
Note: line_item mentioned above would be the navigation declared in OData from workorder to line_item
- Redefine the method DEFINE and write this code snippet:
METHOD define.
super->define( ).
DATA:
lo_annotation TYPE REF TO /iwbep/if_mgw_odata_annotation,
lo_entity_type TYPE REF TO /iwbep/if_mgw_odata_entity_typ,
lo_complex_type TYPE REF TO /iwbep/if_mgw_odata_cmplx_type,
lo_property TYPE REF TO /iwbep/if_mgw_odata_property,
lo_entity_set TYPE REF TO /iwbep/if_mgw_odata_entity_set.
***********************************************************************************************************************************
* ENTITY – Deep Entity
***********************************************************************************************************************************
lo_entity_type = model->get_entity_type( iv_entity_name = ‘WorkOrder’ ). “#EC NOTEXT
lo_entity_type->bind_structure( iv_structure_name = ‘ZCL_ZGWS_XXXX_MPC_EXT=>TS_DEEP_ENTITY’ ). “#EC NOTEXT
ENDMETHOD.
- Go to Data Provider Extension class: DPC_EXT
- Create a method CUSTOM_CREATE_DEEP_ENTITY with the following parameters:
IV_ENTITY_NAME | Importing | Type | STRING |
IV_ENTITY_SET_NAME | Importing | Type | STRING |
IV_SOURCE_NAME | Importing | Type | STRING |
IT_KEY_TAB | Importing | Type | /IWBEP/T_MGW_NAME_VALUE_PAIR |
IT_NAVIGATION_PATH | Importing | Type | /IWBEP/T_MGW_NAVIGATION_PATH |
IO_EXPAND | Importing | Type Ref To | /IWBEP/IF_MGW_ODATA_EXPAND |
IO_TECH_REQUEST_CONTEXT | Importing | Type Ref To | /IWBEP/IF_MGW_REQ_ENTITY_C |
IO_DATA_PROVIDER | Importing | Type Ref To | /IWBEP/IF_MGW_ENTRY_PROVIDER |
ER_DEEP_ENTITY | Exporting | Type | ZCL_ZGWS_XXXX_MPC_EXT=>TS_DEEP_ENTITY |
And write the code:
METHOD custom_create_deep_entity.
DATA:
lr_deep_entity TYPE zcl_zgws_xxxx_mpc_ext=>ts_deep_entity,
lt_wororder TYPE STANDARD TABLE OF workorder_structure,
ls_wororder TYPE workorder_structure,
lt_line_item TYPE STANDARD TABLE OF line_item_structure,
ls_line_item TYPE line_item_structure,
* Transform data into the internal structure
TRY .
io_data_provider->read_entry_data(
IMPORTING
es_data = lr_deep_entity ).
CATCH /iwbep/cx_mgw_tech_exception .
ENDTRY.
**********collect the header fields
me->move_to_header_data(
EXPORTING
ir_deep_entity = lr_deep_entity ” Deep Entity
IMPORTING
es_ wororder = ls_wororder ).
********Collect company fields
LOOP AT lr_deep_entity-line_item INTO ls_line_item.
APPEND ls_line_item TO lt_line_item.
ENDLOOP.
*** Create / Save WorkOrder
*** Write code as per the requirement
CALL METHOD zworkorder_maintenance->create_workorder
CALL METHOD zworkorder_maintenance->save_workorder
me->move_from_header_data(
EXPORTING
is_workorder = ls_workorder
IMPORTING
er_deep_entity = er_deep_entity
).
ENDMETHOD.
Note: Methods mentioned above: move_to_header_data and move_from_header_data and nothing but field to field data move
Like this: ls_workorder-AUFNR = ir_deep_entity-AUFNR.
And vice versa
- Now Redefine the method /IWBEP/IF_MGW_APPL_SRV_RUNTIME~CREATE_DEEP_ENTITY from DPC_EXT class
And write down the code:
METHOD /iwbep/if_mgw_appl_srv_runtime~create_deep_entity.
**TRY.
*CALL METHOD SUPER->/IWBEP/IF_MGW_APPL_SRV_RUNTIME~CREATE_DEEP_ENTITY
* EXPORTING
** iv_entity_name =
** iv_entity_set_name =
** iv_source_name =
* IO_DATA_PROVIDER =
** it_key_tab =
** it_navigation_path =
* IO_EXPAND =
** io_tech_request_context =
** IMPORTING
** er_deep_entity =
* .
** CATCH /iwbep/cx_mgw_busi_exception .
** CATCH /iwbep/cx_mgw_tech_exception .
**ENDTRY.
DATA custom_create_deep_entity TYPE zcl_zgws_XXXX_mpc_ext=>ts_deep_entity.
CASE iv_entity_set_name.
*————————————————————————-*
* EntitySet – HeaderSet
*————————————————————————-*
WHEN ‘WORKORDERSET’.
* Call the entity set generated method
* TRY.
CALL METHOD me->custom_create_deep_entity
EXPORTING
iv_entity_name = iv_entity_name
iv_entity_set_name = iv_entity_set_name
iv_source_name = iv_source_name
it_key_tab = it_key_tab
it_navigation_path = it_navigation_path
io_expand = io_expand
io_tech_request_context = io_tech_request_context
io_data_provider = io_data_provider
IMPORTING
er_deep_entity = custom_create_deep_entity.
copy_data_to_ref(
EXPORTING
is_data = custom_create_deep_entity
CHANGING
cr_data = er_deep_entity ).
ENDCASE.
ENDMETHOD.
Kindly let me know If further information is required.
Thank you for your time.
And let me know If it works for you 🙂
Priyanka Patankar
thanks