TREE list report using SAP ABAP

How to develop A TREE LIST interactive report Using SAP ABAP with Function modules RS_TREE_CONSTRUCT and RS_TREE_LIST_DISPLAY

+ -

As we already know SAP supports multiple types of drill down reports like ABAP Interactive reports, ALV Interactive etc., in this lesson we are going to develop a drill down report with TREE structure.

Requirement: Display list of materials for a give input(Material number ranges) with descriptions in different languages in the form of a tree.

To create a TREE LIST, we use RS_TREE_CONSTRUCT and RS_TREE_LIST_DISPLAY Function Modules

RS_TREE_CONSTRUCT is used to construct a TREE node.

RS_TREE_LIST_DISPLAY is used to display/print constructed TREE.

Final output of the TREE like looks like below.

TREE LIST report in SAP ABAP

Steps to follow to create TREE LIST

  1. Data Declarations for required tables
  2. Get Data From Required Tables
  3. Construct TREE node
  4. Display TREE

Go to SE38, create a report with name ZSAPN_TREE_MATERIAL and follow below steps.

Data Declarations
Internal tables and work area declarations for required tables, in our requirement, we are building tree for MARA(Material) and MAKT(Material descriptions multiple languages).
***Material Basic Data Declarations
TYPES: BEGIN OF TY_MARA,
        MATNR TYPE MARA-MATNR,
        MTART TYPE MARA-MTART,
        MBRSH TYPE MARA-MBRSH,
        MATKL TYPE MARA-MATKL,
        MEINS TYPE MARA-MEINS,
      END OF TY_MARA.
DATA : IT_MARA TYPE TABLE OF TY_MARA.
DATA : WA_MARA TYPE TY_MARA.
***Material Descriptions Declarations
TYPES: BEGIN OF TY_MAKT,
        MATNR TYPE MAKT-MATNR,
        SPRAS TYPE MAKT-SPRAS,
        MAKTX TYPE MAKT-MAKTX,
      END OF TY_MAKT.
DATA : IT_MAKT TYPE TABLE OF TY_MAKT.
DATA : WA_MAKT TYPE TY_MAKT.
Get Data from required tables MARA and MAKT
Get data from tables MARA and MAKT under START-OF-SELECTION event using FOR ALL ENTRIES.
**Get Data From Tables
  SELECT MATNR MTART MBRSH MATKL MEINS FROM MARA INTO TABLE IT_MARA UP TO 50 ROWS WHERE MATNR IN S_MATNR.
  SELECT MATNR SPRAS MAKTX FROM MAKT INTO TABLE IT_MAKT FOR ALL ENTRIES IN IT_MARA WHERE MATNR = IT_MARA-MATNR.
Construct TREE node suing RS_TREE_CONSTRUCT

Function module RS_TREE_CONSTRUCT has a table parameter with name NODETAB, this parameter holds the TLEVEL (Level of a row ex:1, 2 etc), NAME,TEXT,TEXT1,TEXT2 - TEXT9(field name ex: MATNR), NLENGTH,TLENGTH,TLENGTH1 - TLENGTH9(Length of the field ex: 18), COLOR, TCOLOR, TCOLOR1 to TCOLOR9 etc.

Note: By using RS_TREE_LIST_DISPLAY and RS_TREE_CONSTRUCT function modules, we can display maximum of 10 columns in each level.

***Data Table deceleration for FM RS_TREE_CONSTRUCT
DATA:  IT_NODE TYPE STANDARD TABLE OF SNODETEXT,
       WA_NODE TYPE SNODETEXT.

**Declare Constants for TREE
CONSTANTS:
      C_COL_KEY   TYPE C LENGTH 1 VALUE COL_KEY,
      C_COL_FIELD  TYPE C LENGTH 1 VALUE COL_NORMAL,
      C_COL_MATNR TYPE C LENGTH 1 VALUE COL_KEY,
      C_COL_MAKTX TYPE C LENGTH 1 VALUE COL_POSITIVE.

Create Root level

  "Create root node at level 1
  WA_NODE-TLEVEL = 1.
  WA_NODE-NAME   = 'Materials'.
  WA_NODE-NLENGTH = 20.
  WA_NODE-COLOR = C_COL_KEY.
  WA_NODE-TEXT   = 'Material Master Report'.
  WA_NODE-TLENGTH = 50.
  APPEND WA_NODE TO IT_NODE.

Loop through IT_MARA and IT_MAKT(inside IT_MARA loop) and build level 2 and level 3

