SAP ABAP blog

ALV Reports

Sap provides a set of ALV (ABAP LIST VIEWER) function modules which can be put into use to embellish the output of a report. This set of ALV functions is used to enhance the readability and functionality of any report output. Cases arise in sap when the output of a report contains columns extending more than 255 characters in length.

In such cases, this set of ALV functions can help choose selected columns and arrange the different columns from a report output and also save different variants for report display. This is a very efficient tool for dynamically sorting and arranging the columns from a report output.

The report output can contain up to 90 columns in the display with the wide array of display options.

The commonly used ALV functions used for this purpose are;

1. REUSE_ALV_VARIANT_DEFAULT_GET
2. REUSE_ALV_VARIANT_F4
3. REUSE_ALV_VARIANT_EXISTENCE
4. REUSE_ALV_EVENTS_GET
5. REUSE_ALV_COMMENTARY_WRITE
6. REUSE_ALV_FIELDCATALOG_MERGE
7. REUSE_ALV_LIST_DISPLAY
8. REUSE_ALV_GRID_DISPLAY
9. REUSE_ALV_POPUP_TO_SELECT

Purpose of the above Functions are differ not all the functions are required in all the ALV Report.

But either no.7 or No.8 is there in the Program.

How you call this function in your report?

After completion of all the data fetching from the database and append this data into an Internal Table. say I_ITAB.

Then use follwing function module.

CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
EXPORTING
I_CALLBACK_PROGRAM = 'Prog.name'
I_STRUCTURE_NAME = 'I_ITAB'
I_DEFAULT = 'X'
I_SAVE = 'A'
TABLES
T_OUTTAB = I_ITAB.
IF SY-SUBRC <> 0.
WRITE: 'SY-SUBRC: ', SY-SUBRC .
ENDIF.
ENDFORM. " GET_FINAL_DATA



A Simple ABAP ALV LIST VIEWER Example
This ALV program have all the basic report requirements such as page heading, page no, sub-total and a grand total.

* This is a basic ALV with the followings:-
* - Page Heading
* - Page No
* - Sub-Total
* - Grand Total

REPORT ZALV.

TYPE-POOLS: SLIS.

DATA: G_REPID LIKE SY-REPID,
GS_PRINT TYPE SLIS_PRINT_ALV,
GT_LIST_TOP_OF_PAGE TYPE SLIS_T_LISTHEADER,
GT_EVENTS TYPE SLIS_T_EVENT,
GT_SORT TYPE SLIS_T_SORTINFO_ALV,
GS_LAYOUT TYPE SLIS_LAYOUT_ALV,
GT_FIELDCAT TYPE SLIS_T_FIELDCAT_ALV,
FIELDCAT_LN LIKE LINE OF GT_FIELDCAT,
COL_POS TYPE I.

DATA: BEGIN OF ITAB,
FIELD1(5) TYPE C,
FIELD2(5) TYPE C,
FIELD3(5) TYPE P DECIMALS 2,
END OF ITAB.

DATA: BEGIN OF ITAB1 OCCURS 0.
INCLUDE STRUCTURE ITAB.
DATA: END OF ITAB1.

DATA: BEGIN OF ITAB_FIELDCAT OCCURS 0.
INCLUDE STRUCTURE ITAB.
DATA: END OF ITAB_FIELDCAT.

* Print Parameters
PARAMETERS:
P_PRINT AS CHECKBOX DEFAULT ' ', "PRINT IMMEDIATE
P_NOSINF AS CHECKBOX DEFAULT 'X', "NO SELECTION INFO
P_NOCOVE AS CHECKBOX DEFAULT ' ', "NO COVER PAGE
P_NONEWP AS CHECKBOX DEFAULT ' ', "NO NEW PAGE
P_NOLINF AS CHECKBOX DEFAULT 'X', "NO PRINT LIST INFO
P_RESERV TYPE I. "NO OF FOOTER LINE

INITIALIZATION.
G_REPID = SY-REPID.
PERFORM PRINT_BUILD USING GS_PRINT. "Print PARAMETERS

