Dynamic ITAB migration from create_dynamic_table to RTTS

By | Last Updated on July 18, 2013 | 1,618

RTTS is around for years in ABAP now. Code Snippet to generate Dynamic Internal Table using RTTS which was previously developed using Field Catalog.

RTTS is available from release 640 but many developers still uses the old approach. Code Snippet to help you migrate your code from Field Catalog to RTTS to generate the Dynamic ITAB.

Utility Class

Utility class to migrate dynamic itab previously created using cl_alv_table_create=>create_dynamic_table to RTTS cl_abap_tabledescr=>create.

 
*
CLASS zcl_rtts_itab DEFINITION.
  PUBLIC SECTION.
    METHODS:
      create_from_field_catalog
        IMPORTING
          it_fcat TYPE lvc_t_fcat
        RETURNING value(ro_data) TYPE REF TO data.
ENDCLASS.                    "zcl_RTTS_ITAB DEFINITION
*
CLASS zcl_rtts_itab IMPLEMENTATION.
  METHOD create_from_field_catalog.
*&---------------------------------------------------------------------*
*& Purpose - Utility to convert dynamic table using RTTS from
*&           create_dynamic_table
*&           to RTTS cl_abap_tabledescr=>create
*& Author  - Naimesh Patel
*& URL     - http://zevolving.com/?p=2189
*&---------------------------------------------------------------------*
 
* DD_ROLL = Data Element
* FIELDNAME = Field name
 
* Dynamic Table creation
    DATA: lo_struct   TYPE REF TO cl_abap_structdescr,
          lo_element  TYPE REF TO cl_abap_elemdescr,
          lo_new_type TYPE REF TO cl_abap_structdescr,
          lo_new_tab  TYPE REF TO cl_abap_tabledescr,
          lt_comp     TYPE cl_abap_structdescr=>component_table,
          lt_tot_comp TYPE cl_abap_structdescr=>component_table,
          la_comp     LIKE LINE OF lt_comp.
 
*=========== using RTTS ==============================
* Get Field info from Field-Catalog
    LOOP AT lt_fieldcat INTO la_fieldcat.
*   Element Description
      lo_element ?= cl_abap_elemdescr=>describe_by_name( la_fieldcat-dd_roll ).
      la_comp-type = lo_element.
*   Field name
      la_comp-name = la_fieldcat-fieldname.
*
*   Filling the component table
      APPEND la_comp TO lt_tot_comp.
      CLEAR: la_comp.
    ENDLOOP.
 
* 3. Create a New Type
    lo_new_type = cl_abap_structdescr=>create( lt_tot_comp ).
*
* 4. New Table type
    lo_new_tab = cl_abap_tabledescr=>create(
                    p_line_type  = lo_new_type
                    p_table_kind = cl_abap_tabledescr=>tablekind_std
                    p_unique     = abap_false ).
*
* 5. data to handle the new table type
    CREATE DATA ro_data TYPE HANDLE lo_new_tab.
 
 
  ENDMETHOD.                    "create_from_field_catalog
ENDCLASS.                    "zcl_RTTS_ITAB IMPLEMENTATION
 

Demo Program

 
REPORT znp_demo_rtts_from_fcat.
*&---------------------------------------------------------------------*
*& Purpose - Demo program Utility to migrate from create_dynamic_table
*&           to RTTS cl_abap_tabledescr=>create
*& Author  - Naimesh Patel
*& URL     - http://zevolving.com/?p=2189
*&---------------------------------------------------------------------*
 
*
DATA: lo_data     TYPE REF TO data,
      lf_months   TYPE monat,
      lf_run_mon  TYPE monat.
*
* Dynamic Selection fields
TYPES: BEGIN OF ty_fields,
       field TYPE char30,
       END   OF ty_fields.
*
DATA:  lt_fields TYPE STANDARD TABLE OF ty_fields,
       la_fields TYPE ty_fields.
*
* field symbols to access the dynamic table
FIELD-SYMBOLS: <f_tab>   TYPE ANY TABLE.
*
DATA: lt_fieldcat TYPE lvc_t_fcat,
      la_fieldcat TYPE lvc_s_fcat.
 
