Creating Dynamic Internal Table

Creating Dynamic Internal Table

In one of our post, we showed the use of Dynamic Where Condition . In this article, Creating Dynamic Internal Table we would show one out of the numerous ways of creating Dynamic Inward Table and show it in ALV yield.

Let’s assume you have a necessity where you need to show a report to demonstrate one Material is available in the number of Plants. Say your feedback material ‘M1’ is available in two plants say ‘P1’ and ‘P2’. So the result ALV report ought to show just two three segments, M1, P1 and P2. Let’s assume you have another material ‘M2’ which is available in 5 plants ‘P1’, ‘P2’, ‘P3’, ‘P4’ and ‘P5’. Then the result table would have six segments. M1, P1, P2, P3, P4 and P5.

Make a note, the quantity of sections are changing in view of the information. Allow us to see a genuine model. Creating Dynamic Internal Table.

Check the MARC table.

CREATE_DYNAMIC_TABLE

Above screen capture shows that Material ‘100522’ is available in two plants. Our ALV would show as underneath.

Check the MARC screen capture, Material ‘100567’ is available in four plants. Our ALV would show as beneath.

Did you see, the quantity of segments are changing powerfully according to the information? This should be possible in numerous ways, yet the most helpful way is to make dynamic design/inner table and show it.

Capability Module ‘DDIF_FIELDINFO_GET’ would assist us with getting table field data. Really take a look at the code beneath, we are supplanting ‘MARC-WERKS’ field name, with the genuine Plant number. This was only a particular undertaking prerequisite. You could have another prerequisite.

Dynamic Internal Table

Material is a decent segment yet Plants are dynamic in view of information from MARC. Check we are circling through the interior table and utilizing FM ‘DDIF_FIELDINFO_GET’ to decide and populate field index data.

When we have the field index (i_fcat), we want to construct the unique inside table with the not entirely set in stone by i_fcat. For this model, we are utilizing technique ‘CREATE_DYNAMIC_TABLE’ of class CL_ALV_TABLE_CREATE.

———————————————————–

Updated: 19th Dec 2017 – Feedback from Steve

Consider adding:
I_LENGTH_IN_BYTE = ‘X’
to the CALL METHOD cl_alv_table_create=>create_dynamic_table statement
I had a problem where I needed the dynamic table to create a p(7)/3, without that additional statement it calculates a P(4)/3.

———————————————————–

CREATE_DYNAMIC_TABLE

The under two stages are the main ones. Here we are allocating the unique construction to the <field-symbol> table and <field-symbol> workspace which would be utilized later to show the ALV.

* Assign the structure of dynamic table to field symbol
ASSIGN i_dynamic_table->* TO <i_dyn_table>.

* Create the dynamic work area
CREATE DATA wa_dyn_line LIKE LINE OF <i_dyn_table>.
ASSIGN wa_dyn_line->* TO <wa_dyn>.

There is some mathematic done to stamp ‘X’ for the segment where the material is available. So don’t be confounded. In troubleshooting, that’s what it shows despite the fact that MATNR has 18 characters, the unique table show 36 (simply the twofold). Same with Plant, it shows 8 rather than 4. Thus, this is the sort of thing you need to check before you put your information.

Whenever you have populated the information in the powerful inside table, showing information in ALV is a cake walk. Simply utilize the FM ‘REUSE_ALV_GRID_DISPLAY’.

Really look at the code conduct in Troubleshooting mode”

Dynamic Internal Table

Check I_FCAT table, it has 4 columns of fields. This implies, the unique interior table would have 4 segments. One fixed for Material and 3 dynamic for plants.

CL_ALV_TABLE_CREATE

This is the class and technique which really makes the powerful inward table.

Dynamic Internal Table

Check I_DYNAMIC_TABLE, it has now 3 segments for plants. This is the construction you want. Presently make the inward table and workspace and populate your last information and show it.

CL_ALV_TABLE_CREATE

Allow us to see, how the result would look like when we input 3 materials which are accessible in different plants.

Check, the quantity of plant segments are the association of the multitude of three materials. Isn’t it dynamic?

Kindly track down the functioning system for the above prerequisite here.

