0% found this document useful (0 votes)
88 views

Creating Dynamic Internal Table

The document describes how to dynamically generate an internal table and display it in an ALV output based on input data. The number of columns in the output table changes depending on how many plants each material is present in. The code shows using function modules and class methods to build the dynamic table structure at runtime based on the plant values for each material. It then populates the table and displays it in an ALV grid for output.

Uploaded by

Eldior Solutions
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
88 views

Creating Dynamic Internal Table

The document describes how to dynamically generate an internal table and display it in an ALV output based on input data. The number of columns in the output table changes depending on how many plants each material is present in. The code shows using function modules and class methods to build the dynamic table structure at runtime based on the plant values for each material. It then populates the table and displays it in an ALV grid for output.

Uploaded by

Eldior Solutions
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Creating Dynamic Internal Table

By Chinmaya Das - January 14, 2016

Shares

In one of our post, we showed the usage of Dynamic Where Condition . In this
article, we would show one out of the many ways to generate Dynamic Internal
Table and display it in ALV output.

Say you have a requirement where you want to show a report to indicate one Material
is present in how many Plants. Say your input material ‘M1’ is present in two plants
say ‘P1’ and ‘P2’. So the output ALV report should show only two three columns, M1,
P1 and P2. Say you have another material ‘M2’ which is present in 5 plants ‘P1’, ‘P2’,
‘P3’, ‘P4’ and ‘P5’. Then the output table would have six columns. M1, P1, P2, P3, P4
and P5.

Make a note, the number of columns are changing based on the input. Let us see a
real example.

Check the MARC table.

Above screenshot shows that Material ‘100522’ is present in two plants. Our ALV
would show as below.

Check the MARC screenshot, Material ‘100567’ is present in four plants. Our ALV
would show as below.
Did you notice, the number of columns are changing dynamically as per the
data? This can be done in many ways, but the most convenient way is to create
dynamic structure/internal table and display it.

[adToAppearHere]

Function Module ‘DDIF_FIELDINFO_GET‘ would help us to fetch table field


information. Check the code below, we are replacing ‘MARC-WERKS’ field label, with
the actual Plant number. This was just a specific project requirement. You might have
some other requirement.

Material is a fixed column but Plants are dynamic based on data from MARC. Check we
are looping through the internal table and using FM ‘DDIF_FIELDINFO_GET‘ to
determine and populate field catalog information.

Once we have the field catalog (i_fcat), we need to build the dynamic internal table
with the required columns determined by i_fcat. For this example, we are using
method ‘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.

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

The below two steps are the most important ones. Here we are assigning the dynamic
structure to the <field-symbol> table and <field-symbol> work area which would be
used later to display the ALV.

* Assign the structure of dynamic table to field symbol


AASSSSIIGGNN i_dynamic_table->* TTOO <i_dyn_table>.

* Create the dynamic work area


CCRREEAATTEE DDAATTAA wa_dyn_line LLIIKKEE LLIINNEE OOFF <i_dyn_table>.
AASSSSIIGGNN wa_dyn_line->* TTOO <wa_dyn>.

Also Read: ‘SAP HANA from space level’.

There is some mathematic done to mark ‘X’ for the column where the material is
present. So do not be confused. In debugging, it shows that although MATNR has 18
characters, the dynamic table show 36 (just the double). Same with Plant, it shows 8
instead of 4. So, this is something you want to check before you put your data.

Once you have populated the data in the dynamic internal table, displaying data in
ALV is a cake walk. Just use the FM ‘REUSE_ALV_GRID_DISPLAY‘.

Check the code behaviour in Debugging mode”

Check I_FCAT table, it has 4 rows of fields. This means, the dynamic internal table
would have 4 columns. One fixed for Material and 3 dynamic for plants.

This is the class and method which actually creates the dynamic internal table.

Check I_DYNAMIC_TABLE, it has now 3 columns for plants. This is the structure you
need. Now just create the internal table and work area and populate your final data
and display it.

Let us see, how the output would look like when we input 3 materials which are
available in multiple plants.

Check, the number of plant columns are the union of all the three materials. Isn’t it
dynamic?

Please find the working program for the above requirement here.

