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

how do we create data objects dyanamically

Former Member
0 Likes
4,613

Regards,

Srinu

1 ACCEPTED SOLUTION
Read only

former_member199581
Active Participant
2,231

Hi Srinu,

take a look at this sample method, who's taking values from a table passed by name and put them into a Transport Request.

The method doesn't know the passed table type, so it creates a structure based on info taken by RTTI using RTTS.


* // Reference to the table
  strucRef ?= cl_abap_structdescr=>describe_by_name( objName ).
* // Here, we know the table name, so we only take the components and create
* // a structure:
* // Get key fields since e071k need only the keys:
  fields = strucRef->get_ddic_field_list( ).
  LOOP AT fields INTO field WHERE keyflag EQ abap_true.
    key-name        = field-fieldname.
    key-type       ?= cl_abap_datadescr=>describe_by_name( key-name ).
    key-as_include  = abap_false.
    key-suffix      = abap_false.
    APPEND key TO keys.
  ENDLOOP.
* // Create the structure for keys:
  keyRef = cl_abap_structdescr=>create( keys ).
  CREATE DATA keystr TYPE HANDLE keyRef.
  ASSIGN keystr->* TO <strKey>.

* // Now, we have a structure that can get key fields:
* // Get data
  ASSIGN table->* TO <insertedRows>. "--> Here I have data
  LOOP AT <insertedRows> ASSIGNING <line>.
    move-corresponding <line> TO <strKey>.
* // Fill E071 buffer
    wa071k-pgmid      = 'R3TR'.
    wa071k-object     = 'TABU'.  "Table Contents
    wa071k-objname    = objName.
    wa071k-mastertype = 'TABU'.
    wa071k-mastername = objName.
    wa071k-tabkey     = <strKey>.
    COLLECT wa071k INTO e071k_buf.
  ENDLOOP.

If you don't have a template table, and want to create a totally dynamic table:


DATA: structdescr TYPE REF TO cl_abap_structdescr,
      tabledescr  TYPE REF TO cl_abap_tabledescr.
DATA: comptab     TYPE abap_component_tab,
      compline    TYPE abap_componentdescr.
DATA: tabRef      TYPE REF TO data,
      linRef      TYPE REF TO data.

* // Fill Component Table
compline-name        = 'PERNR'.
compline-type       ?= cl_abap_typedescr=>describe_by_name( 'PERNR_D' ).
compline-as_include  = abap_false.
compline-suffix      = space.
APPEND compline TO comptab.

compline-name        = 'ENAME'.
compline-type       ?= cl_abap_typedescr=>describe_by_name( 'EMNAM' ).
compline-as_include  = abap_false.
compline-suffix      = space.
APPEND compline TO comptab.

* // Create a structure
structdescr = cl_abap_structdescr=>create( comptab ).
* // Then create a table
tabledescr = cl_abap_tabledescr=>create( structdescr ).

* // Create a data-ref type the new table created
CREATE DATA tabRef TYPE HANDLE tabledescr.
* // De-Reference the variable to a field-symbol
ASSIGN tabRef->* TO <table>. "Here you have the table:

Or this one, doing the old way:


* // Set tablename
  objName = xtabname-tabname. "--> Table Name

* // Now check table existance:
  CALL FUNCTION 'DDIF_TABL_GET'
    EXPORTING
      name          = xtabname-tabname
    IMPORTING
      dd02v_wa      = xdd02v
    TABLES
      dd03p_tab     = idd03p
    EXCEPTIONS
      illegal_input = 1
      OTHERS        = 2.
  IF sy-subrc <> 0 OR xdd02v-tabname IS INITIAL.
    RAISE EXCEPTION TYPE zcx_table_import_export
      EXPORTING textid = zcx_table_import_export=>table_doesnt_exists
                table  = xtabname-tabname.
  ENDIF.