REPORT elearning. 
*----------------------------------------------------------------------*
* Created by   : eLearning (https://eLearning.com/)                    *
* Purpose      : Program to show Dynamic Internal table                *
* Visit https://eLearning.com/ for SAP Technical Tips & Solutions     *
*----------------------------------------------------------------------*
*---------------------------------------------------------------------*
* POOLS                                                              *
*---------------------------------------------------------------------*
TYPE-POOLS: slis.
*---------------------------------------------------------------------*
* TABLES                                                              *
*---------------------------------------------------------------------*
TABLES: mara.
*---------------------------------------------------------------------*
* TYPES
*---------------------------------------------------------------------*
TYPES: BEGIN OF x_data,
         matnr TYPE matnr,
         werks TYPE werks_d,
       END OF x_data.
*---------------------------------------------------------------------*
*  DATA                                                               *
*---------------------------------------------------------------------*
DATA:
* Internal tables
  i_data          TYPE STANDARD TABLE OF x_data,
  i_data_temp     TYPE STANDARD TABLE OF x_data,
  i_fcat          TYPE lvc_t_fcat,
  i_dynamic_table TYPE REF TO data,
  i_plant         TYPE STANDARD TABLE OF x_plant,
  i_fieldcat      TYPE slis_t_fieldcat_alv,

* Work ara
  wa_fcat         TYPE lvc_s_fcat,
  wa_dyn_line     TYPE REF TO data,
  wa_plant        TYPE x_plant,
  wa_data         TYPE x_data,

* Variable
  v_field_name    TYPE fieldname,
  v_tabix         TYPE sytabix,
  v_fieldname     TYPE fieldname,
  v_seltext       TYPE scrtext_l.
*---------------------------------------------------------------------*
*  Field Symbols                                                      *
*---------------------------------------------------------------------*
FIELD-SYMBOLS:
  <i_dyn_table>   TYPE STANDARD TABLE,
  <i_final_table> TYPE STANDARD TABLE,
  <wa_dyn>        TYPE any,
  <wa_final>      TYPE any.
*---------------------------------------------------------------------*
* SELECTION SCREEN                                                    *
*---------------------------------------------------------------------*
SELECT-OPTIONS: s_matnr FOR mara-matnr.
*---------------------------------------------------------------------*
* INITIALIZATION                                                      *
*---------------------------------------------------------------------*
INITIALIZATION.
* Select data
*---------------------------------------------------------------------*
* START-OF-SELECTION.                                                 *
*---------------------------------------------------------------------*
START-OF-SELECTION.
  PERFORM sub_slect_data.

*---------------------------------------------------------------------*
* END-OF-SELECTION.                                                   *
*---------------------------------------------------------------------*
END-OF-SELECTION.
* Populate the dynamic columns needed for each run
  PERFORM sub_populate_catlog.

* Build the dynamic internal table
  PERFORM sub_build_int_table.

* Build ALF Field Catalog information
  PERFORM sub_alv_field_cat.

* Display data
  PERFORM sub_display_data.

*---------------------------------------------------------------------*
* SUB ROUTINES                                                        *
*---------------------------------------------------------------------*
*&---------------------------------------------------------------------*
*&      Form  SUB_SLECT_DATA
*&---------------------------------------------------------------------*
FORM sub_slect_data .

  SELECT matnr werks
    INTO TABLE i_data
    FROM marc
    WHERE matnr IN s_matnr.
  IF sy-subrc EQ 0.
    SORT i_data BY matnr.

    i_data_temp[] = i_data[].
    SORT i_data_temp BY werks.
    DELETE ADJACENT DUPLICATES FROM i_data_temp
    COMPARING werks.
  ENDIF.

ENDFORM.
*&---------------------------------------------------------------------*
*&      Form  SUB_POPULATE_CATLOG
*&---------------------------------------------------------------------*
FORM sub_populate_catlog .
  v_field_name = 'MATNR'.
* There is one Material column
  PERFORM sub_pop_field_catlog USING v_field_name.

  v_field_name = 'WERKS'.
* There would be 'N' number of dynamic Plant columns
* depending on the material and plants combination
  LOOP AT i_data_temp INTO wa_data.
    PERFORM sub_pop_field_catlog USING v_field_name.
  ENDLOOP.
ENDFORM.
*&---------------------------------------------------------------------*
*&      Form  SUB_POP_FIELD_CATLOG
*&---------------------------------------------------------------------*
FORM sub_pop_field_catlog  USING    p_l_field_name TYPE fieldname.

  DATA: l_i_dfies  TYPE STANDARD TABLE OF dfies,
        l_wa_dfies TYPE dfies.

* Call FM
  CALL FUNCTION 'DDIF_FIELDINFO_GET'
    EXPORTING
      tabname        = 'MARC'
      fieldname      = p_l_field_name
    TABLES
      dfies_tab      = l_i_dfies
    EXCEPTIONS
      not_found      = 1
      internal_error = 2
      OTHERS         = 3.
  IF sy-subrc EQ 0.

    CLEAR l_wa_dfies.
    READ TABLE l_i_dfies INTO l_wa_dfies INDEX 1.
    CLEAR wa_fcat.
* Since we want the Plant number to be the header text
* Replacing the field name with actual plant value
    IF v_field_name = 'WERKS'.
      l_wa_dfies-fieldname = wa_data-werks.
    ENDIF.

    MOVE-CORRESPONDING l_wa_dfies TO wa_fcat.
    APPEND wa_fcat TO i_fcat.
  ENDIF.

ENDFORM.
*&---------------------------------------------------------------------*
*&      Form  SUB_BUILD_INT_TABLE
*&---------------------------------------------------------------------*
FORM sub_build_int_table .

* Prepare the dynamic internal table with required columns of each run
  CALL METHOD cl_alv_table_create=>create_dynamic_table
    EXPORTING
      it_fieldcatalog           = i_fcat
    IMPORTING
      ep_table                  = i_dynamic_table
    EXCEPTIONS
      generate_subpool_dir_full = 1
      OTHERS                    = 2.
* Assign the structure of dynamic table to field symbol
  ASSIGN i_dynamic_table->* TO <i_dyn_table>.

* Create the dynamic work area
  CREATE DATA wa_dyn_line LIKE LINE OF <i_dyn_table>.
  ASSIGN wa_dyn_line->* TO <wa_dyn>.


ENDFORM.
*&---------------------------------------------------------------------*
*&      Form  SUB_ALV_FIELD_CAT
*&---------------------------------------------------------------------*
FORM sub_alv_field_cat .

* Build field catalog for Material
  PERFORM sub_fill_alv_field_cat USING
        'MATNR' '<I_DYN_TABLE>' 'L' 'Material Number' 36.

* Number of Plant columns would be dynamic for Plants
  LOOP AT i_data_temp INTO wa_data.

    v_fieldname = wa_data-werks.
    v_seltext =   wa_data-werks.

    PERFORM sub_fill_alv_field_cat USING
              v_fieldname '<I_DYN_TABLE>' 'L' v_seltext 8.

  ENDLOOP.
ENDFORM.
*&---------------------------------------------------------------------*
*&      Form  SUB_FILL_ALV_FIELD_CAT
*&---------------------------------------------------------------------*
FORM sub_fill_alv_field_cat  USING
                             p_fldnam    TYPE fieldname
                             p_tabnam    TYPE tabname
                             p_justif    TYPE char1
                             p_seltext   TYPE dd03p-scrtext_l
                             p_outlen    TYPE i.

  DATA l_lfl_fcat TYPE slis_fieldcat_alv.

  l_lfl_fcat-fieldname  = p_fldnam.
  l_lfl_fcat-tabname    = p_tabnam.
  l_lfl_fcat-just       = p_justif.
  l_lfl_fcat-seltext_l  = p_seltext.
  l_lfl_fcat-outputlen  = p_outlen.

  APPEND l_lfl_fcat TO i_fieldcat.

  CLEAR l_lfl_fcat.

ENDFORM.

*&---------------------------------------------------------------------*
*&      Form  SUB_DISPLAY_DATA
*&---------------------------------------------------------------------*
FORM sub_display_data .

  DATA: l_count     TYPE i,
        l_factor    TYPE i,
        l_wa_layout TYPE slis_layout_alv.

  LOOP AT i_data INTO wa_data.

    CLEAR: l_factor, l_count.

    READ TABLE i_data_temp WITH KEY werks = wa_data-werks
    TRANSPORTING NO FIELDS
    BINARY SEARCH.
    IF sy-subrc EQ 0.

      <wa_dyn>+0(18) = wa_data-matnr.
      l_factor = sy-tabix - 1.
      l_count = 36 + ( 8 * l_factor ).

      <wa_dyn>+l_count(8) = 'X'.

      AT END OF matnr.
        APPEND <wa_dyn> TO <i_dyn_table>.
        CLEAR <wa_dyn>.
      ENDAT.
    ENDIF.

  ENDLOOP.

  l_wa_layout-colwidth_optimize = 'X'.
  l_wa_layout-zebra = 'X'.

* Funtion module for displaying the ALV report
  CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
    EXPORTING
      i_callback_program = sy-repid
      is_layout          = l_wa_layout
      it_fieldcat        = i_fieldcat
    TABLES
      t_outtab           = <i_dyn_table>
    EXCEPTIONS
      program_error      = 1
      OTHERS             = 2.


ENDFORM.

If you have any desire to get such commonsense changes and deceives directly to your inbox, kindly Buy in. We regard your security and view safeguarding it in a serious way.

In the event that you loved this post, kindly hit the offer buttons. If it’s not too much trouble, similar to us at facebook and support us. Assuming that you have any ideas, analysis, remarks or questions, kindly remark or connect with us.

Many thanks for your time!!

 

YOU MAY LIKE THIS

ALE and IDoc in SAP ABAP: A Deep Dive

Parallel cursor in SAP ABAP

Epic Evolution of ABAP Programming

 

 

WhatsApp WhatsApp us