Apr 6, 2016

Tree List report in ABAP

by Fareez Ahamed

Reports which has the capability to drill down to multiple levels are of good use. It serves as a simple overview as well as more detailed report when you want to look into a particular record in detail. SAP makes it easy to create Tree Listreports with Function Modules RS_TREE_CONSTRUCT and  RS_TREE_LIST_DISPLAY.
Multi-level Tree List Report
Now we are going to develop a Tree List report with three levels of details. First level will be the the list of Customers, second level will be the list of Sales Order Headers for that Customer and third level will be the list of Items in a particularSales Order.

The task can be broken into three steps,
  • Load the data
  • Prepare Node Table
  • Display the Tree List
Before going into the steps, we are going to declare the variables needed for our program. We have to store the three levels of data in three different internal tables and the first thing to do is defining the types for those three structures.
types:
      "Customer Structure (Level 1)
      begin of s_kna1,
        kunnr type kna1-kunnr,
        land1 type kna1-land1,
        name1 type kna1-name1,
        ort01 type kna1-ort01,
      end of s_kna1,

      "Sales order header structure (Level 2)
      begin of s_vbak,
        vbeln type vbak-vbeln,
        erdat type vbak-erdat,
        erzet type vbak-erzet,
        netwr type vbak-netwr,
        waerk type vbak-waerk,
        kunnr type vbak-kunnr,
      end of s_vbak,

      "Sales order items structure (Level 3)
      begin of s_vbap,
        vbeln type vbap-vbeln,
        posnr type vbap-posnr,
        matnr type vbap-matnr,
        netwr type vbap-netwr,
        waerk type vbap-waerk,
      end of s_vbap.
And now declare variables with above defined types...
data:
      "Internal tables and Work Areas
      "for all three levels of data
      gt_kna1 type standard table of s_kna1,
      gs_kna1 type s_kna1,
      gt_vbak type standard table of s_vbak,
      gs_vbak type s_vbak,
      gt_vbap type standard table of s_vbap,
      gs_vbap type s_vbap,

      "Table which represents the data
      "in tree format and passed to RS_TREE_CONSTRUCT
      gt_node type standard table of snodetext,
      gs_node type snodetext. 
We have colors in our report and it is good practice to declare them as constants, so that we will be able to change them without messing up the program. The values comes from the Type Pool col.
constants:
      c_col_key   type c length 1 value col_key,
      c_col_data  type c length 1 value col_normal,
      c_col_curr  type c length 1 value col_total,
      c_col_kunnr type c length 1 value col_key,
      c_col_vbeln type c length 1 value col_positive,
      c_col_posnr type c length 1 value col_group.
As I mentioned the program is just three steps...
start-of-selection.

  perform load_all_data.

  perform prepare_node_table.

  perform display_list_tree.

Load the data

We have to load the list of Customers into gt_kna1, followed by corresponding records of Sales Order Headers and Sales Order Items into gt_vbak and gt_vbap respectively.
form load_all_data .

  "Read Customer data (Level 1)
  select
    kunnr
    land1
    name1
    ort01
    from kna1
    into table gt_kna1
    up to 50 rows.

  "Read corresponding Sales Order headers (Level 2)
  select
    vbeln
    erdat
    erzet
    netwr
    waerk
    kunnr
    from vbak
    into table gt_vbak
    for all entries in gt_kna1
    where
      kunnr = gt_kna1-kunnr.

  "Read corresponding Sales Order items (Level 3)
  select
    vbeln
    posnr
    matnr
    netwr
    waerk
    from vbap
    into table gt_vbap
    for all entries in gt_vbap
    where
      vbeln = gt_vbap-vbeln.

endform.
That's it and we have loaded the data needed.

Prepare Node Table

We are going to prepare a table of structure snodetext which represents the data we have in tree structure and it will also contain the length and color specifications of fields. Important fields in SNODETEXT are,
  • tlevel - Level of the data in the tree. (Root is considered as Level 1)
  • name - First text field of the tree
  • nlength - Length of the first field
  • color - Color of the first field
  • texttext1, ... text9 - Next fields in order left to right
  • tlengthtlength1, ... tlength9 - Lengths of the corresponding fields
  • tcolortcolor1, ... tcolor9 - Colors of the correspoding fields
So our logic will be,
Add root node tlevel = 1

loop at Customers.

    Add Customer with tlevel = 2

    loop at corresponding Sales Order Headers.

        Add Sales Order Header with tlevel = 3

        loop at corresponding Sales Order Items.

            Add Sales Order Item with tlevel = 4

        endloop.

    endloop.

endloop.
  Now the exact code is as follows.
form prepare_node_table .

  data:
        l_netwr type c length 15.

  "Create a root node
  gs_node-tlevel = 1.