* // Create dynamic table and wa
  CREATE DATA drefTab TYPE TABLE OF (xdd02v-tabname).
  ASSIGN drefTab->* TO <dynTab>.  "--> This is your table:
  CREATE DATA drefWa  LIKE LINE  OF <dynTab>.
  ASSIGN drefWa->*  TO <dynWa>. "--> This is the table work area

Hope this helps,

Roby.

4 REPLIES 4
Read only

uwe_schieferstein
Active Contributor
0 Likes
2,231

Hello Srinu

Perhaps the following links may be useful:

<a href="https://www.sdn.sap.comhttp://www.sdn.sap.comhttp://www.sdn.sap.com/irj/sdn/wiki?path=/display/snippets/creating%2bflat%2band%2bcomplex%2binternal%2btables%2bdynamically%2busing%2brtti">creating Flat and Complex Internal Tables Dynamically using RTTI - Code Gallery - Wiki</a>

<a href=" of table of table dynamically</a>

Regards,

Uwe

Read only

Former Member
0 Likes
2,231

Hi Srinivas,

check this sample program............

REPORT ZCLUST1 .

  • Example: how to create a dynamic internal table

  • The dynamic internal table stucture

DATA: BEGIN OF STRUCT OCCURS 10,

FILDNAME(8) TYPE C,

ABPTYPE TYPE C,

LENGTH TYPE I,

END OF STRUCT.

  • The dynamic program source table

DATA: BEGIN OF INCTABL OCCURS 10,

LINE(72),

END OF INCTABL.

DATA: LNG TYPE I, TYPESRTING(6).

  • Sample dynamic internal table stucture

STRUCT-FILDNAME = 'field1'. STRUCT-ABPTYPE = 'c'. STRUCT-LENGTH = '6'.

APPEND STRUCT. CLEAR STRUCT.

STRUCT-FILDNAME = 'field2'. STRUCT-ABPTYPE = 'd'.

APPEND STRUCT. CLEAR STRUCT.

STRUCT-FILDNAME = 'field3'. STRUCT-ABPTYPE = 'i'.

APPEND STRUCT. CLEAR STRUCT.

  • Create the dynamic internal table definition in the dyn. program

INCTABL-LINE = 'program zdynpro.'. APPEND INCTABL.

INCTABL-LINE = 'data: begin of dyntab occurs 10,'. APPEND INCTABL.

LOOP AT STRUCT.

INCTABL-LINE = STRUCT-FILDNAME.

LNG = STRLEN( STRUCT-FILDNAME ).

IF NOT STRUCT-LENGTH IS INITIAL .

TYPESRTING(1) = '('.

TYPESRTING+1 = STRUCT-LENGTH.

TYPESRTING+5 = ')'.

CONDENSE TYPESRTING NO-GAPS.

INCTABL-LINE+LNG = TYPESRTING.

ENDIF.

INCTABL-LINE+15 = 'type '.

INCTABL-LINE+21 = STRUCT-ABPTYPE.

INCTABL-LINE+22 = ','.

APPEND INCTABL.

ENDLOOP.

INCTABL-LINE = 'end of dyntab. '.

APPEND INCTABL.

  • Create the code processes the dynamic internal table

INCTABL-LINE = ' '. APPEND INCTABL.

INCTABL-LINE = 'dyntab-field1 = ''aaaaaa''.'. APPEND INCTABL.

INCTABL-LINE = 'dyntab-field1 = ''19970814''.'. APPEND INCTABL.

INCTABL-LINE = 'dyntab-field1 = 1.'. APPEND INCTABL.

INCTABL-LINE = 'append dyntab.'. APPEND INCTABL.

INCTABL-LINE = ' '. APPEND INCTABL.

INCTABL-LINE = 'loop at dyntab.'. APPEND INCTABL.

INCTABL-LINE = 'write: / dyntab.'. APPEND INCTABL.

INCTABL-LINE = 'endloop.'. APPEND INCTABL.

  • Create and run the dynamic program

INSERT REPORT 'zdynpro'(001) FROM INCTABL.

SUBMIT ZDYNPRO.

-


