Dynamic table creation using RTTS.

I was reading the blog published by Pieter Lemaire on (Dynamic tables in ALV with RTTI); there he has explained some dynamic functionality using RTTS – I thought the way he was creating the dynamic internal tables using RTTS could be achieved in a much simpler way; therefore I thought of writing this blog to show that how easily we can generate dynamic internal tables & work areas from user definitions using RTTS.  Please see the code developed below.

PARAMETERS:: p_table(30) TYPE c.

DATA:

      it_component TYPE abap_component_tab,
      wa_component
TYPE abap_componentdescr,
      o_ref_type
TYPE REF TO cl_abap_typedescr,
      o_ref_struct
TYPE REF TO cl_abap_structdescr,
      o_ref_table
TYPE REF TO cl_abap_tabledescr,
      o_table
TYPE REF TO data,
      o_workarea
TYPE REF TO data.

FIELD-SYMBOLS:
               <fs>
TYPE any,
               <fs_table>
TYPE ANY TABLE.

*Calling this method to get the TYPE Definition.

cl_abap_typedescr=>describe_by_name(
 
EXPORTING
    p_name        
= p_table
  RECEIVING
    p_descr_ref   
= o_ref_type
 
EXCEPTIONS
   type_not_found
= 1 ).

CHECK o_ref_type IS BOUND.
o_ref_struct ?= o_ref_type
.

*Calling to get the components of the structure
it_component[]
= o_ref_struct->get_components( ).

IF it_component[] IS NOT INITIAL.
 
CLEAR:
        o_ref_struct
.

*Factory Method
  o_ref_struct
= cl_abap_structdescr=>create( it_component ).

  CHECK o_ref_struct IS BOUND.
 
CREATE DATA o_workarea TYPE HANDLE o_ref_struct.
 
ASSIGN o_workarea->* TO <fs>.

*Factory Method.
  o_ref_table
= cl_abap_tabledescr=>create(
  p_line_type
= o_ref_struct
  p_table_kind
= cl_abap_tabledescr=>tablekind_std ).

  CHECK o_ref_table IS BOUND.
 
CREATE DATA o_table TYPE HANDLE o_ref_table.
 
ASSIGN o_table->* TO <fs_table>.

ENDIF.



To report this post you need to login first.

3 Comments

You must be Logged on to comment or reply to a post.

  1. Miroslav Oprsteny

    Hi Tanmoy,

    this is really great article!!!

    But I’d personally extend it with info that this approach works for DDIC defined tables only.

    I’m affraid this can’t be used for ITABs (p_name can’t contain a name of some of ITABs used in your program).

    Recently I faced similar problem where I needed to access dynamically created ITAB and extend it with some additional fields (How to append a component to ITAB dynamically).

    Basically to get your it_components of your itab during runtime like in this code:

    DATA:

      mr_data TYPE REF TO data, " Our dynamic ITAB with data

      lr_data_line TYPE REF TO data.

    FIELD-SYMBOLS:

      <fs_table> TYPE STANDARD TABLE,

      <fs_comp>  TYPE abap_componentdescr.

    *Create dynamic line of this itab

      ASSIGN mr_data->* TO <fs_table>.

      CREATE DATA lr_data_line LIKE LINE OF <fs_table>.

    *We are creating components of the new/updated ITAB

      APPEND INITIAL LINE TO it_component ASSIGNING <fs_comp>.

    *We get description of the current itab

    <fs_comp>-type ?= cl_abap_structdescr=>describe_by_data_ref( lr_data_line ).

    *We assign some dummy name (will not be used anyhow)

    <fs_comp>-name = 'DATA'.

    *Include all fields (result will be fields of new itab

    *If 'as_include' = abap_false, then all fields of the structure

    *would be added in our new itab as field of type

    *"Internal table" with name 'DATA'

    <fs_comp>-as_include = abap_true.

    Now you can use yours:

    o_ref_struct = cl_abap_structdescr=>create( it_component ).
    CHECK o_ref_struct IS BOUND.

    o_ref_table = cl_abap_tabledescr=>create(
    p_line_type
    = o_ref_struct
    p_table_kind
    = cl_abap_tabledescr=>tablekind_std ).
    CHECK o_ref_table IS BOUND.

    (0) 
  2. Suhas Saha

    Hello Tanmoy,

    Firstly this is not something new you’re discussing here. RTTS APIs & their usage has been discussed at length in the ABAP forums. I decided not to delete it because someone had put in an elaborate comment.

    I thought the way he was creating the dynamic internal tables using RTTS could be achieved in a much simpler way

    Regarding creating runtime components from DDIC elements, it is not the “simplest” way you have discussed here. A single line of code is enough & you don’t need an RTTC API for this.

    1. PARAMETERS p_ddic TYPE typename OBLIGATORY DEFAULT ‘SFLIGHT’.
    2. DATA gdref TYPE REF TO data.
    3. TRY .
    4.     CREATE DATA gdref TYPE STANDARD TABLE OF (p_ddic).
    5.     ASSIGN gdref->* TO FIELDSYMBOL(<itab>).
    6.     CLEAR gdref.
    7.     CREATE DATA gdref TYPE (p_ddic).
    8.     ASSIGN gdref->* TO FIELDSYMBOL(<wa>).
    9.   CATCH cx_sy_create_data_error ##no_handler.
    10. ENDTRY.
    (0) 

Leave a Reply