LOOP AT IT_MARA INTO WA_MARA.
    CLEAR WA_NODE.
    WA_NODE-TLEVEL = 2. "Node Level 2
    "Material Number
    WA_NODE-NAME   = WA_MARA-MATNR.
    WA_NODE-NLENGTH = 18.
    WA_NODE-COLOR = C_COL_MATNR.

    "Material Type
    WA_NODE-TEXT1   = WA_MARA-MTART.
    WA_NODE-TLENGTH1 = 4.
    WA_NODE-TCOLOR1 = C_COL_FIELD.

    "Industry Sector
    WA_NODE-TEXT2   = WA_MARA-MBRSH.
    WA_NODE-TLENGTH2 = 1.
    WA_NODE-TCOLOR2 = C_COL_FIELD.

    "Material Group
    WA_NODE-TEXT3   = WA_MARA-MATKL.
    WA_NODE-TLENGTH3 = 4.
    WA_NODE-TCOLOR3 = C_COL_FIELD.
    "Unit of Measure
    WA_NODE-TEXT4   = WA_MARA-MEINS.
    WA_NODE-TLENGTH4 = 3.
    WA_NODE-TCOLOR4 = C_COL_FIELD.
    APPEND WA_NODE TO IT_NODE.
    LOOP AT IT_MAKT INTO WA_MAKT WHERE MATNR = WA_MARA-MATNR.
      CLEAR WA_NODE.
      WA_NODE-TLEVEL = 3. "Node level 3
      "Material Number
      WA_NODE-NAME   = WA_MAKT-MATNR.
      WA_NODE-NLENGTH = 18.
      WA_NODE-COLOR = C_COL_MAKTX.
      "Language ISO Code
      WA_NODE-TEXT   = WA_MAKT-SPRAS.
      WA_NODE-TLENGTH = 2.
      WA_NODE-TCOLOR = C_COL_FIELD.
      "Material Description
      WA_NODE-TEXT1   = WA_MAKT-MAKTX.
      WA_NODE-TLENGTH1 = 40.
      WA_NODE-TCOLOR1 = C_COL_FIELD.
      APPEND WA_NODE TO IT_NODE.
    ENDLOOP.
  ENDLOOP.

Call Function module RS_TREE_CONSTRUCT

**Construct Tree
  CALL FUNCTION 'RS_TREE_CONSTRUCT'
*   EXPORTING
*     INSERT_ID                = '000000'
*     RELATIONSHIP             = ' '
*     LOG                      =
    TABLES
      NODETAB            = IT_NODE
    EXCEPTIONS
      TREE_FAILURE       = 1
      ID_NOT_FOUND       = 2
      WRONG_RELATIONSHIP = 3
      OTHERS             = 4.
  IF SY-SUBRC <> 0.
    WRITE 'Error in Tree Construction'.
  ENDIF.
Display TREE

Finally Display TREE by calling Function module RS_TREE_LIST_DISPLAY

***Display TREE
  CALL FUNCTION 'RS_TREE_LIST_DISPLAY'
    EXPORTING
      CALLBACK_PROGRAM = SY-REPID.
Testing the above report
  • Go to SE11 -> MAKT
  • Get material numbers which have more the one description (different languages)
  • Execute the program and provide the material numbers as inputs (which we got from MAKT)
  • Execute, expand TREE and observe
Final and Full program source code
*&---------------------------------------------------------------------*
*& Report  ZSAPN_TREE_MATERIAL
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*

REPORT ZSAPN_TREE_MATERIAL.
***Material Basic Data Declarations
TYPES: BEGIN OF TY_MARA,
        MATNR TYPE MARA-MATNR,
        MTART TYPE MARA-MTART,
        MBRSH TYPE MARA-MBRSH,
        MATKL TYPE MARA-MATKL,
        MEINS TYPE MARA-MEINS,
      END OF TY_MARA.
DATA : IT_MARA TYPE TABLE OF TY_MARA.
DATA : WA_MARA TYPE TY_MARA.
***Material Descriptions Declarations
TYPES: BEGIN OF TY_MAKT,
        MATNR TYPE MAKT-MATNR,
        SPRAS TYPE MAKT-SPRAS,
        MAKTX TYPE MAKT-MAKTX,
      END OF TY_MAKT.
DATA : IT_MAKT TYPE TABLE OF TY_MAKT.
DATA : WA_MAKT TYPE TY_MAKT.
***Data Table decleration for FM RS_TREE_CONSTRUCT
DATA:  IT_NODE TYPE STANDARD TABLE OF SNODETEXT,
       WA_NODE TYPE SNODETEXT.
