Affiliation Platform: start earning Money with your Blog today!
 
 
 
0

One Minute, One Great ABAP Tip

This is my first post for SAPLAB.org, so I’ve decided to write about something really simple yet powerful – building ABAP Dynamic Tables using RTTS ( From Release 6.40). RTTS stands for Run Time Type Services.

Creating dynamic internal tables in ABAP programs can be very useful, dynamic coding can provide fast program running and easier maintenance. From release 6.40, RTTS has provided a new approach to dynamic table programming. Here’s an example of what I mean:

REPORT Z_1MIN_1GREATTIP_RTTS.
 
TABLES PA0002.
 
* Table type
TYPES: BEGIN OF TY_PERNR_LIST,
         PERNR TYPE PERNR_D,
         BEGDA TYPE BEGDA,
         ENDDA TYPE ENDDA,
       END   OF TY_PERNR_LIST.
 
* Dynamic Table
DATA: LO_STRUCTDESCR   TYPE REF TO CL_ABAP_STRUCTDESCR,
      LO_ELEMDESCR  TYPE REF TO CL_ABAP_ELEMDESCR,
      LO_STRUCTDESCR_NEW TYPE REF TO CL_ABAP_STRUCTDESCR,
      LO_TABLEDESCR  TYPE REF TO CL_ABAP_TABLEDESCR,
      LO_DATA     TYPE REF TO DATA,
      LT_COMPONENT     TYPE CL_ABAP_STRUCTDESCR=>COMPONENT_TABLE,
      LT_TOT_COMPONENT TYPE CL_ABAP_STRUCTDESCR=>COMPONENT_TABLE,
      LA_COMPONENT     LIKE LINE OF LT_COMPONENT.
 
* Dynamic Selection fields
TYPES: BEGIN OF TY_FIELDCATALOG,
         FIELDNAME TYPE CHAR30,
       END   OF TY_FIELDCATALOG.
*
DATA:  LT_FIELDCATALOG TYPE STANDARD TABLE OF TY_FIELDCATALOG,
       LA_FIELDCATALOG TYPE TY_FIELDCATALOG.
 
* field symbols to access the dynamic table
FIELD-SYMBOLS: <FS_TAB>   TYPE ANY TABLE,
               <FS_LINE>  TYPE ANY,
               <FS_FIELD> TYPE ANY.
*
* Selection Screen
SELECT-OPTIONS: SO_PERNR FOR PA0002-PERNR.
 
START-OF-SELECTION.
 
* 1- Getting Components of type
  LO_STRUCTDESCR ?= CL_ABAP_TYPEDESCR=>DESCRIBE_BY_NAME( 'TY_PERNR_LIST' ).
  LT_COMPONENT  = LO_STRUCTDESCR->GET_COMPONENTS( ).
  APPEND LINES OF LT_COMPONENT TO LT_TOT_COMPONENT.
 
*   Element Description Last Name
  LO_ELEMDESCR ?= CL_ABAP_ELEMDESCR=>DESCRIBE_BY_NAME( 'PAD_NACHN' ).
*
*   Field name
  LA_COMPONENT-NAME = 'NACHN'.
*
*   Field type
  LA_COMPONENT-TYPE = CL_ABAP_ELEMDESCR=>GET_C(
                    P_LENGTH   = LO_ELEMDESCR->LENGTH ).
*
*   Filling the component table
  APPEND LA_COMPONENT TO LT_TOT_COMPONENT.
  CLEAR: LA_COMPONENT.
 
*   Element Description First Name
  LO_ELEMDESCR ?= CL_ABAP_ELEMDESCR=>DESCRIBE_BY_NAME( 'PAD_VORNA' ).
*
*   Field name
  LA_COMPONENT-NAME = 'VORNA'.
*
*   Field type
  LA_COMPONENT-TYPE = CL_ABAP_ELEMDESCR=>GET_C(
                     P_LENGTH   = LO_ELEMDESCR->LENGTH ).
*
*   Filling the component table
  APPEND LA_COMPONENT TO LT_TOT_COMPONENT.
  CLEAR: LA_COMPONENT.
 
*
* 3. Create a New Type
  LO_STRUCTDESCR_NEW = CL_ABAP_STRUCTDESCR=>CREATE( LT_TOT_COMPONENT ).
*
* 4. New Table type
  LO_TABLEDESCR = CL_ABAP_TABLEDESCR=>CREATE(
                  P_LINE_TYPE  = LO_STRUCTDESCR_NEW
                  P_TABLE_KIND = CL_ABAP_TABLEDESCR=>TABLEKIND_STD
                  P_UNIQUE     = ABAP_FALSE ).
