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

create field(s) dynamically

Former Member
0 Likes
519

Hi Experts,

How to create field or multiple fields dynamically????

Requirement :

In "Z" table lets say there are 2 entries. Structure of "Z" table as follows:

TableName type tabname

fieldname type field

entries of Z table will be e.g.

TableName | fieldname

-


VBAK | SPART

MARA | MATNR

Now, i will get these two entries into internal table. Using thsese two entries i want to declare two variable dynamically. e.g.

V TYPE VBAK-SPART

V1 TYPE MARA-MATNR

V, V1 should be declare dynamically.

if the entries are 5 then program should declare five variables.

This should be done dynamically.

any Sample code???

thanks in advance.

Saurabh

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
483

There is no easy methods. You can try create dynamic structure with variables, for example. But using it is not simple.

DATA: BEGIN OF ITAB OCCURS 0,
        TABLENAME TYPE TABNAME,
        FIELDNAME TYPE FIELDNAME,
      END OF ITAB.
DATA: FCAT TYPE LVC_T_FCAT,
      HCAT TYPE LVC_S_FCAT,
      NUM.
DATA: DREF TYPE REF TO DATA,
      HEAD TYPE REF TO DATA.

FIELD-SYMBOLS: <TABLE> TYPE TABLE,
               <STRUC> TYPE ANY,
               <VARIABLE>.

INITIALIZATION.
  REFRESH ITAB.
  ITAB-TABLENAME = 'VBAK'.
  ITAB-FIELDNAME = 'SPART'.
  APPEND ITAB.
  ITAB-TABLENAME = 'MARA'.
  ITAB-FIELDNAME = 'MATNR'.
  APPEND ITAB.

START-OF-SELECTION.
* Create fieldcatalog
  LOOP AT ITAB.
    " Set variable name
    NUM = NUM + 1.
    CONCATENATE 'V' NUM INTO HCAT-FIELDNAME.

    " Set reference type
    HCAT-REF_TABLE = ITAB-TABLENAME.
    HCAT-REF_FIELD = ITAB-FIELDNAME.

    APPEND HCAT TO FCAT.
  ENDLOOP.

* Create internal table
  CALL METHOD CL_ALV_TABLE_CREATE=>CREATE_DYNAMIC_TABLE
    EXPORTING
      IT_FIELDCATALOG = FCAT
    IMPORTING
      EP_TABLE        = DREF.
  ASSIGN  DREF->* TO <TABLE>.

* Create header with variables
  CREATE DATA HEAD LIKE LINE OF <TABLE>.
  ASSIGN HEAD->* TO <STRUC>.

* Get first variable
  ASSIGN COMPONENT 'V1' OF STRUCTURE <STRUC> TO <VARIABLE>.
  SELECT SINGLE SPART FROM VBAK INTO <VARIABLE>.
  WRITE <VARIABLE>.

* Get second variable
  ASSIGN COMPONENT 'V2' OF STRUCTURE <STRUC> TO <VARIABLE>.
  SELECT SINGLE MATNR FROM MARA INTO <VARIABLE>.
  WRITE <VARIABLE>.

2 REPLIES 2
Read only

Former Member
0 Likes
484

There is no easy methods. You can try create dynamic structure with variables, for example. But using it is not simple.

DATA: BEGIN OF ITAB OCCURS 0,
        TABLENAME TYPE TABNAME,
        FIELDNAME TYPE FIELDNAME,
      END OF ITAB.
DATA: FCAT TYPE LVC_T_FCAT,
      HCAT TYPE LVC_S_FCAT,
      NUM.
DATA: DREF TYPE REF TO DATA,
      HEAD TYPE REF TO DATA.

FIELD-SYMBOLS: <TABLE> TYPE TABLE,
               <STRUC> TYPE ANY,
               <VARIABLE>.

INITIALIZATION.
  REFRESH ITAB.
  ITAB-TABLENAME = 'VBAK'.
  ITAB-FIELDNAME = 'SPART'.
  APPEND ITAB.
  ITAB-TABLENAME = 'MARA'.
  ITAB-FIELDNAME = 'MATNR'.
  APPEND ITAB.

START-OF-SELECTION.
* Create fieldcatalog
  LOOP AT ITAB.
    " Set variable name
    NUM = NUM + 1.
    CONCATENATE 'V' NUM INTO HCAT-FIELDNAME.

    " Set reference type
    HCAT-REF_TABLE = ITAB-TABLENAME.
    HCAT-REF_FIELD = ITAB-FIELDNAME.

    APPEND HCAT TO FCAT.
  ENDLOOP.

* Create internal table
  CALL METHOD CL_ALV_TABLE_CREATE=>CREATE_DYNAMIC_TABLE
    EXPORTING
      IT_FIELDCATALOG = FCAT
    IMPORTING
      EP_TABLE        = DREF.
  ASSIGN  DREF->* TO <TABLE>.

* Create header with variables
  CREATE DATA HEAD LIKE LINE OF <TABLE>.
  ASSIGN HEAD->* TO <STRUC>.

* Get first variable
  ASSIGN COMPONENT 'V1' OF STRUCTURE <STRUC> TO <VARIABLE>.
  SELECT SINGLE SPART FROM VBAK INTO <VARIABLE>.
  WRITE <VARIABLE>.

* Get second variable
  ASSIGN COMPONENT 'V2' OF STRUCTURE <STRUC> TO <VARIABLE>.
  SELECT SINGLE MATNR FROM MARA INTO <VARIABLE>.
  WRITE <VARIABLE>.

Read only

0 Likes
483

Thanks Andrey. It works.