**Declare Constants for TREE
CONSTANTS:
      C_COL_KEY   TYPE C LENGTH 1 VALUE COL_KEY,
      C_COL_FIELD  TYPE C LENGTH 1 VALUE COL_NORMAL,
      C_COL_MATNR TYPE C LENGTH 1 VALUE COL_KEY,
      C_COL_MAKTX TYPE C LENGTH 1 VALUE COL_POSITIVE.
**Selection Screen
SELECT-OPTIONS : S_MATNR FOR WA_MARA-MATNR.

START-OF-SELECTION.
**Get Data From Tables
  SELECT MATNR MTART MBRSH MATKL MEINS FROM MARA INTO TABLE IT_MARA UP TO 50 ROWS WHERE MATNR IN S_MATNR.
  SELECT MATNR SPRAS MAKTX FROM MAKT INTO TABLE IT_MAKT FOR ALL ENTRIES IN IT_MARA WHERE MATNR = IT_MARA-MATNR.
  "Create root node at level 1
  WA_NODE-TLEVEL = 1.
  WA_NODE-NAME   = 'Materials'.
  WA_NODE-NLENGTH = 20.
  WA_NODE-COLOR = C_COL_KEY.
  WA_NODE-TEXT   = 'Material Master Report'.
  WA_NODE-TLENGTH = 50.
  APPEND WA_NODE TO IT_NODE.
  LOOP AT IT_MARA INTO WA_MARA.
    CLEAR WA_NODE.
    WA_NODE-TLEVEL = 2. "Node Level 2
    "Material Number
    WA_NODE-NAME   = WA_MARA-MATNR.
    WA_NODE-NLENGTH = 18.
    WA_NODE-COLOR = C_COL_MATNR.

    "Material Type
    WA_NODE-TEXT1   = WA_MARA-MTART.
    WA_NODE-TLENGTH1 = 4.
    WA_NODE-TCOLOR1 = C_COL_FIELD.

    "Industry Sector
    WA_NODE-TEXT2   = WA_MARA-MBRSH.
    WA_NODE-TLENGTH2 = 1.
    WA_NODE-TCOLOR2 = C_COL_FIELD.

    "Material Group
    WA_NODE-TEXT3   = WA_MARA-MATKL.
    WA_NODE-TLENGTH3 = 4.
    WA_NODE-TCOLOR3 = C_COL_FIELD.
    "Unit of Measure
    WA_NODE-TEXT4   = WA_MARA-MEINS.
    WA_NODE-TLENGTH4 = 3.
    WA_NODE-TCOLOR4 = C_COL_FIELD.
    APPEND WA_NODE TO IT_NODE.
    LOOP AT IT_MAKT INTO WA_MAKT WHERE MATNR = WA_MARA-MATNR.
      CLEAR WA_NODE.
      WA_NODE-TLEVEL = 3. "Node level 3
      "Material Number
      WA_NODE-NAME   = WA_MAKT-MATNR.
      WA_NODE-NLENGTH = 18.
      WA_NODE-COLOR = C_COL_MAKTX.
      "Language ISO Code
      WA_NODE-TEXT   = WA_MAKT-SPRAS.
      WA_NODE-TLENGTH = 2.
      WA_NODE-TCOLOR = C_COL_FIELD.
      "Material Description
      WA_NODE-TEXT1   = WA_MAKT-MAKTX.
      WA_NODE-TLENGTH1 = 40.
      WA_NODE-TCOLOR1 = C_COL_FIELD.
      APPEND WA_NODE TO IT_NODE.
    ENDLOOP.
  ENDLOOP.
**Construct Tree
  CALL FUNCTION 'RS_TREE_CONSTRUCT'
*   EXPORTING
*     INSERT_ID                = '000000'
*     RELATIONSHIP             = ' '
*     LOG                      =
    TABLES
      NODETAB            = IT_NODE
    EXCEPTIONS
      TREE_FAILURE       = 1
      ID_NOT_FOUND       = 2
      WRONG_RELATIONSHIP = 3
      OTHERS             = 4.
  IF SY-SUBRC <> 0.
    WRITE 'Error in Tree Construction'.
  ENDIF.
***Display TREE
  CALL FUNCTION 'RS_TREE_LIST_DISPLAY'
    EXPORTING
      CALLBACK_PROGRAM = SY-REPID.
Note: By using RS_TREE_LIST_DISPLAY and RS_TREE_CONSTRUCT function modules, we can display maximum of 10 columns in each level.

Learner Questions


No Questions by learners, be first one to ask ..!!

    Please Sign in to ask a question

    Was this lesson helpful to you? Yes No 12 People out of 12 think this lesson helpful


    Lesson Navigation

    ←Previous Chapter
    Next Lesson → Menu painter in SAP