Skip to Content

Archived discussions are read-only. Learn more about SAP Q&A

merge two structures to one structure in run-time

HI All,

I'm having two structures type any and i want to merge them to one structure

which will have the both fields ,while searching in the forum there is option

to use the RTTI options (mainly for tables issues ) ,i try it but without success .

cl_abap_typedescr

cl_abap_structdescr

This is the structures

FIELD-SYMBOLS: <ls_attr_structure> TYPE any,
                 <ls_key_struct>          TYPE any,
                 <ls_merge> type any.


DATA: lo_attr_struct TYPE REF TO data,
        lo_attr_key TYPE REF TO data.


 ASSIGN:lo_attr_struct->* TO <ls_attr_structure>,
         lo_attr_key->* TO <ls_key_struct>.

now i want that <ls_merge> will have the both structures fields .

Thanks,

Joy

Edited by: Joy Stpr on Jun 6, 2011 1:16 PM

Former Member
replied

Hello Joy,

You're using the wrong method to get the fields of the structure. You have to use the method GET_COMPONENTS( ) of the class CL_ABAP_STRUCTDESCR to get the detail.

Check this code snippet:

DATA: go_struc      TYPE REF TO cl_abap_structdescr,
      gt_comp1      TYPE cl_abap_structdescr=>component_table,
      gt_comp2      TYPE cl_abap_structdescr=>component_table,
      gv_dref       TYPE REF TO data.

FIELD-SYMBOLS: <gs_struc1> TYPE ANY,
               <gs_struc2> TYPE ANY,
               <gs_merge>  TYPE ANY.

* Get fields of 1st Structure
go_struc ?= cl_abap_structdescr=>describe_by_data( <gs_struc1> ).
gt_comp1 = go_struc->get_components( ).
CLEAR go_struc.

* Get the fields of the 2nd structure
go_struc ?= cl_abap_structdescr=>describe_by_data( <gs_struc2> ).
gt_comp2 = go_struc->get_components( ).
CLEAR go_struc.

* Merge the fields of the 2 structures
APPEND LINES OF gt_comp2 TO gt_comp1.
FREE gt_comp2.

* Create the RTTC instance for the merged structure
TRY.
    go_struc = cl_abap_structdescr=>create( p_components = gt_comp1 ).
  CATCH cx_sy_struct_creation .
ENDTRY.

CREATE DATA gv_dref TYPE HANDLE go_struc. "Create data ref. variable
ASSIGN gv_dref->* TO <gs_merge>. "Dereference the data reference

BR,

Suhas

0 View this answer in context
Not what you were looking for? View more on this topic or Ask a question