RREEPPOORRTT zsapyard.
*----------------------------------------------------------------------*
* Created by : SAPYard (https://sapyard.com/) *
* Purpose : Program to show Dynamic Internal table *
* Visit https://sapyard.com/ for SAP Technical Tips & Solutions *
*----------------------------------------------------------------------*
*---------------------------------------------------------------------*
* POOLS *
*---------------------------------------------------------------------*
TTYYPPEE-POOLS: slis.
*---------------------------------------------------------------------*
* TABLES *
*---------------------------------------------------------------------*
TTAABBLLEESS: mara.
*---------------------------------------------------------------------*
* TYPES
*---------------------------------------------------------------------*
TTYYPPEESS: BEGIN OOFF x_data,
matnr TTYYPPEE matnr,
werks TTYYPPEE werks_d,
END OOFF x_data.
*---------------------------------------------------------------------*
* DATA *
*---------------------------------------------------------------------*
DDAATTAA:
* Internal tables
i_data TTYYPPEE STANDARD TTAABBLLEE OOFF x_data,
i_data_temp TTYYPPEE STANDARD TTAABBLLEE OOFF x_data,
i_fcat TTYYPPEE lvc_t_fcat,
i_dynamic_table TTYYPPEE REF TTOO ddaattaa,
i_plant TTYYPPEE STANDARD TTAABBLLEE OOFF x_plant,
i_fieldcat TTYYPPEE slis_t_fieldcat_alv,

* Work ara
wa_fcat TTYYPPEE lvc_s_fcat,
wa_dyn_line TTYYPPEE REF TTOO ddaattaa,
wa_plant TTYYPPEE x_plant,
wa_data TTYYPPEE x_data,

* Variable
v_field_name TTYYPPEE fieldname,
v_tabix TTYYPPEE sytabix,
v_fieldname TTYYPPEE fieldname,
v_seltext TTYYPPEE scrtext_l.
*---------------------------------------------------------------------*
* Field Symbols *
*---------------------------------------------------------------------*
FFIIEELLDD-SYMBOLS:
<i_dyn_table> TTYYPPEE STANDARD TTAABBLLEE,
<i_final_table> TTYYPPEE STANDARD TTAABBLLEE,
<wa_dyn> TTYYPPEE aannyy,
<wa_final> TTYYPPEE aannyy.
*---------------------------------------------------------------------*
* SELECTION SCREEN *
*---------------------------------------------------------------------*
SSEELLEECCTT--OOPPTTIIOONNSS: s_matnr FFOORR mara-matnr.
*---------------------------------------------------------------------*
* INITIALIZATION *
*---------------------------------------------------------------------*
IINNIITTIIAALLIIZZAATTIIOONN.
* Select data
*---------------------------------------------------------------------*
* START-OF-SELECTION. *
*---------------------------------------------------------------------*
SSTTAARRTT--OOFF--SSEELLEECCTTIIOONN.
PPEERRFFOORRMM sub_slect_data.

*---------------------------------------------------------------------*
* END-OF-SELECTION. *
*---------------------------------------------------------------------*
EENNDD--OOFF--SSEELLEECCTTIIOONN.
* Populate the dynamic columns needed for each run
PPEERRFFOORRMM sub_populate_catlog.

* Build the dynamic internal table


PPEERRFFOORRMM sub_build_int_table.

* Build ALF Field Catalog information


PPEERRFFOORRMM sub_alv_field_cat.

* Display data
PPEERRFFOORRMM sub_display_data.

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

SSEELLEECCTT matnr werks


IINNTTOO TTAABBLLEE i_data
FFRROOMM marc
WWHHEERREE matnr IINN s_matnr.
IIFF sy-subrc EEQQ 0.
SSOORRTT i_data BBYY matnr.

i_data_temp[] = i_data[].
SSOORRTT i_data_temp BBYY werks.
DDEELLEETTEE ADJACENT DUPLICATES FFRROOMM i_data_temp
CCOOMMPPAARRIINNGG werks.
EENNDDIIFF.

EENNDDFFOORRMM.
*&---------------------------------------------------------------------*
*& Form SUB_POPULATE_CATLOG
*&---------------------------------------------------------------------*
FFOORRMM sub_populate_catlog .
v_field_name = 'MATNR'.
* There is one Material column
PPEERRFFOORRMM sub_pop_field_catlog UUSSIINNGG v_field_name.

v_field_name = 'WERKS'.
* There would be 'N' number of dynamic Plant columns
* depending on the material and plants combination
LLOOOOPP AATT i_data_temp IINNTTOO wa_data.
PPEERRFFOORRMM sub_pop_field_catlog UUSSIINNGG v_field_name.
EENNDDLLOOOOPP.
EENNDDFFOORRMM.
*&---------------------------------------------------------------------*
*& Form SUB_POP_FIELD_CATLOG
*&---------------------------------------------------------------------*
FFOORRMM sub_pop_field_catlog UUSSIINNGG p_l_field_name TTYYPPEE fieldname.

DDAATTAA: l_i_dfies TTYYPPEE STANDARD TTAABBLLEE OOFF dfies,


l_wa_dfies TTYYPPEE dfies.

* Call FM
CALL FFUUNNCCTTIIOONN 'DDIF_FIELDINFO_GET'
EEXXPPOORRTTIINNGG
tabname = 'MARC'
fieldname = p_l_field_name
TTAABBLLEESS
dfies_tab = l_i_dfies
EEXXCCEEPPTTIIOONNSS
not_found = 1
internal_error = 2
OOTTHHEERRSS = 3.
IIFF sy-subrc EEQQ 0.

CCLLEEAARR l_wa_dfies.
READ TTAABBLLEE l_i_dfies IINNTTOO l_wa_dfies IINNDDEEXX 1.
CCLLEEAARR wa_fcat.
* Since we want the Plant number to be the header text
* Replacing the field name with actual plant value
IIFF v_field_name = 'WERKS'.
l_wa_dfies-fieldname = wa_data-werks.
EENNDDIIFF.

MMOOVVEE--CCOORRRREESSPPOONNDDIINNGG l_wa_dfies TTOO wa_fcat.


AAPPPPEENNDD wa_fcat TTOO i_fcat.
EENNDDIIFF.

EENNDDFFOORRMM.
*&---------------------------------------------------------------------*
*& Form SUB_BUILD_INT_TABLE
*&---------------------------------------------------------------------*
FFOORRMM sub_build_int_table .

* Prepare the dynamic internal table with required columns of each run
CALL MMEETTHHOODD cl_alv_table_create=>create_dynamic_table
EEXXPPOORRTTIINNGG
it_fieldcatalog = i_fcat
IIMMPPOORRTTIINNGG
ep_table = i_dynamic_table
EEXXCCEEPPTTIIOONNSS
generate_subpool_dir_full = 1
OOTTHHEERRSS = 2.
* Assign the structure of dynamic table to field symbol
AASSSSIIGGNN i_dynamic_table->* TTOO <i_dyn_table>.

* Create the dynamic work area


CREATE DDAATTAA wa_dyn_line LLIIKKEE LLIINNEE OOFF <i_dyn_table>.
AASSSSIIGGNN wa_dyn_line->* TTOO <wa_dyn>.

EENNDDFFOORRMM.
*&---------------------------------------------------------------------*
*& Form SUB_ALV_FIELD_CAT
*&---------------------------------------------------------------------*
FFOORRMM sub_alv_field_cat .

* Build field catalog for Material


PPEERRFFOORRMM sub_fill_alv_field_cat UUSSIINNGG
'MATNR' '<I_DYN_TABLE>' 'L' 'Material Number' 36.

* Number of Plant columns would be dynamic for Plants


LLOOOOPP AATT i_data_temp IINNTTOO wa_data.

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

PPEERRFFOORRMM sub_fill_alv_field_cat UUSSIINNGG


v_fieldname '<I_DYN_TABLE>' 'L' v_seltext 8.

EENNDDLLOOOOPP.
EENNDDFFOORRMM.
*&---------------------------------------------------------------------*
*& Form SUB_FILL_ALV_FIELD_CAT
*&---------------------------------------------------------------------*
FFOORRMM sub_fill_alv_field_cat UUSSIINNGG
p_fldnam TTYYPPEE fieldname
p_tabnam TTYYPPEE tabname
p_justif TTYYPPEE char1
p_seltext TTYYPPEE dd03p-scrtext_l
p_outlen TTYYPPEE i.

DDAATTAA l_lfl_fcat TTYYPPEE 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.

AAPPPPEENNDD l_lfl_fcat TTOO i_fieldcat.

CCLLEEAARR l_lfl_fcat.

EENNDDFFOORRMM.

*&---------------------------------------------------------------------*
*& Form SUB_DISPLAY_DATA
*&---------------------------------------------------------------------*
FFOORRMM sub_display_data .

DDAATTAA: l_count TTYYPPEE i,


l_factor TTYYPPEE i,
l_wa_layout TTYYPPEE slis_layout_alv.

LLOOOOPP AATT i_data IINNTTOO wa_data.


CCLLEEAARR: l_factor, l_count.

READ TTAABBLLEE i_data_temp WWIITTHH KKEEYY werks = wa_data-werks


TTRRAANNSSPPOORRTTIINNGG NNOO FFIIEELLDDSS
BINARY SSEEAARRCCHH.
IIFF sy-subrc EEQQ 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'.

AATT END OOFF matnr.


AAPPPPEENNDD <wa_dyn> TTOO <i_dyn_table>.
CCLLEEAARR <wa_dyn>.
EENNDDAATT.
EENNDDIIFF.

EENNDDLLOOOOPP.

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

* Funtion module for displaying the ALV report


CALL FFUUNNCCTTIIOONN 'REUSE_ALV_GRID_DISPLAY'
EEXXPPOORRTTIINNGG
i_callback_program = sy-repid
is_layout = l_wa_layout
it_fieldcat = i_fieldcat
TTAABBLLEESS
t_outtab = <i_dyn_table>
EEXXCCEEPPTTIIOONNSS
program_error = 1
OOTTHHEERRSS = 2.

EENNDDFFOORRMM.

If you want to get such practical tweaks and tricks straight to your inbox, please
SUBSCRIBE. We respect your privacy and take protecting it seriously.

If you liked this post, please hit the share buttons. Please like us at facebook and
encourage us. If you have any suggestions, criticism, comments or questions, please
comment or reach out to us.

Thank you very much for your time!!

Image source: www.mobygames.com (modified)

Chinmaya Das
https://sapyard.com/

Qualification: M.Tech from IIT Kharagpur, India. SAP ABAP Experience since 2006. Find more
about him on LinkedIn.

More than one instance of Sumo is attempting to start on this page. Please check that you are only
loading Sumo once per page.

You might also like