Working with Multiple dynamic internal tables

By Bhumika Mahawar, YASH Technologies

The main objective of this article is not to show how to display two ALV’s on a single page rather it mainly focuses on the use of dynamic tables in ALV.

My requirement was to create two dynamic table and display data into them.

The first dynamic table was created in the same way as already known using the method “create_dynamic_table” but the problem was the second dynamic table creation. 

To do this, you don’t need to use the same method again rather the RTTC concept helps us in the same. The below code illustrates the same and displays the data in an ALV as shown in the diagram below. 

REPORT  ztest.
****  Data declarations                                      
DATA:
  dtab           TYPE REF TO data,
  newstr2        TYPE REF TO cl_abap_typedescr,
  tab_type1      TYPE REF TO cl_abap_tabledescr,
  lref_ditab     TYPE REF TO data,
  lref_new_line  TYPE REF TO data.
**** Field-Symbols declarations
FIELD-SYMBOLS:
 <fs_dyn_tab1> TYPE  ANY TABLE,
 <fs_dyn_tab2> TYPE ANY TABLE,
 <fs_dyn_wa>   TYPE ANY.
**** Field Catalog declarations
DATA:
  ls_fcat  TYPE lvc_s_fcat,
  lt_fcat  TYPE lvc_t_fcat.
****  Data declarations for ALV Display                                  *
**** Object variable for ALV grid
DATA:
  obj_my_alv_grid TYPE REF TO cl_gui_alv_grid.
**** Object variable for ALV Container
DATA:
  obj_r_container TYPE REF TO cl_gui_custom_container.
*---------------------------------------------------------------------*
*                  START O F   S E L E C T I O N                      *
*---------------------------------------------------------------------*
START-OF-SELECTION.
* Create Field Catalog
  PERFORM create_field_catalog.
* Create Dynamic Table
  PERFORM create_dynamic_table.
* Fill the dynamic tables with data
  PERFORM fill_dynamic_table.
* Call the screen no.100
  PERFORM call_screen.
*&---------------------------------------------------------------------*
*&      Form  CREATE_FIELD_CATALOG
*&---------------------------------------------------------------------*
FORM create_field_catalog .
* Append fields to field catalog table
  ls_fcat-fieldname = 'VBELN'.
  ls_fcat-ref_field = 'VBELN'.
  ls_fcat-ref_table = 'VBAK'.
  APPEND ls_fcat TO lt_fcat.
  CLEAR ls_fcat.
  ls_fcat-fieldname = 'AUART'.
  ls_fcat-ref_field = 'AUART'.
  ls_fcat-ref_table = 'VBAK'.
  APPEND ls_fcat TO lt_fcat.
  CLEAR ls_fcat.
  ls_fcat-fieldname = 'ERNAM'.
  ls_fcat-ref_field = 'ERNAM'.
  ls_fcat-ref_table = 'VBAK'.
  APPEND ls_fcat TO lt_fcat.
  CLEAR ls_fcat.
  ls_fcat-fieldname = 'ERDAT'.
  ls_fcat-ref_field = 'ERDAT'.
  ls_fcat-ref_table = 'VBAK'.
  APPEND ls_fcat TO lt_fcat.
  CLEAR ls_fcat.
  ls_fcat-fieldname = 'ERZET'.
  ls_fcat-ref_field = 'ERZET'.
  ls_fcat-ref_table = 'VBAK'.
  APPEND ls_fcat TO lt_fcat.
  CLEAR ls_fcat.
ENDFORM.                    " CREATE_FIELD_CATALOG
*&---------------------------------------------------------------------*
*&      Form CREATE_DYNAMIC_TABLE
*&---------------------------------------------------------------------*
FORM create_dynamic_table .
** Create dynamic table
  CALL METHOD cl_alv_table_create=>create_dynamic_table
    EXPORTING
      it_fieldcatalog           = lt_fcat
    IMPORTING
      ep_table                  = lref_ditab
    EXCEPTIONS
      generate_subpool_dir_full = 1
      OTHERS                    = 2.
  IF sy-subrc <> 0.
    MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
               WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
  ENDIF.
** Assign the dynamic table reference to a field-symbol
  ASSIGN lref_ditab->* TO <fs_dyn_tab1>.
**Create another dynamic table with the same structure
  newstr2 ?= cl_abap_structdescr=>describe_by_data_ref( lref_ditab ).
  tab_type1 ?= newstr2.
  CREATE DATA dtab TYPE HANDLE tab_type1.
  ASSIGN dtab->* TO <fs_dyn_tab2>.
** Create a structure similar to the dynamic table created
  CREATE DATA lref_new_line LIKE LINE OF <fs_dyn_tab1>.
  ASSIGN lref_new_line->* TO <fs_dyn_wa>.