START-OF-SELECTION.
* TEST DATA
MOVE 'TEST1' TO ITAB1-FIELD1.
MOVE 'TEST1' TO ITAB1-FIELD2.
MOVE '10.00' TO ITAB1-FIELD3.
APPEND ITAB1.

MOVE 'TEST2' TO ITAB1-FIELD1.
MOVE 'TEST2' TO ITAB1-FIELD2.
MOVE '20.00' TO ITAB1-FIELD3.
APPEND ITAB1.

DO 50 TIMES.
APPEND ITAB1.
ENDDO.

END-OF-SELECTION.

PERFORM BUILD.
PERFORM EVENTTAB_BUILD CHANGING GT_EVENTS.
PERFORM COMMENT_BUILD CHANGING GT_LIST_TOP_OF_PAGE.
PERFORM CALL_ALV.

FORM BUILD.
* DATA FIELD CATALOG
* Explain Field Description to ALV
DATA: FIELDCAT_IN TYPE SLIS_FIELDCAT_ALV.

CLEAR FIELDCAT_IN.
FIELDCAT_LN-FIELDNAME = 'FIELD1'.
FIELDCAT_LN-TABNAME = 'ITAB1'.
*FIELDCAT_LN-NO_OUT = 'X'. "FIELD NOT DISPLAY, CHOOSE FROM LAYOUT
FIELDCAT_LN-KEY = ' '. "SUBTOTAL KEY
FIELDCAT_LN-NO_OUT = ' '.
FIELDCAT_LN-SELTEXT_L = 'HEAD1'.
APPEND FIELDCAT_LN TO GT_FIELDCAT.

CLEAR FIELDCAT_IN.
FIELDCAT_LN-FIELDNAME = 'FIELD2'.
FIELDCAT_LN-TABNAME = 'ITAB1'.
FIELDCAT_LN-NO_OUT = 'X'.
FIELDCAT_LN-SELTEXT_L = 'HEAD2'.
APPEND FIELDCAT_LN TO GT_FIELDCAT.