or Just try out this simpler dynamic internal tables

DATA: itab TYPE STANDARD TABLE OF spfli,

wa LIKE LINE OF itab.

DATA: line(72) TYPE c,

list LIKE TABLE OF line(72).

START-OF-SELECTION.

*line = ' CITYFROM CITYTO '.

line = ' AIRPTO '.

APPEND line TO list.

SELECT DISTINCT (list)

INTO CORRESPONDING FIELDS OF TABLE itab

FROM spfli.

IF sy-subrc EQ 0.

LOOP AT itab INTO wa.

  • WRITE: / wa-cityfrom, wa-cityto.

WRITE 😕 wa-airpto.

ENDLOOP.

ENDIF.

<b>Kindly reward points if you found the reply helpful.<b>

Cheers,

Chaitanya.

Read only

former_member199581
Active Participant
2,232

Hi Srinu,

take a look at this sample method, who's taking values from a table passed by name and put them into a Transport Request.

The method doesn't know the passed table type, so it creates a structure based on info taken by RTTI using RTTS.


* // Reference to the table
  strucRef ?= cl_abap_structdescr=>describe_by_name( objName ).
* // Here, we know the table name, so we only take the components and create
* // a structure:
* // Get key fields since e071k need only the keys:
  fields = strucRef->get_ddic_field_list( ).
  LOOP AT fields INTO field WHERE keyflag EQ abap_true.
    key-name        = field-fieldname.
    key-type       ?= cl_abap_datadescr=>describe_by_name( key-name ).
    key-as_include  = abap_false.
    key-suffix      = abap_false.
    APPEND key TO keys.
  ENDLOOP.
* // Create the structure for keys:
  keyRef = cl_abap_structdescr=>create( keys ).
  CREATE DATA keystr TYPE HANDLE keyRef.
  ASSIGN keystr->* TO <strKey>.

* // Now, we have a structure that can get key fields:
* // Get data
  ASSIGN table->* TO <insertedRows>. "--> Here I have data
  LOOP AT <insertedRows> ASSIGNING <line>.
    move-corresponding <line> TO <strKey>.
* // Fill E071 buffer
    wa071k-pgmid      = 'R3TR'.
    wa071k-object     = 'TABU'.  "Table Contents
    wa071k-objname    = objName.
    wa071k-mastertype = 'TABU'.
    wa071k-mastername = objName.
    wa071k-tabkey     = <strKey>.
    COLLECT wa071k INTO e071k_buf.
  ENDLOOP.

If you don't have a template table, and want to create a totally dynamic table:


DATA: structdescr TYPE REF TO cl_abap_structdescr,
      tabledescr  TYPE REF TO cl_abap_tabledescr.
DATA: comptab     TYPE abap_component_tab,
      compline    TYPE abap_componentdescr.
DATA: tabRef      TYPE REF TO data,
      linRef      TYPE REF TO data.

* // Fill Component Table
compline-name        = 'PERNR'.
compline-type       ?= cl_abap_typedescr=>describe_by_name( 'PERNR_D' ).
compline-as_include  = abap_false.
compline-suffix      = space.
APPEND compline TO comptab.

compline-name        = 'ENAME'.
compline-type       ?= cl_abap_typedescr=>describe_by_name( 'EMNAM' ).
compline-as_include  = abap_false.
compline-suffix      = space.
APPEND compline TO comptab.

* // Create a structure
structdescr = cl_abap_structdescr=>create( comptab ).
* // Then create a table
tabledescr = cl_abap_tabledescr=>create( structdescr ).

* // Create a data-ref type the new table created
CREATE DATA tabRef TYPE HANDLE tabledescr.
* // De-Reference the variable to a field-symbol
ASSIGN tabRef->* TO <table>. "Here you have the table:

Or this one, doing the old way:


* // Set tablename
  objName = xtabname-tabname. "--> Table Name

