Your First ALV Report (Program 1)

Your First ALV Report (Program 1)
ABAP Reports Series · Blog 5 of 15

Your First ALV Report (Program 1)

Beginner · 15 min read · Needs Blogs 3 and 4

1. Why ALV? (And why WRITE is not enough)

ALV stands for ABAP List Viewer. It shows your internal table as an interactive grid. With WRITE you only get plain text. With ALV the user gets, for free:

  • Sorting by any column
  • Filtering and searching
  • Totals and subtotals
  • Moving, hiding and resizing columns
  • Export to Excel or PDF
  • Saving personal layouts

Nearly every report in a real project ends in ALV, so this is the most useful step in the series.

2. Two ways to build ALV

ApproachStyleNotes
REUSE_ALV_GRID_DISPLAYFunction moduleSimple, very common in existing code. Used in this series first.
CL_SALV_TABLEObject-orientedSAP’s recommended choice for new read-only ALV. Covered in Blog 15.
CL_GUI_ALV_GRIDObject-orientedMost control, including editable cells. More code.

You will meet REUSE_ALV_GRID_DISPLAY in a huge amount of existing code, so learning it first helps you read what others wrote.

3. The two building blocks

Every ALV needs exactly two things:

BlockWhat it isIn Program 1
Output tableThe internal table with the data to showlt_vbak
Field catalogA description of each column: name, heading, length, formatBuilt automatically from structure ZST_VBAK
Auto vs. manual

If you pass a dictionary structure name, ALV builds the field catalog for you. This blog uses that. Blog 6 builds the catalog by hand when you need full control.

4. Create the ZST_VBAK structure (SE11)

The structure tells ALV which columns exist and what to call them.

  1. Open SE11, select Data type, enter ZST_VBAK, and click Create.
  2. Choose Structure and enter a short description, for example Sales order header for ALV.
  3. On the Components tab, add these rows. Use the same name for the component and the data element.
  4. Open the Currency/Quantity Fields tab. For NETWR, enter reference table ZST_VBAK and reference field WAERK.
  5. Choose Extras > Enhancement Category and pick Can be enhanced (character-type or numeric), or whatever your team uses.
  6. Save and activate with Ctrl+F3.
ComponentData elementMeaning
VBELNVBELNSales document number
ERDATERDATCreated on
ERNAMERNAMCreated by
VBTYPVBTYPDocument category
NETWRNETWRNet value
WAERKWAERKDocument currency
Do not skip the currency reference

A currency amount needs to know its currency to display correctly. Without the reference in step 4, the structure will not activate.

5. Program 1: full code

*&---------------------------------------------------------------------*
*& Report ZALV_PROGRAM1
*& First ALV report: automatic field catalog from a DDIC structure
*&---------------------------------------------------------------------*
REPORT zalv_program1.

*---------------------------------------------------------------------*
* Data declarations
*---------------------------------------------------------------------*
DATA lt_vbak  TYPE STANDARD TABLE OF zst_vbak WITH DEFAULT KEY.
DATA lv_erdat TYPE vbak-erdat.
DATA lv_repid TYPE sy-repid.

*---------------------------------------------------------------------*
* Selection screen
*---------------------------------------------------------------------*
SELECT-OPTIONS s_erdat FOR lv_erdat.
PARAMETERS p_max TYPE i DEFAULT 500.

INITIALIZATION.
  s_erdat-sign   = 'I'.
  s_erdat-option = 'BT'.
  s_erdat-low    = sy-datum - 30.
  s_erdat-high   = sy-datum.
  APPEND s_erdat.