CLEAR FIELDCAT_IN.
FIELDCAT_LN-FIELDNAME = 'FIELD3'.
FIELDCAT_LN-TABNAME = 'ITAB1'.
FIELDCAT_LN-REF_FIELDNAME = 'MENGE'. "<- REF FIELD IN THE DICTIONNARY FIELDCAT_LN-REF_TABNAME = 'MSEG'. "<- REF TABLE IN THE DICTIONNARY FIELDCAT_LN-NO_OUT = ' '. FIELDCAT_LN-DO_SUM = 'X'. "SUM UPON DISPLAY APPEND FIELDCAT_LN TO GT_FIELDCAT. * DATA SORTING AND SUBTOTAL DATA: GS_SORT TYPE SLIS_SORTINFO_ALV. CLEAR GS_SORT. GS_SORT-FIELDNAME = 'FIELD1'. GS_SORT-SPOS = 1. GS_SORT-UP = 'X'. GS_SORT-SUBTOT = 'X'. APPEND GS_SORT TO GT_SORT. CLEAR GS_SORT. GS_SORT-FIELDNAME = 'FIELD2'. GS_SORT-SPOS = 2. GS_SORT-UP = 'X'. *GS_SORT-SUBTOT = 'X'. APPEND GS_SORT TO GT_SORT. ENDFORM. FORM CALL_ALV. * ABAP List Viewer CALL FUNCTION 'REUSE_ALV_LIST_DISPLAY' EXPORTING * I_INTERFACE_CHECK = ' ' * I_BYPASSING_BUFFER = * I_BUFFER_ACTIVE = ' ' I_CALLBACK_PROGRAM = G_REPID * I_CALLBACK_PF_STATUS_SET = ' ' * I_CALLBACK_USER_COMMAND = ' ' I_STRUCTURE_NAME = 'ITAB1' IS_LAYOUT = GS_LAYOUT IT_FIELDCAT = GT_FIELDCAT[] * IT_EXCLUDING = * IT_SPECIAL_GROUPS = IT_SORT = GT_SORT[] * IT_FILTER = * IS_SEL_HIDE = * I_DEFAULT = 'X' * I_SAVE = ' ' * IS_VARIANT = IT_EVENTS = GT_EVENTS[] * IT_EVENT_EXIT = IS_PRINT = GS_PRINT * IS_REPREP_ID = * I_SCREEN_START_COLUMN = 0 * I_SCREEN_START_LINE = 0 * I_SCREEN_END_COLUMN = 0 * I_SCREEN_END_LINE = 0 * IMPORTING * E_EXIT_CAUSED_BY_CALLER = * ES_EXIT_CAUSED_BY_USER = TABLES T_OUTTAB = ITAB1 EXCEPTIONS PROGRAM_ERROR = 1 OTHERS = 2. ENDFORM. * HEADER FORM FORM EVENTTAB_BUILD CHANGING LT_EVENTS TYPE SLIS_T_EVENT. CONSTANTS: GC_FORMNAME_TOP_OF_PAGE TYPE SLIS_FORMNAME VALUE 'TOP_OF_PAGE'. *GC_FORMNAME_END_OF_PAGE TYPE SLIS_FORMNAME VALUE 'END_OF_PAGE'. DATA: LS_EVENT TYPE SLIS_ALV_EVENT. CALL FUNCTION 'REUSE_ALV_EVENTS_GET' EXPORTING I_LIST_TYPE = 0 IMPORTING ET_EVENTS = LT_EVENTS. READ TABLE LT_EVENTS WITH KEY NAME = SLIS_EV_TOP_OF_PAGE INTO LS_EVENT. IF SY-SUBRC = 0. MOVE GC_FORMNAME_TOP_OF_PAGE TO LS_EVENT-FORM. APPEND LS_EVENT TO LT_EVENTS. ENDIF. * define END_OF_PAGE event * READ TABLE LT_EVENTS WITH KEY NAME = SLIS_EV_END_OF_PAGE * INTO LS_EVENT. * IF SY-SUBRC = 0. * MOVE GC_FORMNAME_END_OF_PAGE TO LS_EVENT-FORM. * APPEND LS_EVENT TO LT_EVENTS. * ENDIF. ENDFORM. FORM COMMENT_BUILD CHANGING GT_TOP_OF_PAGE TYPE SLIS_T_LISTHEADER. DATA: GS_LINE TYPE SLIS_LISTHEADER. CLEAR GS_LINE. GS_LINE-TYP = 'H'. GS_LINE-INFO = 'HEADER 1'. APPEND GS_LINE TO GT_TOP_OF_PAGE. CLEAR GS_LINE. GS_LINE-TYP = 'S'. GS_LINE-KEY = 'STATUS 1'. GS_LINE-INFO = 'INFO 1'. APPEND GS_LINE TO GT_TOP_OF_PAGE. GS_LINE-KEY = 'STATUS 2'. GS_LINE-INFO = 'INFO 2'. APPEND GS_LINE TO GT_TOP_OF_PAGE. * CLEAR GS_LINE. * GS_LINE-TYP = 'A'. * * GS_LINE-INFO = 'ACTION'. * APPEND GS_LINE TO GT_TOP_OF_PAGE. ENDFORM. FORM TOP_OF_PAGE. CALL FUNCTION 'REUSE_ALV_COMMENTARY_WRITE' EXPORTING IT_LIST_COMMENTARY = GT_LIST_TOP_OF_PAGE. WRITE: SY-DATUM, 'Page No', SY-PAGNO LEFT-JUSTIFIED. ENDFORM. FORM END_OF_PAGE. WRITE at (sy-linsz) sy-pagno CENTERED. ENDFORM. * PRINT SETTINGS FORM PRINT_BUILD USING LS_PRINT TYPE SLIS_PRINT_ALV. LS_PRINT-PRINT = P_PRINT. "PRINT IMMEDIATE LS_PRINT-NO_PRINT_SELINFOS = P_NOSINF. "NO SELECTION INFO LS_PRINT-NO_COVERPAGE = P_NOCOVE. "NO COVER PAGE LS_PRINT-NO_NEW_PAGE = P_NONEWP. LS_PRINT-NO_PRINT_LISTINFOS = P_NOLINF. "NO PRINT LIST INFO LS_PRINT-RESERVE_LINES = P_RESERV. ENDFORM. *END OF ZALV PROGRAM Question
How many types of reports are there in ABAP and what is the difference between them?

