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/

Apr 2, 2016

How to Create a Zip File in ABAP

By Fareez Ahamed

We generate different kinds of files varying from Excel sheets to Text files based on the data available in SAP. It is very much possible to zip multiple reports within a single zip file and download the zip file from SAP.

In this article we are going to generate a zip file which will contain Material Number (MATNR) and Material Description (MAKTX) in two languages (English and German) in two different files.

SAP has provided the class CL_ABAP_ZIP for reading and writing zip files which we are going to utilize to generate zip file. Following is the sequence of process involved to generate a zip file.

  • Fetch data into internal tables
  • Convert them to xstring.
  • Create object for CL_ABAP_ZIP
  • Use add method to add new files to the zip archive
  • Now, save method returns the final zip file in xstring
  • Convert the xstring to internal table
  • Download it using GUI_DOWNLOAD

The result:



And the program is as follows:

*&---------------------------------------------------------------------*
*& Report  ZDI_ZIP
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*

REPORT  zdi_zip.

DATA:
      BEGIN OF file_stru,
        matnr TYPE matnr,
        maktx TYPE maktx,
      END OF file_stru,
      BEGIN OF s_matnr,
        matnr TYPE matnr,
      END OF s_matnr,
      BEGIN OF s_line,
        line TYPE LENGTH 256,
      END OF s_line,
      gt_mara_en LIKE STANDARD TABLE OF file_stru,
      gt_mara_de LIKE STANDARD TABLE OF file_stru,
      gt_matnr LIKE STANDARD TABLE OF s_matnr,
      go_zip TYPE REF TO cl_abap_zip,
      g_xstr_en TYPE xstring,
      g_xstr_de TYPE xstring,
      g_xstr_zip TYPE xstring.

START-OF-SELECTION.

  "Read and prepare data
  PERFORM fetch_data.

  "Create object for CL_ABAP_ZIP
  CREATE OBJECT go_zip.

  "Convert itabs to xstring
  PERFORM convert_to_xstring
    TABLES gt_mara_en
    USING  g_xstr_en.

  PERFORM convert_to_xstring
    TABLES gt_mara_de
    USING  g_xstr_de.

  "Add xstring as file content to zip
  go_zip->add(
    EXPORTING
      name 'en.txt'
      content g_xstr_en
  ).

  go_zip->add(
    EXPORTING
      name 'de.txt'
      content g_xstr_de
  ).

  "Get the xstring for final zip file
  g_xstr_zip go_zip->save).

  "Dowload the xstring received
  "from save method
  PERFORM download_zip_file.

  WRITE 'File Downloaded'.


*&---------------------------------------------------*
*&      Form  fetch_data
*&---------------------------------------------------*
*       text
*----------------------------------------------------*
FORM fetch_data.
  DATA:
        lt_mara_en LIKE STANDARD TABLE OF file_stru,
        lt_mara_de LIKE STANDARD TABLE OF file_stru,
        s_temp LIKE file_stru.

  "Select thousand materials
  SELECT
    matnr
  FROM mara
  INTO TABLE gt_matnr
  UP TO 1000 ROWS.

  "Select their english texts
  SELECT
    matnr
    maktx
  FROM makt
  INTO TABLE lt_mara_en
  FOR ALL ENTRIES IN gt_matnr
    WHERE matnr EQ gt_matnr-matnr
    AND spras EQ 'E'.

  "Select their german texts
  SELECT
    matnr
    maktx
  FROM makt
  INTO TABLE lt_mara_de
  FOR ALL ENTRIES IN gt_matnr
    WHERE matnr EQ gt_matnr-matnr
    AND spras EQ 'D'.

  SORT gt_matnr ASCENDING.
  SORT gt_mara_en ASCENDING.
  SORT gt_mara_de ASCENDING.

  LOOP AT gt_matnr INTO s_matnr.

    CLEAR s_temp.

    READ TABLE lt_mara_en INTO s_temp
      WITH KEY matnr s_matnr-matnr.

    IF sy-subrc EQ 0.
      APPEND s_temp TO gt_mara_en.
    ELSE.
      s_temp-matnr s_matnr-matnr.
      APPEND s_temp TO gt_mara_en.
    ENDIF.

    CLEAR s_temp.

    READ TABLE lt_mara_de INTO s_temp
      WITH KEY matnr s_matnr-matnr.

    IF sy-subrc EQ 0.
      APPEND s_temp TO gt_mara_de.
    ELSE.
      s_temp-matnr s_matnr-matnr.
      APPEND s_temp TO gt_mara_de.
    ENDIF.

  ENDLOOP.

ENDFORM.                    "fetch_data

*&----------------------------------------------*
*&      Form  convert_to_xstring
*&----------------------------------------------*
*       text
*-----------------------------------------------*
*      -->P_TAB      text
*      -->P_XSTR     text
*-----------------------------------------------*
FORM convert_to_xstring
  TABLES p_tab
  USING p_xstr.

  DATA:
        lt_lines LIKE STANDARD TABLE OF s_line.

  LOOP AT p_tab INTO file_stru.
    s_line-line file_stru.
    APPEND s_line TO lt_lines.
  ENDLOOP.

  CALL FUNCTION 'SCMS_TEXT_TO_XSTRING'
*   EXPORTING
*     FIRST_LINE       = 0
*     LAST_LINE        = 0
*     MIMETYPE         = ' '
   IMPORTING
     buffer           p_xstr
   TABLES
     text_tab         lt_lines
   EXCEPTIONS
     failed           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.


ENDFORM.                    "convert_to_xstring

*&------------------------------------------------*
*&      Form  download_zip_file
*&------------------------------------------------*
*       text
*-------------------------------------------------*
FORM download_zip_file.

  DATA:
        l_len TYPE i,
        lt_lines LIKE STANDARD TABLE OF s_line.

  CALL FUNCTION 'SCMS_XSTRING_TO_BINARY'
    EXPORTING
      buffer          g_xstr_zip
*     APPEND_TO_TABLE = ' '
    IMPORTING
      output_length   l_len
    TABLES
      binary_tab      lt_lines.

  CALL FUNCTION 'GUI_DOWNLOAD'
    EXPORTING
      filename   'D:/out.zip'
      filetype   'BIN'
    IMPORTING
      filelength l_len
    TABLES
      data_tab   lt_lines.
  IF sy-subrc <> 0.
    MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
            WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
  ENDIF.

ENDFORM.                    "download_zip_file