X

ALV Report in SAP ABAP

ALV Report is one of the many ways of displaying SAP table data in a reporting manner. Acronym ALV stands for ABAP List Viewer. ALV Report in SAP ABAP are very commonly used in many standard and custom SAP transaction across different SAP modules.

ALV reports include below in-built functions:

  • Sorting of records.
  • Filtering of records.
  • Totaling of records when there is a quantity column.
  • Sub totaling of records when there is a quantity column.
  • Hiding columns.
  • Changing order of columns.
  • Downloading report in Excel or HTML format.

Main advantage of these functions is, we can avoid implementing these all the way from scratch. You can use these functions because the mechanism we are using to develop an ALV report is standard function modules provided by SAP.

Types of ALV Reports in SAP

Below are the three types of ALV Reports in SAP ABAP:

  • Simple ALV Report.
  • Blocked ALV Report.
  • Hierarchical ALV Report.

At the early stages of SAP, we used the REUSE_ALV_LIST_DISPLAY function module to display an ALV list. With the rise of object-oriented programming now we can use the REUSE_ALV_GRID_DISPLAY function module to display an ALV grid. Though performance wise ALV grid display is below par from the ALV list display, developers tend to use ALV grid display mostly.

We can use the REUSE_ALV_FIELDCATALOG_MERGE function module to create a field catalog from an internal table or dictionary structure. By the way you don’t need to use this function module if you are selecting all the fields from one table.

Let’s assume that the user needs to view two or more reports in a single screen. How do you do that? Well, the solution is to benefit from Blocked ALV reports. Below function modules can be used to develop Blocked ALV reports.

  • REUSE_ALV_BLOCK_LIST_INIT
  • REUSE_ALV_BLOCK_LIST_APPEND
  • REUSE_ALV_BLOCK_LIST_DISPLAY

Then there is another type of ALV Report called Sequential ALV Reports. You can use these sequential reports to output header and item table details of a certain process. As an example, to process purchase orders, we can output the main header and item tables called EKKO and EKPO in a single report. With these kinds of reports users can analyze hierarchical breakdown of a process. The REUSE_ALV_HIERSEQ_LIST_DISPLAY function module can be used for this purpose.

Limitations of ALV Reports

Moving on to the limitations of ALV reports in SAP there are some. Below are those limitations:

  • Maximum of 255 characters can be shown on a column of a report.
  • Maximum of 90 columns can be displayed on a single report.
  • ALV list display is more efficient than ALV grid display considering the number of records can be shown on one go.

So far, we identified types of SAP ALV reports, pros, and cons of them. Let’s check on how to create a simple ALV report in SAP ABAP.

Create ALV Report in SAP ABAP

First, you need to create a program to develop the ALV report. You need to go to the transaction code SE38 called ABAP Editor and type a program name starting with ‘Z’ since this is a customizing program. Then you can click the create button. Check whether the Source Code option is already selected from the sub objects box before clicking on the create button.

In the Program Attributes window, type a descriptive name for the report in the Title text box. Select Executable program from Attributes box. Then click on the Save button. You will direct to the Object Directory Entry window after that. Here you can type the Package name in the Package text box in the Attributes box. After that you can click the Save button with the save icon.

Then, there is a window for a Transportable Workbench Request. Select the Transport Request from there and click on Okay button with green tick icon. Now, you will direct to ABAP Editor and let’s develop an ALV report using SAP standard function modules.

Requirement is to display records in the VBAP table in the ALV report.

First, you need to define VBAP as a table since you need to define a type using VBAP table fields after that. Then you can define a type called ty_vbap and include the fields in the VBAP table. Let’s include table fields vbeln, posnr, matnr, matkl, arktx, netwr, waerk, klmeng and vrkme. Table declaration and type declaration is done. Then we’ll move to the data declaration part.

For this part you need to define an internal table called lt_vbap using the type ty_vbap. With internal tables always comes a work area. Define ls_vbap work area using the type ty_vbap. Then we’ll define the field catalog of the ALV report. You need to define an internal table called lt_fieldcat using SAP standard type slis_t_fieldcat_alv and a work area called ls_fieldcat using SAP standard type slis_fieldcat_alv.

Data declaration part is also over now. You can name the definitions as you wish. But we are following the general coding standards. It will be very easy to identify definitions if you follow them.

Moving on to the selection screen. You can create a selection screen block called b1 and include select options as you wish. We’ll define two select options called so_vbeln and so_matnr for vbeln and matnr table fields in the VBAP table.

Now, we can fetch data from the VBAP table selecting relevant fields in the internal table. Don’t forget to filter out the select options using where clause. Here comes the filling out field catalog for ALV report in SAP. Since there are nine table fields that need to fill column position, field name, table name, and selection text one by one using ls_fieldcat. There are some important things to note when filling out a field catalog.

  • Fill field name using capital letters.
  • Table name should be the internal table name.
  • Keep in mind to fill the key attribute for the key field as X.
  • Append ls_fieldcat to lt_fieldcat after filling for one field.
  • Clear ls_fieldcat once it appended to lt_fieldcat.

Now comes the most important part. Calling the SAP standard function module called REUSE_ALV_GRID_DISPLAY. Pass i_callback_program exporting parameter sy-repid system variable value and it_fieldcat exporting parameter lt_fieldcat to field catalog. Pass table lt_vbap to tables and pass exceptions too in case of a failure.