ENDFORM.                               " CREATE_DYNAMIC_TABLE
*&---------------------------------------------------------------------*
*&      Form  FILL_DYNAMIC_TABLE
*&---------------------------------------------------------------------*
FORM fill_dynamic_table.
** Fill the dynamic table <fs_dyn_tab1>
  SELECT vbeln auart ernam erdat erzet
    FROM vbak  INTO TABLE <fs_dyn_tab1>
   WHERE auart = 'ROR'
     AND erdat LT sy-datum.
** Fill the dynamic table <fs_dyn_tab2>
  SELECT vbeln auart ernam erdat erzet
    FROM vbak INTO TABLE <fs_dyn_tab2>
   WHERE auart = 'ZSOR'
     AND erdat LT sy-datum.
ENDFORM.                               " FILL_DYNAMIC_TABLE
*&---------------------------------------------------------------------*
*&      Form  CALL_SCREEN
*&---------------------------------------------------------------------*
FORM call_screen .
  CALL SCREEN 100.
ENDFORM.                               " CALL_SCREEN
*&---------------------------------------------------------------------*
*&      Module  STATUS_0100  OUTPUT
*&---------------------------------------------------------------------*
*       PBO Module- Display both the tables in alv
*---------------------------------------------------------------------*
MODULE status_0100 OUTPUT.
  SET PF-STATUS 'MENUBAR'.
  SET TITLEBAR 'ALV REPORT'.
*&---------------------------------------------------------------------*
*       ALV Display-1st table
*----------------------------------------------------------------------*
* Object for container
  CREATE OBJECT obj_r_container
    EXPORTING
      container_name              = 'CUSTOM1'
    EXCEPTIONS
      cntl_error                  = 1
      cntl_system_error           = 2
      create_error                = 3
      lifetime_error              = 4
      lifetime_dynpro_dynpro_link = 5
      OTHERS                      = 6.
  IF sy-subrc <> 0.
    MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
               WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
  ENDIF.
* Object for Alv grid
  CREATE OBJECT obj_my_alv_grid
    EXPORTING
      i_parent          = obj_r_container
    EXCEPTIONS
      error_cntl_create = 1
      error_cntl_init   = 2
      error_cntl_link   = 3
      error_dp_create   = 4
      OTHERS            = 5.
  IF sy-subrc <> 0.
    MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
               WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
  ENDIF.
* Calling method for displaying the data
  CALL METHOD obj_my_alv_grid->set_table_for_first_display
    CHANGING
      it_outtab                     = <fs_dyn_tab1>
      it_fieldcatalog               = lt_fcat
    EXCEPTIONS
      invalid_parameter_combination = 1
      program_error                 = 2
      too_many_lines                = 3
      OTHERS                        = 4.
  IF sy-subrc <> 0.
    MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
               WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
  ENDIF.
*&---------------------------------------------------------------------*
*       ALV Display-2nd table
*----------------------------------------------------------------------*
* Object for conatainer
  CREATE OBJECT obj_r_container
    EXPORTING
      container_name              = 'CUSTOM2'
    EXCEPTIONS
      cntl_error                  = 1
      cntl_system_error           = 2
      create_error                = 3
      lifetime_error              = 4
      lifetime_dynpro_dynpro_link = 5
      OTHERS                      = 6.
  IF sy-subrc <> 0.
    MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
               WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
  ENDIF.
* Object for Alv grid
  CREATE OBJECT obj_my_alv_grid
    EXPORTING
      i_parent          = obj_r_container
    EXCEPTIONS
      error_cntl_create = 1
      error_cntl_init   = 2
      error_cntl_link   = 3
      error_dp_create   = 4
      OTHERS            = 5.
  IF sy-subrc <> 0.
    MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
               WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
  ENDIF.
* Calling method for displaying the data
  CALL METHOD obj_my_alv_grid->set_table_for_first_display
    CHANGING
      it_outtab                     = <fs_dyn_tab2>
      it_fieldcatalog               = lt_fcat
    EXCEPTIONS
      invalid_parameter_combination = 1
      program_error                 = 2
      too_many_lines                = 3
      OTHERS                        = 4.
  IF sy-subrc <> 0.
    MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
               WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
  ENDIF.
ENDMODULE.                             " STATUS_0100  OUTPUT
*&---------------------------------------------------------------------*
*&      Module  USER_COMMAND_0100  INPUT
*&---------------------------------------------------------------------*
*       PAI Module
*----------------------------------------------------------------------*
MODULE user_command_0100 INPUT.
  CASE sy-ucomm.
    WHEN 'BACK' OR 'EXIT'.
      LEAVE TO  SCREEN 0.
    WHEN 'CANCEL'.
      LEAVE PROGRAM.
  ENDCASE.                             " CASE SY-UCOMM
ENDMODULE.                             " USER_COMMAND_0100  INPUT

Please send us your feedback/suggestions at webmaster@SAPTechnical.COM 

HomeContribute About Us Privacy Terms Of Use • Disclaimer • SafeCompanies: Advertise on SAPTechnical.COM | Post JobContact Us  

Graphic Design by Round the Bend Wizards

footer image footer image