*  gs_node-type   = 'H'.
  gs_node-name   = 'Customers'.
  gs_node-nlength = 20.
  gs_node-color = c_col_key.
  gs_node-text   = 'Customer Sales Report'.
  gs_node-tlength = 50.
  append gs_node to gt_node.


  loop at gt_kna1 into gs_kna1.

    clear gs_node.

    gs_node-tlevel = 2.

    "Customer Number
    gs_node-name   = gs_kna1-kunnr.
    gs_node-nlength = 20.
    gs_node-color = c_col_kunnr.

    "Country code
    gs_node-text   = gs_kna1-land1.
    gs_node-tlength = 5.
    gs_node-tcolor = c_col_data.

    "Customer Name
    gs_node-text1   = gs_kna1-name1.
    gs_node-tlength1 = 40.
    gs_node-tcolor1 = c_col_data.

    "City
    gs_node-text2   = gs_kna1-ort01.
    gs_node-tlength2 = 25.
    gs_node-tcolor2 = c_col_data.

    append gs_node to gt_node.

    loop at gt_vbak into gs_vbak
      where kunnr = gs_kna1-kunnr.

      clear gs_node.

      gs_node-tlevel = 3.

      "Sales Document Number
      gs_node-name   = gs_vbak-vbeln.
      gs_node-nlength = 15.
      gs_node-color = c_col_vbeln.

      "Created date
      gs_node-text   = gs_vbak-erdat.
      gs_node-tlength = 12.
      gs_node-tcolor = c_col_data.

      "Created Time
      gs_node-text1   = gs_vbak-erzet.
      gs_node-tlength1 = 10.
      gs_node-tcolor1 = c_col_data.

      "Net worth
      l_netwr = gs_vbak-netwr.
      gs_node-text2   = l_netwr.
      gs_node-tlength2 = 15.
      gs_node-tcolor2 = c_col_curr.

      "Currency
      gs_node-text3   = gs_vbak-waerk.
      gs_node-tlength3 = 5.
      gs_node-tcolor3 = c_col_curr.

      append gs_node to gt_node.

      loop at gt_vbap into gs_vbap
        where vbeln = gs_vbak-vbeln.

        clear gs_node.

        gs_node-tlevel = 4.

        "Item Number
        gs_node-name   = gs_vbap-posnr.
        gs_node-nlength = 15.
        gs_node-color = c_col_posnr.

        "Material Number
        gs_node-text   = gs_vbap-matnr.
        gs_node-tlength = 12.
        gs_node-tcolor = c_col_data.

        "Net worth
        l_netwr = gs_vbap-netwr.
        gs_node-text1   = l_netwr.
        gs_node-tlength1 = 15.
        gs_node-tcolor1 = c_col_curr.

        "Currency
        gs_node-text2   = gs_vbap-waerk.
        gs_node-tlength2 = 5.
        gs_node-tcolor2 = c_col_curr.

        append gs_node to gt_node.

      endloop.

    endloop.

  endloop.

endform.

Display Tree List

Now to display the Tree List, you just have to call the Function Modules RS_TREE_CONSTRUCT and RS_TREE_LIST_DISPLAY sequentially.  RS_TREE_CONSTRUCT takes gt_node as input and processes it internally and fills in the necessary fields of that internal table while RS_TREE_LIST_DISPLAY is for displaying the Tree List in the screen.
form display_list_tree .

  call function 'RS_TREE_CONSTRUCT'
*   EXPORTING
*     INSERT_ID                = '000000'
*     RELATIONSHIP             = ' '
*     LOG                      =
    tables
      nodetab                  = gt_node
   exceptions
     tree_failure             = 1
     id_not_found             = 2
     wrong_relationship       = 3
     others                   = 4
            .
  if sy-subrc <> 0.
    write sy-subrc.
  endif.

  call function 'RS_TREE_LIST_DISPLAY'
   exporting
     callback_program                = sy-repid.

endform.                    
That's it :) The entire program is below.
report  zfar_list_tree.

type-pools:
  col. "For color constants

types:
      "Customer Structure (Level 1)
      begin of s_kna1,
        kunnr type kna1-kunnr,
        land1 type kna1-land1,
        name1 type kna1-name1,
        ort01 type kna1-ort01,
      end of s_kna1,

      "Sales order header structure (Level 2)
      begin of s_vbak,
        vbeln type vbak-vbeln,
        erdat type vbak-erdat,
        erzet type vbak-erzet,
        netwr type vbak-netwr,
        waerk type vbak-waerk,
        kunnr type vbak-kunnr,
      end of s_vbak,

      "Sales order items structure (Level 3)
      begin of s_vbap,
        vbeln type vbap-vbeln,
        posnr type vbap-posnr,
        matnr type vbap-matnr,
        netwr type vbap-netwr,
        waerk type vbap-waerk,
      end of s_vbap.

data:
      "Internal tables and Work Areas
      "for all three levels of data
      gt_kna1 type standard table of s_kna1,
      gs_kna1 type s_kna1,
      gt_vbak type standard table of s_vbak,
      gs_vbak type s_vbak,
      gt_vbap type standard table of s_vbap,
      gs_vbap type s_vbap,

      "Table which represents the data
      "in tree format and passed to RS_TREE_CONSTRUCT
      gt_node type standard table of snodetext,
      gs_node type snodetext.

