Tuesday, August 1, 2017

ALV Report with Field Catalog SAP ABAP

Requirement: Develop an ALV report to display Material no (MATNR), Material type (MTART), Industry Sector (MBRSH) and Basic Unit Of measure (MEINS) for a range of material input (Select-Options).

To develop above report, we have to use field catalog (because we have to display four fields only from MARA) and we have to pass field catalog parameter to Function Module REUSE_ALV_GIRD_DISPLAY.


REPORT ZAVI_ALV_FCAT.
TABLES : MARA.
TYPE-POOLS SLIS .

TYPES : BEGIN OF TY_MARA,  "User defined internal table type
        MATNR TYPE MARA-MATNR,
        MTART TYPE MARA-MTART,
        MBRSH TYPE MARA-MBRSH,
        MEINS TYPE MARA-MEINS,
      END OF TY_MARA.

DATA : IT_MARA TYPE TABLE OF TY_MARA .
DATA : WA_MARA TYPE TY_MARA .

DATA : IT_FCAT TYPE SLIS_T_FIELDCAT_ALV .
DATA : WA_FCAT LIKE LINE OF IT_FCAT .
SELECT-OPTIONS: S_MATNR FOR MARA-MATNR.

START-OF-SELECTION .
  PERFORM GET_DATA .
  PERFORM CREATE_FCAT.

END-OF-SELECTION .
  PERFORM DISP_ALV .

*&---------------------------------------------------------------------*
*&      Form  GET_DATA
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM GET_DATA .
  SELECT MATNR MTART MBRSH MEINS FROM MARA
      INTO TABLE IT_MARA
      WHERE MATNR IN S_MATNR.
  .
ENDFORM.                    " GET_DATA
*&---------------------------------------------------------------------*
*&      Form  DISP_ALV
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM DISP_ALV .
  CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
    EXPORTING
      I_CALLBACK_PROGRAM = SY-REPID
      IT_FIELDCAT        = IT_FCAT "PASS FIELD CATALOG TO ALV
    TABLES
      T_OUTTAB           = IT_MARA.


ENDFORM.                    " DISP_ALV
*&---------------------------------------------------------------------*
*&      Form  CREATE_FCAT
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM CREATE_FCAT .
  WA_FCAT-COL_POS = '1' .
  WA_FCAT-FIELDNAME = 'MATNR' .
  WA_FCAT-TABNAME = 'IT_MARA' .
  WA_FCAT-SELTEXT_M = 'MATERIALNO' .
  WA_FCAT-KEY = 'X' .
  APPEND WA_FCAT TO IT_FCAT .
  CLEAR WA_FCAT .

  WA_FCAT-COL_POS = '2' .
  WA_FCAT-FIELDNAME = 'MTART' .
  WA_FCAT-TABNAME = 'IT_MARA' .
  WA_FCAT-SELTEXT_M = 'MATERIALTYPE' .
*  WA_FCAT-NO_OUT = 'X' .
  WA_FCAT-HOTSPOT = 'X' .
  APPEND WA_FCAT TO IT_FCAT .
  CLEAR WA_FCAT .

  WA_FCAT-COL_POS = '3' .
  WA_FCAT-FIELDNAME = 'MBRSH' .
  WA_FCAT-REF_FIELDNAME = 'MBRSH' .
  WA_FCAT-REF_TABNAME = 'MARA' .
*  WA_FCAT-TABNAME = 'IT_MARA' .
*  WA_FCAT-SELTEXT_M = 'INDSECTOR' .
*  WA_FCAT-EDIT = 'X' .
  APPEND WA_FCAT TO IT_FCAT .
  CLEAR WA_FCAT .

  WA_FCAT-COL_POS = '4' .
  WA_FCAT-FIELDNAME = 'MEINS' .
  WA_FCAT-TABNAME = 'IT_MARA' .
  WA_FCAT-SELTEXT_M = 'MAT.UNITS' .
  WA_FCAT-EMPHASIZE = 'C610'.
  APPEND WA_FCAT TO IT_FCAT .
  CLEAR WA_FCAT .

ENDFORM.                    " CREATE_FCAT

Creation of Simple SAP ALV Report

