Application Development and Automation Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

Dynamic Structure Creation

nisha_vinod
Product and Topic Expert
Product and Topic Expert
0 Likes
578

I Have a string (for a eg: 'BPSearch'). I Have a name-value pair table (say T1).

I need to dynamically create a structure with name 'BPSearch' . The fields of this structure would be the first column in the table T1 and the value for the fields of this structure is the 2nd column of the table T1.

Can some one help me how to do this as I am new to ABAP.

Regards

Nisha NC

3 REPLIES 3
Read only

Former Member
0 Likes
551

Hello,

To create structures dynamically you need to use RTTS and RTTI, I suggest you to read the following [https://www.sdn.sap.com/irj/sdn/go/portal/prtroot/docs/library/uuid/b332e090-0201-0010-bdbd-b735e96fe0ae].

Regards,

Read only

0 Likes
551

Hi David,

thanks for that link to presentation. It is really interesting and btw there is also better solution where you do not have to create dynamic table.


DATA: personType TYPE REF TO cl_abap_structdescr,
           employeeType TYPE REF TO cl_abap_structdescr,
           comp_tab TYPE cl_abap_structdescr=>component_table,
           comp LIKE LINE OF comp_tab.

personType ?= cl_abap_typedescr=>describe_by_name( 'personType' ).
comp-name = 'EMPLOYEE'. comp-type = personType. append comp to comp_tab.
comp-name = 'MANAGER'. comp-type = personType. append comp to comp_tab.
employeeType = cl_abap_structdescr=>create( comp_tab ).

Cheers

Read only

mvoros
Active Contributor
0 Likes
551

Hi,

I have solution for you, but it is not the best solution. It creates dynamic table described by ALV fieldcatalog and then it creates structure like line of the dynamic table. You will have to prepare ALV field catalog using your table T1. But maybe it is possible to avoid creating table. So you will just create directly dynamic structure. Just look inside the method CREATE_DYNAMIC_TABLE, it has to be somewhere there.

Here is the code.


FIELD-SYMBOLS: <tab1> TYPE STANDARD TABLE,
               <line> TYPE ANY.

  DATA: lt_new TYPE REF TO data.
  DATA: ls_new TYPE REF TO data.
  FIELD-SYMBOLS: <field> TYPE ANY.

* Create tab1
  CALL METHOD cl_alv_table_create=>create_dynamic_table
    EXPORTING
      it_fieldcatalog = gt_fc1
    IMPORTING
      ep_table        = lt_new.

* Create a new line with the same structure as the table.
  ASSIGN lt_new->* TO <tab1>.

  CREATE DATA ls_new LIKE LINE OF <tab1>.
  ASSIGN ls_new->* TO <line>.

* Assigning value YYY to component XXX
  ASSIGN COMPONENT 'XXX' OF STRUCTURE <line> TO <field>.
  MOVE 'YYY' TO <field>.
 

Cheers