*
* 5. data to handle the new table type
  CREATE DATA LO_DATA TYPE HANDLE LO_TABLEDESCR.
*
* 6. New internal table in the fieldsymbols
  ASSIGN LO_DATA->* TO <FS_TAB>.
*
*$*$*...............Dynamic Selection.............................*$*$*
* Filling up the table for the Selection fields of Select Query
  LOOP AT LT_TOT_COMPONENT INTO LA_COMPONENT.
    LA_FIELDCATALOG-FIELDNAME = LA_COMPONENT-NAME.
    APPEND LA_FIELDCATALOG TO LT_FIELDCATALOG.
    CLEAR: LA_COMPONENT, LA_FIELDCATALOG.
  ENDLOOP.
*
* Selecting data
  SELECT (LT_FIELDCATALOG)
         INTO  TABLE <FS_TAB>
         FROM  PA0002
         UP TO 10 ROWS
         WHERE PERNR IN SO_PERNR.
*
*$*$*...............Accessing dynamic table.......................*$*$*
  LOOP AT <FS_TAB> ASSIGNING <FS_LINE>.
    ASSIGN COMPONENT 'VORNA' OF STRUCTURE <FS_LINE> TO <FS_FIELD>.
    CONCATENATE 'Mr.' <FS_FIELD> INTO <FS_FIELD> SEPARATED BY SPACE.
  ENDLOOP.
*
*
*$*$*...............Displaying using SALV model...................*$*$*
*
  DATA: LO_ALV TYPE REF TO CL_SALV_TABLE.
*
  TRY.
      CL_SALV_TABLE=>FACTORY(
        EXPORTING
          LIST_DISPLAY = ABAP_FALSE
        IMPORTING
          R_SALV_TABLE = LO_ALV
        CHANGING
          T_TABLE      = <FS_TAB> ).
    CATCH CX_SALV_MSG .
  ENDTRY.
*
  LO_ALV->DISPLAY( ).

This is a good and quick way to create a dynamic ALV report in just a few lines of code. Please be sure to read up next on “1 minute – 1 Great Tip”: ECC6 Enhancement Framework in 100 lines.

About the author

Helder Goncalves (hfgoncalves) has been working with ABAP development for more than 9 years, covering all major modules, releases and technologies. Click here to view Helder’s full profile.

Author : Antonio Caldas

Author's Website | Articles from

Working as an Information Systems Consultant for over 10 years, I've found that working with SAP brought me added value on how to deal and manage IT and Information Systems Projects. More than a hobby, this blog is aimed to propagate SAP knowledge exchange and help other SAP technology or functional Consultants finding any piece of useful information. Please participate with your comments and opinions, it will help enrich the SAP community. Thanks!


Related Posts






Community Feeds

  • Java Online Training

    very nice post thanks for sharing this article.
    Thanks
    Training Aspirants
    http://www.trainingaspirants.com

  • informatica online training/obiee online training

    We are providing the best online training by industrial IT experts with one free demo class, real time coaching and placement assistance.

  • Sap

    TallyMarks Consulting has successfully implemented SAP BI and Business Objects in a record time of 5 months at Getz Pharma. For more details on this cost effective project by the market leader, visit: http://www.tallymarks.co/

  • Learn SAP

    Refer this blog to learn some useful tips on various SAP customizing areas.

  • Free and premium SAP Training

    SAP training for all skill levels – complete beginner to very experienced. Articles, Videos and downloadable content.

  • SAP Blog

    SAP centric blog based on practical experiences of an admin

  • SAP Jobs

    Red Commerce is a leading recruitment agency which specialises in sourcing quality staff for the SAP industry. Our site is full of useful information and we are always on hand to answer any queries you may have.

  • SAP Online Training on all Major IT Courses.

    Training Aspirants provide Online Training & Corporate Training. We make sure for a right class size in order to make Online sessions most interactive.
    Training Aspirants started with a motto to provide quality and effective online training for the students worldwide with the best instructors.

    For Details Visit: http://www.trainingaspirants.com

    Call Us: US :001-713-900-7669
    US: 001-630-974-1794
    India : 091-779-985-5779
    Mail Us: mail@trainingaspirants.com

  • Expertise technique SAP, ABAP

    Expert Technique SAP, ABAP

  • SAP Training and Certification Courses

    Be a part of SAP training and online sap certification to boost up your skills and career. SAP training helps to improve your business methodologies which are beneficial to generate business around the world. JKT SAP Academy is an authorize education partner of sap and provide sap training for sap abap, sap erp, sap basis, sap fi,sap co, sap mm, sap sd, sap pp and other functional modules.

Submit More
;;