* // Now check table existance:
  CALL FUNCTION 'DDIF_TABL_GET'
    EXPORTING
      name          = xtabname-tabname
    IMPORTING
      dd02v_wa      = xdd02v
    TABLES
      dd03p_tab     = idd03p
    EXCEPTIONS
      illegal_input = 1
      OTHERS        = 2.
  IF sy-subrc <> 0 OR xdd02v-tabname IS INITIAL.
    RAISE EXCEPTION TYPE zcx_table_import_export
      EXPORTING textid = zcx_table_import_export=>table_doesnt_exists
                table  = xtabname-tabname.
  ENDIF.

* // Create dynamic table and wa
  CREATE DATA drefTab TYPE TABLE OF (xdd02v-tabname).
  ASSIGN drefTab->* TO <dynTab>.  "--> This is your table:
  CREATE DATA drefWa  LIKE LINE  OF <dynTab>.
  ASSIGN drefWa->*  TO <dynWa>. "--> This is the table work area

Hope this helps,

Roby.

Read only

Former Member
0 Likes
2,231

Hi Guys --all you have to do is pass your data structure using the RTTI functionality.

For example

types: begin of s_elements,

tabname type dd02l-tabname,

tabclass type dd02l-tabclass,

as4user type dd02l-as4user,

as4date type dd02l-as4date,

as4time type dd02l-as4time,

viewed(1) type c.

  • drop_down_handle type int4.

types: end of s_elements.

create data dref type s_elements.

assign dref->* to <fs>.

invoker = sy-repid.

*This field symbol <fs> will contain your

  • data structure from which you construct a field catalog.

*I do it by calling methods in a class.

In your class define some data

data lr_rtti_struc type ref to cl_abap_structdescr .

data:

zog like line of lr_rtti_struc->components .

data:

zogt like table of zog .

data wa_it_fldcat type lvc_s_fcat .

data it_fldcat type lvc_t_fcat .

data dy_table type ref to data .

data dy_line type ref to data .

*Next build an FCAT and dynamic table base on your input structure.

call method z_object->build_dynamic_structures

exporting

my_line = my_line

calling_program = invoker

importing

dy_table = dy_table

changing

it_fldcat = it_fldcat.

method build_dynamic_structures.

caller = calling_program.

call method me->return_structure

exporting

my_line = my_line. " This is your data structure

call method me->create_dynamic_fcat

importing

it_fldcat = it_fldcat.

method return_structure.

lr_rtti_struc ?= cl_abap_structdescr=>describe_by_data( my_line ).

zogt[] = lr_rtti_struc->components.

  • ...

endmethod.

Table ZOGT now has the definitions in it so we can build the field catalog

method create_dynamic_fcat.

loop at zogt into zog.

clear wa_it_fldcat.

wa_it_fldcat-fieldname = zog-name .

wa_it_fldcat-datatype = zog-type_kind.

wa_it_fldcat-inttype = zog-type_kind.

wa_it_fldcat-intlen = zog-length.

wa_it_fldcat-decimals = zog-decimals.

wa_it_fldcat-coltext = zog-name.

wa_it_fldcat-lowercase = 'X'.

append wa_it_fldcat to it_fldcat .

endloop.

  • .. Add any exta field catalog stuff you want to add but the above method contains the basic for any (non deep) structure whether the elements are in DDIC or not.

  • create your dynmic table

call method me->create_dynamic_table

exporting

it_fldcat = it_fldcat

importing

dy_table = dy_table.

endmethod

method create_dynamic_table.

call method cl_alv_table_create=>create_dynamic_table

exporting

it_fieldcatalog = it_fldcat

importing

ep_table = dy_table.

  • ...

endmethod.

Now you can populate it such as

form populate_dynamic_itab changing dy_table.

data tabx like dd02l-tabname.

assign dy_table->* to <dyn_table>.

create data dy_line like line of <dyn_table>.

assign dy_line->* to <dyn_wa>.

select *

from dd02l

into corresponding fields of table <dyn_table>

where tabname like 'ZHR%'.

endform

  • ...

Cheers

Jimbo