Exercise ALV-Reports
Exercise ALV-Reports
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.
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.
Output
Field Catalog in SAP ALV
The field catalog is a table which contains information on the fields to be displayed on the ALV output.
First we need to build a field catalog before displaying any output in the ALV. We have the following three ways
to build a field catalog.
Automatically through a Data Dictionary structure.
Manually in ABAP program.
Semi-automatically by combining the above two procedures.
The third option is used in the following cases.
We want to display all the fields from the DDIC structure but want to modify certain attributes like
descriptions etc.
We want to display most of the fields i.e. we want to hide certain fields.
Add new fields.
To generate a field catalog semi-automatically:
Declare an internal table of type SLIS_T_FIELDCAT_ALV.
Call function module REUSE_ALV_FIELDCATALOG_MERGE and pass the DDIC structure of the
output table and the internal table for the field catalog. The function module generates the field catalog and fills
the internal table accordingly.
Read the rows you want to change, and adapt the fields accordingly. If your output table contains more
fields than are stored in the Data Dictionary, you must append one row for each new field to the field catalog.
Example Program
TYPE-POOLS: slis. " SLIS contains all the ALV data types
*& *
*& *
status(4).
*& *
*& START-OF-SELECTION
*& *
START-OF-SELECTION.
EXPORTING
i_structure_name = 'SBOOK'
CHANGING
ct_fieldcat = it_fieldcat
EXCEPTIONS
inconsistent_interface = 1
program_error = 2
OTHERS = 3.
*Pass data and field catalog to ALV function module to display ALV list
EXPORTING
it_fieldcat = it_fieldcat
TABLES
t_outtab = it_sbook
EXCEPTIONS
program_error = 1
OTHERS = 2.
Output
Status column is added to the field catalog along all the fields of structure SBOOK.
TYPE-POOLS: slis.
* *
* Data Decalaration
* *
* *
* START-OF-SELECTION
* *
START-OF-SELECTION.
g_repid = sy-repid.
PERFORM build_alv_header.
EXPORTING
i_callback_program = g_repid
i_callback_top_of_page = 'TOP_OF_PAGE'
i_structure_name = 'SPFLI'
TABLES
t_outtab = it_spfli.
*& *
*& *
FORM build_alv_header .
wa_listheader-typ = 'H'.
CLEAR wa_listheader.
wa_listheader-typ = 'S'.
wa_listheader-key = 'Date :' .
CONCATENATE sy-datum+6(2)
sy-datum+4(2)
sy-datum(4)
INTO wa_listheader-info
SEPARATED BY '/'.
CLEAR wa_listheader.
wa_listheader-typ = 'A'.
CLEAR wa_listheader.
*& *
*& *
FORM top_of_page.
EXPORTING
it_list_commentary = it_listheader.
ENDFORM. "top_of_page
Output
Display Logo in ABAP ALV Header
Use the below steps to display the logo in ABAP ALV header.
1. First upload the logo that you want to display in ABAP ALV header.
2. Build the ALV header table.
3. Pass the TOP-OF-EVENT subroutine name and callback program name to
I_CALLBACK_TOP_OF_PAGE and I_CALLBACK_PROGRAM parameters of REUSE_ALV_GRID_DISPLAY
function module.
4. Display the ALV header and logo by passing the ALV header table built in step two and logo that you
uploaded in step one to function module ‘REUSE_ALV_COMMENTARY_WRITE’ inside TOP-OF-EVENT
subroutine.
TYPE-POOLS: slis.
* *
* Data Decalaration
* *
DATA: it_spfli TYPE TABLE OF spfli.
DATA: g_repid TYPE sy-repid.
DATA: it_listheader TYPE slis_t_listheader,
wa_listheader TYPE slis_listheader.
* *
* START-OF-SELECTION
* *
START-OF-SELECTION.
g_repid = sy-repid.
PERFORM build_alv_header.
CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
EXPORTING
i_callback_program = g_repid
i_callback_top_of_page = 'TOP_OF_PAGE'
i_structure_name = 'SPFLI'
TABLES
t_outtab = it_spfli.
*& *
*& Form BUILD_ALV_HEADER
*& *
FORM build_alv_header .
Output
*& *
*& 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.
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.
wa_fieldcat-fieldname = 'LOCCURAM'.
wa_fieldcat-seltext_m = 'Price'.
*Calculate Total for Price
wa_fieldcat-do_sum = 'X'.
APPEND wa_fieldcat TO it_fieldcat.
wa_fieldcat-fieldname = 'LOCCURKEY'.
wa_fieldcat-seltext_m = 'Currency'.
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.
Output
Subtotal in ABAP ALV
To calculate subtotal in ALV, build sort catalog and set the field SUBTOT of sort catalog to ‘X’.
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,
it_sort TYPE slis_t_sortinfo_alv,
wa_sort TYPE slis_sortinfo_alv..
*& *
*& START-OF-SELECTION
*& *
START-OF-SELECTION.
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.
wa_fieldcat-fieldname = 'LOCCURAM'.
wa_fieldcat-seltext_m = 'Price'.
wa_fieldcat-do_sum = 'X'.
APPEND wa_fieldcat TO it_fieldcat.
wa_fieldcat-fieldname = 'LOCCURKEY'.
wa_fieldcat-seltext_m = 'Currency'.
APPEND wa_fieldcat TO it_fieldcat.
*Build sort catalog
wa_sort-spos = 1.
wa_sort-fieldname = 'CARRID'.
wa_sort-up = 'X'.
wa_sort-subtot = 'X'.
APPEND wa_sort TO it_sort.
*Pass data and field catalog to ALV function module to display ALV list
CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
EXPORTING
it_fieldcat = it_fieldcat
it_sort = it_sort
TABLES
t_outtab = it_sbook
EXCEPTIONS
program_error = 1
OTHERS = 2.
Output
If you want to calculate subtotals for more fields, just add a record in sort field catalog for each field.
wa_sort-spos = 2.
wa_sort-fieldname = 'CONNID'.
wa_sort-up = 'X'.
wa_sort-subtot = 'X'.
APPEND wa_sort TO it_sort.
Output
Disable Icons in SAP ALV Toolbar
Use below steps to disable icons in ABAP ALV toolbar.
1. Fetch data from database table.
2. Build field catalog and fill the excluding table with function codes of icons that you want to disable.
&ILT& is the function code for Filter Icon.
3. Pass field catalog and excluding table to function module ‘REUSE_ALV_GRID_DISPLAY’.
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.
DATA: it_excluding TYPE slis_t_extab,
wa_excluding TYPE slis_extab.
DATA: g_repid TYPE sy-repid.
*& *
*& START-OF-SELECTION
*& *
START-OF-SELECTION.
g_repid = sy-repid.
*Fetch data from the database
SELECT * FROM sbook INTO TABLE it_sbook.
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
i_callback_program = g_repid
it_fieldcat = it_fieldcat
it_excluding = it_excluding
TABLES
t_outtab = it_sbook
EXCEPTIONS
program_error = 1
OTHERS = 2.
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.
DATA: g_repid TYPE sy-repid.
*& *
*& START-OF-SELECTION
*& *
START-OF-SELECTION.
g_repid = sy-repid.
*Fetch data from the database
SELECT * UP TO 20 ROWS FROM sbook INTO TABLE it_sbook.
wa_fieldcat-fieldname = 'CONNID'.
wa_fieldcat-seltext_m = 'Con. No.'.
APPEND wa_fieldcat TO it_fieldcat.
CLEAR wa_fieldcat.
wa_fieldcat-fieldname = 'FLDATE'.
wa_fieldcat-seltext_m = 'Date'.
APPEND wa_fieldcat TO it_fieldcat.
CLEAR wa_fieldcat.
wa_fieldcat-fieldname = 'BOOKID'.
wa_fieldcat-seltext_m = 'Book. ID'.
APPEND wa_fieldcat TO it_fieldcat.
CLEAR wa_fieldcat.
wa_fieldcat-fieldname = 'FORCURAM'.
wa_fieldcat-seltext_m = 'Price'.
wa_fieldcat-do_sum = 'X'.
wa_fieldcat-cfieldname = 'FORCURKEY'.
APPEND wa_fieldcat TO it_fieldcat.
CLEAR wa_fieldcat.
wa_fieldcat-fieldname = 'FORCURKEY'.
wa_fieldcat-seltext_m = 'Currency'.
APPEND wa_fieldcat TO it_fieldcat.
CLEAR wa_fieldcat.
wa_fieldcat-fieldname = 'LUGGWEIGHT'.
wa_fieldcat-seltext_m = 'Weight'.
wa_fieldcat-do_sum = 'X'.
wa_fieldcat-qfieldname = 'WUNIT'.
APPEND wa_fieldcat TO it_fieldcat.
CLEAR wa_fieldcat.
wa_fieldcat-fieldname = 'WUNIT'.
wa_fieldcat-seltext_m = 'Unit'.
APPEND wa_fieldcat TO it_fieldcat.
CLEAR wa_fieldcat.
*Pass data and field catalog to ALV function module to display ALV list
CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
EXPORTING
i_callback_program = g_repid
it_fieldcat = it_fieldcat
TABLES
t_outtab = it_sbook
EXCEPTIONS
program_error = 1
OTHERS = 2.
Output
TYPE-POOLS: slis. " SLIS contains all the ALV data types
*& *
*& Data Types
*& *
TYPES: BEGIN OF ty_sbook.
INCLUDE STRUCTURE sbook.
TYPES: icon TYPE c, " Add field to hold traffic light value
END OF ty_sbook.
*& *
*& Data Declaration
*& *
DATA: it_sbook TYPE TABLE OF ty_sbook.
DATA: wa_sbook TYPE ty_sbook.
DATA: it_fieldcat TYPE slis_t_fieldcat_alv,
wa_fieldcat TYPE slis_fieldcat_alv.
DATA: is_layout TYPE slis_layout_alv.
DATA: g_repid TYPE sy-repid.
*& *
*& START-OF-SELECTION
*& *
START-OF-SELECTION.
g_repid = sy-repid.
*Fetch data from the database
SELECT * UP TO 20 ROWS FROM sbook INTO TABLE it_sbook.
wa_fieldcat-fieldname = 'CONNID'.
wa_fieldcat-seltext_m = 'Con. No.'.
APPEND wa_fieldcat TO it_fieldcat.
CLEAR wa_fieldcat.
wa_fieldcat-fieldname = 'FLDATE'.
wa_fieldcat-seltext_m = 'Date'.
APPEND wa_fieldcat TO it_fieldcat.
CLEAR wa_fieldcat.
wa_fieldcat-fieldname = 'BOOKID'.
wa_fieldcat-seltext_m = 'Book. ID'.
APPEND wa_fieldcat TO it_fieldcat.
CLEAR wa_fieldcat.
wa_fieldcat-fieldname = 'FORCURAM'.
wa_fieldcat-seltext_m = 'Price'.
APPEND wa_fieldcat TO it_fieldcat.
CLEAR wa_fieldcat.
wa_fieldcat-fieldname = 'FORCURKEY'.
wa_fieldcat-seltext_m = 'Currency'.
APPEND wa_fieldcat TO it_fieldcat.
CLEAR wa_fieldcat.
wa_fieldcat-fieldname = 'LUGGWEIGHT'.
wa_fieldcat-seltext_m = 'Weight'.
APPEND wa_fieldcat TO it_fieldcat.
CLEAR wa_fieldcat.
wa_fieldcat-fieldname = 'WUNIT'.
wa_fieldcat-seltext_m = 'Unit'.
APPEND wa_fieldcat TO it_fieldcat.
CLEAR wa_fieldcat.
*Pass data and field catalog to ALV function module to display ALV list
CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
EXPORTING
i_callback_program = g_repid
is_layout = is_layout
it_fieldcat = it_fieldcat
TABLES
t_outtab = it_sbook
EXCEPTIONS
program_error = 1
OTHERS = 2.
Output