constants:
      c_col_key   type c length 1 value col_key,
      c_col_data  type c length 1 value col_normal,
      c_col_curr  type c length 1 value col_total,
      c_col_kunnr type c length 1 value col_key,
      c_col_vbeln type c length 1 value col_positive,
      c_col_posnr type c length 1 value col_group.

start-of-selection.

  perform load_all_data.

  perform prepare_node_table.

  perform display_list_tree.

form load_all_data .

  "Read Customer data (Level 1)
  select
    kunnr
    land1
    name1
    ort01
    from kna1
    into table gt_kna1
    up to 50 rows.

  "Read corresponding Sales Order headers (Level 2)
  select
    vbeln
    erdat
    erzet
    netwr
    waerk
    kunnr
    from vbak
    into table gt_vbak
    for all entries in gt_kna1
    where
      kunnr = gt_kna1-kunnr.

  "Read corresponding Sales Order items (Level 3)
  select
    vbeln
    posnr
    matnr
    netwr
    waerk
    from vbap
    into table gt_vbap
    for all entries in gt_vbap
    where
      vbeln = gt_vbap-vbeln.


endform.                    " LOAD_ALL_DATA

form prepare_node_table .

  data:
        l_netwr type c length 15.

  "Create a root node
  gs_node-tlevel = 1.
*  gs_node-type   = 'H'.
  gs_node-name   = 'Customers'.
  gs_node-nlength = 20.
  gs_node-color = c_col_key.
  gs_node-text   = 'Customer Sales Report'.
  gs_node-tlength = 50.
  append gs_node to gt_node.


  loop at gt_kna1 into gs_kna1.

    clear gs_node.

    gs_node-tlevel = 2.

    "Customer Number
    gs_node-name   = gs_kna1-kunnr.
    gs_node-nlength = 20.
    gs_node-color = c_col_kunnr.

    "Country code
    gs_node-text   = gs_kna1-land1.
    gs_node-tlength = 5.
    gs_node-tcolor = c_col_data.

    "Customer Name
    gs_node-text1   = gs_kna1-name1.
    gs_node-tlength1 = 40.
    gs_node-tcolor1 = c_col_data.

    "City
    gs_node-text2   = gs_kna1-ort01.
    gs_node-tlength2 = 25.
    gs_node-tcolor2 = c_col_data.

    append gs_node to gt_node.

    loop at gt_vbak into gs_vbak
      where kunnr = gs_kna1-kunnr.

      clear gs_node.

      gs_node-tlevel = 3.

      "Sales Document Number
      gs_node-name   = gs_vbak-vbeln.
      gs_node-nlength = 15.
      gs_node-color = c_col_vbeln.

      "Created date
      gs_node-text   = gs_vbak-erdat.
      gs_node-tlength = 12.
      gs_node-tcolor = c_col_data.

      "Created Time
      gs_node-text1   = gs_vbak-erzet.
      gs_node-tlength1 = 10.
      gs_node-tcolor1 = c_col_data.

      "Net worth
      l_netwr = gs_vbak-netwr.
      gs_node-text2   = l_netwr.
      gs_node-tlength2 = 15.
      gs_node-tcolor2 = c_col_curr.

      "Currency
      gs_node-text3   = gs_vbak-waerk.
      gs_node-tlength3 = 5.
      gs_node-tcolor3 = c_col_curr.

      append gs_node to gt_node.

      loop at gt_vbap into gs_vbap
        where vbeln = gs_vbak-vbeln.

        clear gs_node.

        gs_node-tlevel = 4.

        "Item Number
        gs_node-name   = gs_vbap-posnr.
        gs_node-nlength = 15.
        gs_node-color = c_col_posnr.

        "Material Number
        gs_node-text   = gs_vbap-matnr.
        gs_node-tlength = 12.
        gs_node-tcolor = c_col_data.

        "Net worth
        l_netwr = gs_vbap-netwr.
        gs_node-text1   = l_netwr.
        gs_node-tlength1 = 15.
        gs_node-tcolor1 = c_col_curr.

        "Currency
        gs_node-text2   = gs_vbap-waerk.
        gs_node-tlength2 = 5.
        gs_node-tcolor2 = c_col_curr.

        append gs_node to gt_node.

      endloop.

    endloop.

  endloop.

endform.                    " PREPARE_NODE_TABLE

form display_list_tree .

  call function 'RS_TREE_CONSTRUCT'
*   EXPORTING
*     INSERT_ID                = '000000'
*     RELATIONSHIP             = ' '
*     LOG                      =
    tables
      nodetab                  = gt_node
   exceptions
     tree_failure             = 1
     id_not_found             = 2
     wrong_relationship       = 3
     others                   = 4
            .
  if sy-subrc <> 0.
    write sy-subrc.
  endif.

  call function 'RS_TREE_LIST_DISPLAY'
   exporting
     callback_program                = sy-repid.

endform.                    " DISPLAY_LIST_TREE
From http://sap.fareez.info/blog/abap/how-to-create-tree-list-report-in-abap/

No comments :