There are 2 type of reports. They are:

1.Interactive report
2.Classic reports

In classic reports,we can see the output in single list where as in interactive reports we can see the output in multiple list.

Answer
In ABAP, there are a total of 7 types of reports. They are:

1.Classical
2.Interactive
3.Logical Database
4.ABAP query
5.ALV Reports (ALV stands for ABAP List Viewer)
6.Report Writer/Report Painter
7.Views (There are different types of views also)

Classical Reports

These are the most simple reports. Programmers learn this one first. It is just an output of data using the Write statement inside a loop.

Classical reports are normal reports. These reports are not having any sub reports. IT IS HAVING ONLY ONE SCREEN/LIST FOR OUTPUT.
Events In Classical Reports.

INTIALIZATION: This event triggers before selection screen display.
AT-SELECTION-SCREEN: This event triggers after proccesing user input still selection screen is in active mode.
START OF SELECTION: Start of selection screen triggers after proceesing selection screen.
END-OF-SELECTION : It is for Logical Database Reporting.

Interactive Reports
As the name suggests, the user can Interact with the report. We can have a drill down into the report data. For example, Column one of the report displays the material numbers, and the user feels that he needs some more specific data about the vendor for that material, he can HIDE that data under those material numbers.

And when the user clicks the material number, another report (actually sub report/secondary list) which displays the vendor details will be displayed.

We can have a basic list (number starts from 0) and 20 secondary lists (1 to 21).

Events associated with Interactive Reports are:

1.AT LINE-SELECTION
2.AT USER-COMMAND
3.AT PF
4.TOP-OF-PAGE DURING LINE-SELECTION.
HIDE statement holds the data to be displayed in the secondary list.

sy-lisel : contains data of the selected line.

sy-lsind : contains the level of report (from 0 to 21)

Interactive Report Events:

AT LINE-SELECTION : This Event triggers when we double click a line on the list, when the event is triggered a new sublist is going to be generated. Under this event what ever the statements that are been return will be displayed on newly generated sublist.
AT PFn: For predefined function keys...
AT USER-COMMAND : It provides user functions keys.
TOP-OF-PAGE DURING LINE-SELECTION :top of page event for secondary list.

Logical Database Reports
Logical database is another tool for ABAP reports. Using LDB we can provide extra features for ABAP reports.

While using LDB there is no need for us to declare Parameters.

Selection-screen as they will be generated automatically.

We have to use the statement NODES in ABAP report.

If there are many tables the Performance will be slow as all the table data will be read from top node to bottom node .

ABAP Query Reports

ABAP query is another tool for ABAP. It provides efficency for ABAP reports. These reports are very accurate.

Transaction Code : SQ01

The advantage with ABAP QUERY is logic required for classic & interactive reports system design automatically 80%.
For ABAP QUERY handle these

SQ01 ; QUERY
SQ02 : INFOSET OR FUNCTIONAL AREA
SQ03: USER GROUP.
[edit]Report Writer
Key Concept :

Super users and end users can use Report Painter/Report Writer tools to write their own reports.

Giving them the ability to report on additional fields at their discretion shifts the report maintenance burden to them, saving SAP support groups time and effort normally spent creating and maintaining the reports.

Instead of using ABAP code to write a report in FI and CO, many users build a Report Painter/ Report Writer library using transaction MC27.

However, this workaround has some drawbacks. Little known transaction GRCT solves these problems in most cases, and eliminates the need to use transaction MC27.

ABAP Report Types
ABAP report types are those ones available in some report's attributes screen, i.e. :

Executable program
Function group (containing function modules)
Include
Interface pool
Class pool
Module pool
Subroutine pool
Also ALV means ABAP List Viewer. Most convenient way to use it is through reuse library (cf. transaction se83) available from release 4.6 of SAP R/3.

ALV is available in two modes: list and grid. List mode is good old list processing with standard functionnalities, and grid mode is using a new OCX object displaying grids.