*
FIELD-SYMBOLS: <f_tab_new> TYPE ANY TABLE.
DATA: lo_data_new TYPE REF TO data.
* Selection Screen
PARAMETERS: p_mon_fr TYPE monat,
            p_mon_to TYPE monat.
*
START-OF-SELECTION.
*=========== using the create_dynamic_table ==================
 
* 1. Adding fields in the field catalog
  la_fieldcat-fieldname = 'KSTAR'.
  la_fieldcat-datatype  = 'CHAR'.
  la_fieldcat-dd_roll   = 'KSTAR'.
  la_fieldcat-outputlen = 10.
  APPEND la_fieldcat TO lt_fieldcat.
  CLEAR  la_fieldcat.
*
* 2. Adding required fields based on the selection months
* Determining Number of fields
  lf_months = ( p_mon_to - p_mon_fr ) + 1.
  lf_run_mon = p_mon_fr.
*
  DO lf_months TIMES.
*   Field name
    CONCATENATE 'WTG0' lf_run_mon INTO la_fieldcat-fieldname.
    la_fieldcat-datatype  = 'CURR'.
    la_fieldcat-outputlen = 17.
    la_fieldcat-dd_roll   = 'WTGXXX'.
*
*   Filling the fieldcatalog
    APPEND la_fieldcat TO lt_fieldcat.
    CLEAR  la_fieldcat.
*
    lf_run_mon = lf_run_mon + 1.
  ENDDO.
*
* Calling method to generate the dynamic internal table based on
* ALV field catalog
  cl_alv_table_create=>create_dynamic_table(
    EXPORTING
      it_fieldcatalog = lt_fieldcat
    IMPORTING
      ep_table = lo_data ).
*
  ASSIGN lo_data->* TO <f_tab>.
 
*=========== using RTTS ==============================
  DATA: lo_rtts_tab TYPE REF TO zcl_rtts_itab.
  CREATE OBJECT lo_rtts_tab.
  lo_data_new = lo_rtts_tab->create_from_field_catalog( lt_fieldcat ).
* Get in Field Symbol to work local
  ASSIGN lo_data_new->* TO <f_tab_new>.
 
  WRITE: 'done'.
 

Link to Dynamic ITAB using RTTS

Do you have a Code Snippet which you want to share, Submit Code Snippet here

Share It!

Don't miss an Update

Naimesh Patel{234 articles}

I'm SAP ABAP Consultant for more than a decade. I like to experiment with ABAP especially OO. I have been SDN Top Contributor.
Follow :

Explore all of his 234 articles.

Load comments

4 Comments

  • steve oldner

    Thanks Naimesh!

    Only one did I have a need to dynamically create an itab, so this will come in very handy.

  • Smriti Mukherjee

    Hi Naimesh,
    Thanks for this post, will be very helpful.
    I am currently working on a report which determines some column dynamically based on user inputs and custom table entries. This is SAP HR report and pulls huge volume of data and displays around 75 columns.
    The dynamic column determination process is affecting performance terribly. This was not written using RTTS but create dynamic table etc . DO loops were written to populated the static fields in the dynamic structure thereby negatively impacting the performance to a great extent.
    Do you think using the process you have mentioned above would have impact on report performance?
    Please suggest, many thanks :)

  • Hello Smriti,

    Dynamic table creation using RTTS would be faster definitely.

    But when you have lot of data to be loaded, getting to each field would take a while as you would need to get to the Field level. When you use RTTS, you can create the a Flat structure as well. This structure can be used as the Work area to append the data in the internal table.

    For huge dynamic data load, I would suggest to generate a subroutine pool. As I have mentioned in one of my comment , data load programs in BW uses the Dynamic Subroutines to load the program as they need to load lot of data.

    This would give a benefit as you don’t need to get to the field level and avoid having the nested loop: Data Loop and Field level loop. You can have the subroutine pool written in a way that it would generate the subroutines with the field names to append data to your ITAB.

    Thanks,
    Naimesh Patel

  • Smriti Mukherjee

    Thanks so much Naimesh,
    trying out your suggestion to create dynamic subroutine poool..thanks for your awesome blog!
    -Smriti Mukherjee

Comments on this Post are now closed. If you have something important to share, you can always contact me.