*---------------------------------------------------------------------*
* Main logic
*---------------------------------------------------------------------*
START-OF-SELECTION.

  " 1. Read data
  SELECT vbeln, erdat, ernam, vbtyp, netwr, waerk
    FROM vbak
    WHERE erdat IN @s_erdat
    ORDER BY erdat DESCENDING, vbeln
    INTO CORRESPONDING FIELDS OF TABLE @lt_vbak
    UP TO @p_max ROWS.

  IF sy-subrc <> 0.
    MESSAGE 'No sales orders found for your selection'
            TYPE 'S' DISPLAY LIKE 'E'.
    RETURN.
  ENDIF.

  " 2. Show data in ALV (field catalog built automatically)
  lv_repid = sy-repid.

  CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
    EXPORTING
      i_callback_program = lv_repid
      i_structure_name   = 'ZST_VBAK'
    TABLES
      t_outtab           = lt_vbak
    EXCEPTIONS
      program_error      = 1
      OTHERS             = 2.

  IF sy-subrc <> 0.
    MESSAGE 'ALV could not be displayed' TYPE 'S' DISPLAY LIKE 'E'.
  ENDIF.

6. Walkthrough

PartWhat it does
TYPE STANDARD TABLE OF zst_vbakThe output table has exactly the columns of the structure.
s_erdat, p_maxThe user picks the date range and the maximum rows (Blog 2).
INITIALIZATIONPre-fills the last 30 days.
SELECT ... INTO CORRESPONDING FIELDS OF TABLEFills only the columns that exist in both the query and the table.
IF sy-subrc <> 0Stops with a clear message if nothing was found (Blog 3).
lv_repid = sy-repidStores the program name in a variable first. This is safer than passing sy-repid directly.
i_callback_programTells ALV which program it belongs to.
i_structure_nameThe magic line. ALV reads the structure and builds the field catalog by itself.
TABLES t_outtabThe internal table to display.
EXCEPTIONSHandles a failure of the function module.
Where do the column headings come from?

From the data elements in the structure. Their short, medium and long texts become the column headings, so a clean data element gives you good headings for free.

7. What the user sees

When you run the report, press F8 on the selection screen. A grid like this appears (values will differ):

Sales Doc.Created onCreated byCat.Net valueCurr.
000000497130.10.2026SALES01C12,500.00INR
000000497028.10.2026SALES02C8,200.00INR
000000496925.10.2026SALES01A1,450.00INR

The toolbar above the grid comes automatically. Try these:

  • Click a column header, then the sort icons.
  • Select a column and use the filter icon.
  • Select Net value and click the sum icon.
  • Use Export > Spreadsheet to open the data in Excel.
  • Use Settings > Layout to hide or reorder columns.

8. The same report with SALV

To give you a preview of the modern approach, here is the display part using CL_SALV_TABLE. It needs no structure name because it reads the table’s own type.

TRY.
    cl_salv_table=>factory(
      IMPORTING r_salv_table = DATA(lo_alv)
      CHANGING  t_table      = lt_vbak ).

    lo_alv->get_functions( )->set_all( abap_true ).
    lo_alv->get_columns( )->set_optimize( abap_true ).
    lo_alv->display( ).
  CATCH cx_salv_msg INTO DATA(lx_msg).
    MESSAGE lx_msg->get_text( ) TYPE 'S' DISPLAY LIKE 'E'.
ENDTRY.

Both give a similar grid. Blog 15 compares them in depth.

9. Try it yourself

  • Add a new field VKORG to ZST_VBAK, add it to the SELECT, and see the new column.
  • Add a selection screen field for sales organization (s_vkorg) and use it in WHERE.
  • Change ORDER BY to sort by netwr DESCENDING.
  • In the ALV, sort by Created by, then save a layout.
  • Export the grid to Excel and compare it with what you see on screen.

10. Common mistakes

  • Structure not activated. The program fails or shows no columns. Activate it in SE11.
  • No currency reference for NETWR, so the structure will not activate.
  • Structure name in lowercase or misspelled. Use the exact name in capitals: 'ZST_VBAK'.
  • Table does not match the structure. Declare the output table TYPE ... OF zst_vbak.
  • Forgetting TABLES t_outtab. ALV has nothing to show.
  • No sy-subrc check after SELECT. The user sees an empty grid with no explanation.
  • Skipping i_callback_program. Later features such as user commands will not work.
  • Reading millions of rows. Use WHERE and UP TO n ROWS while testing.

Up next: Blog 6

Manual Field Catalog and Report Events. Take full control of column names, widths and positions, and add a header to your ALV.

ABAP Reports: Zero to Hero · Blog 5 of 15
WhatsApp WhatsApp us
Call Now Button