Tuesday, 1 August 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

Create a Simple SAP ALV Report

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

*&---------------------------------------------------------------------*
*& Data Declaration
*&---------------------------------------------------------------------*
DATA: it_sbook     TYPE TABLE OF sbook.
DATA: it_fieldcat  TYPE slis_t_fieldcat_alv,
      wa_fieldcat  TYPE slis_fieldcat_alv.
*&---------------------------------------------------------------------*
*& START-OF-SELECTION
*&---------------------------------------------------------------------*
START-OF-SELECTION.

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

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

  wa_fieldcat-fieldname  = 'CONNID'.
  wa_fieldcat-seltext_m  = 'Con. No.'.
  APPEND wa_fieldcat TO it_fieldcat.

  wa_fieldcat-fieldname  = 'FLDATE'.
  wa_fieldcat-seltext_m  = 'Date'.
  APPEND wa_fieldcat TO it_fieldcat.

  wa_fieldcat-fieldname  = 'BOOKID'.
  wa_fieldcat-seltext_m  = 'Book. ID'.
  APPEND wa_fieldcat TO it_fieldcat.

  wa_fieldcat-fieldname  = 'PASSNAME'.
  wa_fieldcat-seltext_m  = 'Passenger Name'.
  APPEND wa_fieldcat TO it_fieldcat.

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

ALV REPORT

ALV REPORT

What is SAP ALV?


ALV stands for ABAP List Viewer. ALV gives us a standard List format and user interface to all our ABAP reports. ALV is created by a set of standard function modules provided by SAP.
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
Because of the above functions, ALV substantially decreases the report development time. ALV takes care of rendering the list and we can concentrate only on the data retrieval part.
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


Advantages of ALV Reports
  • Better look and feel.
  • ALV report consists of some per-defined options like sort, filters, sum, downloading, print, changing the layout structure and many more.
List of Function Modules used to develop ALV reports
The below are the list of Function Modules we use to display ALV reports in different formats.
REUSE_ALV_GRID_DISPLAY. "Display ALV grid format
REUSE_ALV_LIST_DISPLAY. "Display ALV List format 
REUSE_ALV_COMMENTARY_WRITE. "Display Top of page, logo, etc.
REUSE_ALV_FIELDCATELOGUE_MERGE. "Used to generate field catalogue
REUSE_ALV_EVENTS_GET. "Use events in ALV
REUSE_ALV_HEIRARCHY_LIST_DISPLAY. "Display ALV Hierarchy 
REUSE_ALV_BLOCKED_LIST_DISPLAY. "Display blocked list
Simple ALV report.
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.