An ALV report is created using the standard function modules provided by SAP
  1. An ALV report can be created using the following steps.
  2. Include SLIS type pool – SLIS type pool contains all the data types required by ALV function modules.
  3. Data retrieval – Code the logic to fetch the data from database table into an Internal Table.
  4. Build Field Catalog – Add the columns into an internal that you want to display in the ALV output list.
  5. Pass the data table and field catalog table to ALV function module
    Sample Program - 

    TYPE-POOLS: slis.  " SLIS contains all the ALV data types

    *&---------------------------------------------------------------------*
    *& Data Declaration
    *&---------------------------------------------------------------------*
    DATA: it_sbook1     TYPE TABLE OF sbook.
    DATA: it_fieldcat1  TYPE slis_t_fieldcat_alv,
          wa_fieldcat1  TYPE slis_fieldcat_alv.
    *&---------------------------------------------------------------------*
    *& START-OF-SELECTION
    *&---------------------------------------------------------------------*
    START-OF-SELECTION.

    *Fetch data from the database
      SELECT * FROM sbook INTO TABLE it_sbook1.

    *Build field catalog
      wa_fieldcat1-fieldname  = 'CARRID'.    " Fieldname in the data table
      wa_fieldcat1-seltext_m  = 'Airline'.   " Column description in the output
      APPEND wa_fieldcat1 TO it_fieldcat1.

      wa_fieldcat1-fieldname  = 'CONNID'.
      wa_fieldcat1-seltext_m  = 'Con. No.'.
      APPEND wa_fieldcat1 TO it_fieldcat1.

      wa_fieldcat1-fieldname  = 'FLDATE'.
      wa_fieldcat1-seltext_m  = 'Date'.
      APPEND wa_fieldcat1TO it_fieldcat1.

      wa_fieldcat1-fieldname  = 'BOOKID'.
      wa_fieldcat1-seltext_m  = 'Book. ID'.
      APPEND wa_fieldcat1TO it_fieldcat1.

      wa_fieldcat1-fieldname  = 'PASSNAME'.
      wa_fieldcat1-seltext_m  = 'Passenger Name'.
      APPEND wa_fieldcat1TO it_fieldcat1.

    *Pass data and field catalog to ALV function module to display ALV list
      CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
        EXPORTING
          it_fieldcat   = it_fieldcat1
        TABLES
          t_outtab      = it_sbook1
        EXCEPTIONS
          program_error = 1
          OTHERS        = 2.



    ALV (ABAP List Viewer) Reports

     ALV (ABAP List Viewer) Reports are a type of interactive report in SAP ABAP that enable users to display and manipulate data in a tabular format. They are used to provide various functionalities such as sorting, filtering, and aggregating data to offer users a better understanding of the data.

    There are several types of ALV reports available in ABAP such as simple, hierarchical, and block lists. Simple lists show data in a table format, hierarchical lists exhibit data in a tree-like structure, while block lists present data in a spreadsheet-like format with user-defined rows and columns.

    ALV reports also offer several events that developers can utilize to customize the behavior of the report. These events include initialization, top of page, end of page, user command, and data retrieval, among others. The events allow developers to add custom functionality to the ALV report, such as adding a custom button or calculating summary values.

    ALV reports offer several benefits, including a user-friendly interface that allows for the display and manipulation of large volumes of data. They also have features like sorting and filtering, which help users analyze the data easily. Additionally, ALV reports can be customized using the available events and other options to meet specific business requirements.

    ALV provides a lot of inbuilt functions to our reports and some of the functions  are listed below.
    • Sorting of records
    • Filtering of records
    • Totals and Sub-totals
    • Download the report output to Excel/HTML
    • Changing the order of the columns in the report
    • Hide the unwanted columns  from the report
    Some of the function modules used to create ALV reports are listed below
    Function ModuleDescription
    REUSE_ALV_LIST_DISPLAYDisplay an ALV list
    REUSE_ALV_GRID_DISPLAYDisplay an ALV grid
    REUSE_ALV_COMMENTARY_WRITEOutput List header information
    REUSE_ALV_VARIANT_F4Display variant selection dialog box
    REUSE_ALV_VARIANT_EXISTENCEChecks whether a variant exists
    REUSE_ALV_FIELDCATALOG_MERGECreate field catalog from dictionary structure or internal table
    DATA: it_spfli TYPE TABLE OF spfli.
    
    SELECT * FROM spfli INTO TABLE it_spfli.
    
    CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
      EXPORTING
        i_structure_name = 'SPFLI'
      TABLES
        t_outtab         = it_spfli.

    In conclusion, ALV reports are essential interactive reports in SAP ABAP that enable users to display and manipulate data in a tabular format. There are various types of ALV reports available, and they offer events that can be used to customize their behavior. ALV reports are beneficial for displaying large volumes of data in a user-friendly manner and can be customized to meet specific business requirements.