Technology Blogs by Members
Explore a vibrant mix of technical expertise, industry insights, and tech buzz in member blogs covering SAP products, technology, and events. Get in the mix!
cancel
Showing results for 
Search instead for 
Did you mean: 
pranay570708
Active Contributor
Hello Everyone, this blog explains how to create Dynamic Entity type/Entity Set in SAP Gateway. We can easily create an Static entity type using Gateway service builder (SEGW). But sometimes requirements can be tricky where you need to generate Entity model at runtime. Recently, i got a requirement to develop an OData service which could not be fulfilled using static entity model.

The requirement was to create an SAPUI5 application (running on SAP OData) that will have dynamic selection screen. On execution, it should generate a dynamic report based on output fields selected by the user (relevant to the selection screen input fields). Dynamic selection screen part was easily achieved using static entity set. But the real challenge was to send multiple output records as part of odata service response since user can select any number of fields to display in the dynamic report.

The approach which i followed in developing OData service was:

  1. Created a static entity with properties: Field_Name, Data_Element & Table_Name. In GET_ENTITYSET method, exposed all the fields that need to be selected as part of Dynamic report.

  2. When an user selects output fields, application calls POST method of OData service to store relevant properties(Field_Name, Data_Element & Table_Name) in a custom table.

  3. Now, Create an entity with some dummy field inside it. We will change the properties of this entity at run time reading entry from the custom table. MPC_EXT class will be used here.

  4. Once response gets sent successfully, delete the whole custom table entries stored.


 

Steps:

Create a custom table (that will store user selected fields along with data element):



Insert some records for fields in the custom table:



Now, create a project ‘ZDYMANIC_ENTITY’ using t-code SEGW. Create an entity ‘TEST’ with one dummy attribute (VBELN).



Create an entityset ‘TESTSet’ for the entity. Generate runtime artifacts.



To create dynamic properties(fields) inside the entity, Redefine DEFINE method of MPC_EXT class. Create a private instance method ‘DEFINE_TEST’.



Call ‘DEFINE_TEST’ method inside DEFINE method.



DYNAMIC PROPERTIES Creation (MPC_EXT Class):

Inside the method ‘DEFINE_TEST’. Fetch the properties to be created as well as the data element (needed for binding). Loop in above properties and create properties as below:









METHOD define_test.

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.

DATA: lt_output TYPE STANDARD TABLE OF ztable_fields_op,
ls_output TYPE ztable_fields_op.
**********************************************************************
*   ENTITY - TEST
**********************************************************************

lo_entity_type = model->create_entity_type( iv_entity_type_name = 'TEST' iv_def_entity_set = abap_false ). "#EC NOTEXT

**********************************************************************
*Properties
**********************************************************************

SELECT * FROM ztable_fields_op INTO TABLE lt_output.
IF sy-subrc = 0.
LOOP AT lt_output INTO ls_output.
lo_property = lo_entity_type->create_property( iv_property_name = ls_output- field_label ).
lo_property->set_is_key( ).
lo_property->set_type_edm_string( ).
lo_property->set_nullable( abap_false ).

TRY.
DATA: lv_element TYPE string.
lv_element = ls_output-data_element.

CALL METHOD lo_property->bind_data_element
EXPORTING
iv_element_name = lv_element.

CATCH /iwbep/cx_mgw_med_exception .
ENDTRY.
ENDLOOP.
ENDIF.

**********************************************************************
*   ENTITY SETS
**********************************************************************
lo_entity_set = lo_entity_type->create_entity_set( 'TESTSet' ). "#EC NOTEXT

lo_entity_set->set_has_ftxt_search( abap_false ).
lo_entity_set->set_subscribable( abap_false ).
lo_entity_set->set_filter_required( abap_false ).

ENDMETHOD.


POPULATE Records(DPC_EXT Class):

Now, Inside DPC_EXT class, Redefine the method /IWBEP/IF_MGW_APPL_SRV_RUNTIME~GET_ENTITYSET to populate records in the dynamic entityset:









  METHOD /iwbep/if_mgw_appl_srv_runtime~get_entityset.

DATA: lt_output TYPE STANDARD TABLE OF ztable_fields_op,
ls_output TYPE ztable_fields_op.

DATA: lt_cat TYPE TABLE OF lvc_s_fcat,
ls_cat LIKE LINE OF lt_cat,
d_ref TYPE REF TO data.

FIELD-SYMBOLS : <f_fs> TYPE table.

IF iv_entity_name = 'TEST'.

SELECT * FROM ztable_fields_op INTO TABLE lt_output.
IF sy-subrc = 0.

LOOP AT lt_output INTO ls_output.
ls_cat-tabname = ls_output-table_name.
ls_cat-fieldname =  ls_output-field_label.
ls_cat-ref_field = ls_output-field_label.
ls_cat-ref_table = ls_output-table_name.
APPEND ls_cat TO lt_cat.
ENDLOOP.
ENDIF.

""create a dynamic internal table
CALL METHOD cl_alv_table_create=>create_dynamic_table
EXPORTING
it_fieldcatalog = lt_cat
IMPORTING
ep_table        = d_ref.

ASSIGN d_ref->* TO <f_fs>.

**--Fetch records dynamically
SELECT * FROM (ls_output-table_name)
INTO CORRESPONDING FIELDS OF TABLE <f_fs>.

** Call methos copy_data_to_ref and export entity set data
copy_data_to_ref( EXPORTING is_data = <f_fs>
CHANGING cr_data = er_entityset ).

ENDIF.

ENDMETHOD.


Register the service in the gateway and test the service using Gateway client using URI /sap/opu/odata/sap/ZDYNAMIC_ENTITY_SRV/TESTSet?$format=json:



You can see status code is 200 and multiple records are coming as part of entityset in the response.

Cheers!!
21 Comments
Labels in this area