If sy-subrc value is not equal to zero, you can implement suitable error handling. Better to add an error message with the relevant exception.

Sample Code of ALV Report in SAP ABAP

You can check out the sample ABAP code for your reference.

REPORT ztesting.

*Table Declaration
TABLES: vbap.

*Type Declaration
TYPES : BEGIN OF ty_vbap,

vbeln TYPE vbap-vbeln,
posnr TYPE vbap-posnr,
matnr TYPE vbap-matnr,
matkl TYPE vbap-matkl,
arktx TYPE vbap-arktx,
netwr TYPE vbap-netwr,
waerk TYPE vbap-waerk,
klmeng TYPE vbap-klmeng,
vrkme TYPE vbap-vrkme,

END OF ty_vbap.

*Data Declaration
DATA : lt_vbap TYPE STANDARD TABLE OF ty_vbap,
ls_vbap TYPE ty_vbap.

DATA : lt_fieldcat TYPE slis_t_fieldcat_alv,
ls_fieldcat TYPE slis_fieldcat_alv,
v_repid LIKE sy-repid.

*Selection Screen
SELECTION-SCREEN BEGIN OF BLOCK b1 WITH FRAME TITLE TEXT-002.
SELECT-OPTIONS: so_vbeln FOR vbap-vbeln,
so_matnr FOR vbap-matnr.
SELECTION-SCREEN END OF BLOCK b1.

START-OF-SELECTION.

*Data Fetching
SELECT vbeln

posnr
matnr
matkl
arktx
netwr
waerk
kimeng
vrkme

FROM vbap INTO TABLE lt_vbap
WHERE vbeln IN so_vbeln AND matnr IN so_matnr.

*Field Catalog
CLEAR ls_fieldcat.
ls_fieldcat-col_pos = 1.
ls_fieldcat-fieldname = 'VBELN'.
ls_fieldcat-tabname = 'IT_VBAP'.
ls_fieldcat-seltext_m = 'SD NO.'.
ls_fieldcat-key = 'X'.
APPEND ls_fieldcat TO lt_fieldcat.

CLEAR ls_fieldcat.
ls_fieldcat-col_pos = 2.
ls_fieldcat-fieldname = 'POSNR'.
ls_fieldcat-tabname = 'IT_VBAP'.
ls_fieldcat-seltext_m = 'ITEM NO.'.
APPEND ls_fieldcat TO lt_fieldcat.

CLEAR ls_fieldcat.
ls_fieldcat-col_pos = 3.
ls_fieldcat-fieldname = 'MATNR'.
ls_fieldcat-tabname = 'IT_VBAP'.
ls_fieldcat-seltext_m = 'MATERIAL NO.'.
APPEND ls_fieldcat TO lt_fieldcat.

CLEAR ls_fieldcat.
ls_fieldcat-col_pos = 4.
ls_fieldcat-fieldname = 'MATKL'.
ls_fieldcat-tabname = 'IT_VBAP'.
ls_fieldcat-seltext_m = 'MATERIAL GROUP'.
APPEND ls_fieldcat TO lt_fieldcat.

CLEAR ls_fieldcat.
ls_fieldcat-col_pos = 5.
ls_fieldcat-fieldname = 'ARKTX'.
ls_fieldcat-tabname = 'IT_VBAP'.
ls_fieldcat-seltext_m = 'DESCRIPTION'.
APPEND ls_fieldcat TO lt_fieldcat.

CLEAR ls_fieldcat.
ls_fieldcat-col_pos = 6.
ls_fieldcat-fieldname = 'NETWR'.
ls_fieldcat-tabname = 'IT_VBAP'.
ls_fieldcat-seltext_m = 'AMOUNT'.
APPEND ls_fieldcat TO lt_fieldcat.

CLEAR ls_fieldcat.
ls_fieldcat-col_pos = 7.
ls_fieldcat-fieldname = 'WAERK'.
ls_fieldcat-tabname = 'IT_VBAP'.
ls_fieldcat-seltext_m = 'CURRENCY'.
APPEND ls_fieldcat TO lt_fieldcat.

CLEAR ls_fieldcat.
ls_fieldcat-col_pos = 8.
ls_fieldcat-fieldname = 'KLMENG'.
ls_fieldcat-tabname = 'IT_VBAP'.
ls_fieldcat-seltext_m = 'QUANTITY'.
APPEND ls_fieldcat TO lt_fieldcat.

CLEAR ls_fieldcat.
ls_fieldcat-col_pos = 9.
ls_fieldcat-fieldname = 'VRKME'.
ls_fieldcat-tabname = 'IT_VBAP'.
ls_fieldcat-seltext_m = 'SALES UNIT'.
APPEND ls_fieldcat TO lt_fieldcat.

CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
EXPORTING
i_callback_program = sy-repid
it_fieldcat = lt_fieldcat
TABLES
t_outtab = lt_vbap
EXCEPTIONS
program_error = 1
OTHERS = 2.

IF sy-subrc <> 0.
* Implement suitable error handling here
ENDIF.

Did you like this tutorial? Have any questions or comments? We would love to hear your feedback in the comments section below. It’d be a big help for us, and hopefully it’s something we can address for you in improvement of our free SAP ABAP tutorials.

Navigation Links

Go to next lesson:

Go to previous lesson: SAP ABAP Dialog Programming

Go to overview of the course: SAP ABAP Training

View Comments (8)