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:    TYPE ANY TABLE,
                 TYPE ANY,
                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 .
*
*$*$*...............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 
         FROM  PA0002
         UP TO 10 ROWS
         WHERE PERNR IN SO_PERNR.
*
*$*$*...............Accessing dynamic table.......................*$*$*
  LOOP AT  ASSIGNING .
    ASSIGN COMPONENT 'VORNA' OF STRUCTURE  TO .
    CONCATENATE 'Mr.'  INTO  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      =  ).
    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 : webmaster

Author's Website | Articles from webmaster

Working as an Information Systems Consultant for over 10 years now, 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!

Like this post? Share it!

  • Tweet
  • Facebook
  • Diggit
  • Delicious
  • Diggit
  • Diggit
  • Diggit
  • Diggit
  • Diggit

Related Posts





Community Feeds

Submit More
;;