ALV (SAP List Viewer)

This content was generated with the assistance of AI and should be verified against the official documentation before being relied on in production.

When a user starts a report and the result is a table of rows, the result should not be a wall of text. Users expect to click a column heading to sort, to filter, to add up a column, to change which columns are visible and to send the table to a spreadsheet. In classic ABAP the standard way to give them all of that is ALV, the SAP List Viewer: you hand it an internal table, and it draws an interactive table with those functions built in. After reading this page you can show an internal table as an ALV grid with cl_salv_table=>factory, set column headings, sorting, totals and toolbar functions, react to a double click, tell the three generations of ALV apart (the old function modules, the grid control and the object model), and decide when ALV is the wrong tool. The page is written against Standard ABAP release 8.16. ALV is part of classic ABAP and needs SAP GUI to draw its output, so it applies in S/4HANA on premise and Private Edition and in older systems, and it is not a technique of ABAP for Cloud Development (release 9.20, 2608), where user interfaces are Fiori apps described by CDS annotations. Every listing here needs an SAP system and is labelled as not run here, because the local playground has no SAP GUI and no ALV classes.

Before you read

Key terms

  • ALV: the SAP List Viewer, a standard tool that displays an internal table as an interactive table with sorting, filtering, totals and export.

  • ALV object model: the set of SALV classes (CL_SALV_TABLE and its sub-objects) that SAP recommends for new ALV output.

  • Field catalog: the description of the columns (name, heading, type, width) that the older ALV tools needed from the programmer; the object model builds it for you.

  • GUI control: a visual element that runs in SAP GUI on the user’s PC and is managed by the Control Framework, for example the ALV grid.

  • Integrated Data Access (IDA): a way to show a database table in ALV without selecting it into an internal table first, so that sorting and paging run in the database.

  • SAP Fiori elements: ready-made app templates that draw a list or an object page from CDS annotations, instead of a program drawing the screen.

What ALV is and what it gives the user

A classic WRITE list is plain text laid out line by line. It cannot sort itself, and the program has to code every filter and every total. ALV works the other way round. The program supplies only data, and ALV supplies the table and its behaviour. Out of the box a user can sort by one or several columns, filter, set totals and subtotals, hide or move columns, save the arrangement as a layout, print and export to a spreadsheet. Because this is the same everywhere in SAP, a user who knows one ALV list knows all of them, which is why almost every report in a classic system ends in ALV.

The SAP documentation names three tools that the object model covers: a simple two-dimensional table, a hierarchical-sequential list with two levels, and a tree. This page concentrates on the simple table, which is by far the most common case.

Three generations of ALV

ALV has existed for a very long time, so a classic system contains all three of these ways to call it. You need to recognise all three, because you will maintain old programs, but you write only the third in new code.

Generation How the program calls it Status

Reuse library

Function modules such as REUSE_ALV_GRID_DISPLAY and REUSE_ALV_LIST_DISPLAY. The program fills a field catalog and a layout structure and passes them with the table.

Legacy. Old reports use it, and a book from the time of ABAP 7.x describes it at length. No official help page was found that describes the function modules, so their in-system documentation (transaction SE37) is the reference.

ALV Grid Control

The class CL_GUI_ALV_GRID placed in a container on a screen, again with a field catalog and a layout structure.

The SAP help page for it states that it is no longer recommended and should be used only if the object-oriented ALV cannot be used for technical reasons. It is a GUI control (see Control Framework and GUI Controls).

Object model

The classes of package SALV: CL_SALV_TABLE for a simple table, CL_SALV_HIERSEQ_TABLE for a two-level list and CL_SALV_TREE for a tree.

Recommended by SAP for new programs. All functions of the grid control are available in it.

The old function modules and the grid control are no longer the way to learn ALV. The book Complete ABAP (SAP PRESS, 1st edition 2016, for ABAP 7.4 and 7.5) spends most of its ALV chapter on the field catalog and on those two tools. Read that part to understand an old program, but do not copy it into new code: the object model builds the catalog itself from the table you pass and is shorter, safer (it raises exceptions instead of failing quietly) and keeps the same techniques for all three list kinds.

flowchart TD A[Need to show rows to a user] --> B{ABAP Cloud or a Fiori app?} B -- Yes --> C[Do not use ALV: CDS view, RAP, Fiori elements list report] B -- No, classic report --> D{Table is huge and on SAP HANA?} D -- Yes --> E[ALV with IDA: CL_SALV_GUI_TABLE_IDA] D -- No --> F{Editing the same program from long ago?} F -- Yes --> G[Keep its REUSE_ALV or grid control code, replace when you rewrite] F -- No --> H[Object model: CL_SALV_TABLE]

The smallest ALV program

The idea is two method calls. FACTORY creates the ALV object for your table, and DISPLAY shows it. The SAP documentation says that these two are enough to display output. FACTORY is a static method of CL_SALV_TABLE; it receives the internal table in the CHANGING parameter t_table and returns the ALV object in the IMPORTING parameter r_salv_table. It can raise the exception CX_SALV_MSG, so the call sits in a TRY block.

The listing builds three claims in a local table of the running example and shows them. It requires an SAP system; not run here.

REPORT zexp_alv_basic.

TYPES: BEGIN OF ty_claim,
         claim_id TYPE c LENGTH 10,
         employee TYPE c LENGTH 20,
         status   TYPE c LENGTH 1,
         amount   TYPE p LENGTH 8 DECIMALS 2,
         currency TYPE waers,
       END OF ty_claim.

TYPES ty_claims TYPE STANDARD TABLE OF ty_claim WITH EMPTY KEY.

START-OF-SELECTION.
  DATA(claims) = VALUE ty_claims(
    ( claim_id = 'C-0001' employee = 'Ana'   status = 'S' amount = '120.50' currency = 'EUR' )
    ( claim_id = 'C-0002' employee = 'Ben'   status = 'A' amount = '48.00'  currency = 'EUR' )
    ( claim_id = 'C-0003' employee = 'Carla' status = 'S' amount = '310.75' currency = 'USD' ) ).

  TRY.
      cl_salv_table=>factory(
        IMPORTING r_salv_table = DATA(alv)
        CHANGING  t_table      = claims ).
      alv->display( ).
    CATCH cx_salv_msg INTO DATA(error).
      MESSAGE error TYPE 'I' DISPLAY LIKE 'E'.
  ENDTRY.

The documentation passes the table only the first time, in FACTORY; later changes to the data use other methods of the object. A column heading comes from the data element of the field in the ABAP Dictionary. The local type above has no data elements, so the headings would fall back to the field names. The next section shows how to set them in code, which is also what you do whenever the dictionary label does not suit the report.

Display types: full screen, container and list

By default, DISPLAY shows the grid on a full screen of its own. Two variants of FACTORY change that. Passing a container (the parameters r_container and container_name) puts the ALV inside a screen area that you control, which is how a grid sits next to other controls on a dialog screen; it belongs to the Control Framework and needs a screen with a custom control. Passing the parameter list_display switches to the classic list look instead of the grid. The documentation notes that some settings are available only in one display type, so check the in-system documentation of a method before relying on it in all three.

Columns, sorting, totals and toolbar

Everything beyond the basic display is set through sub-objects that you get from the ALV object with get_... methods. The class name tells you what it controls: a singular name such as CL_SALV_COLUMN acts on one object, a plural name such as CL_SALV_COLUMNS on all of them. Three getters exist on all three main classes: get_columns, get_functions and get_event. get_sorts exists on the simple table and the hierarchical list but not on the tree.

flowchart LR A["CL_SALV_TABLE object"] --> B["get_columns"] A --> C["get_sorts"] A --> D["get_aggregations"] A --> E["get_functions"] A --> F["get_display_settings"] A --> G["get_event"] B --> B1["get_column: heading, visibility, currency"] C --> C1["add_sort: order of rows"] D --> D1["add_aggregation: totals"] E --> E1["set_all: standard toolbar"] F --> F1["striped rows, list header"] G --> G1["double click, link click"]

The recipe never changes: declare a reference to the sub-object class, call the getter, set properties with the sub-object’s methods, and finally call display. The listing below formats the claims of the previous example. It widens the columns to fit their content, gives readable headings, hides the status column, sorts by employee, adds a total of the amount (split by currency, because the amount column is told which column holds its currency), switches on the standard toolbar functions and stripes the rows. Column names are written in capital letters because they are the field names of the table row. The class names and methods come from the object model; the exact signatures are in the in-system documentation of each class. It requires an SAP system; not run here.

CLASS lcl_report DEFINITION FINAL.
  PUBLIC SECTION.
    METHODS format IMPORTING alv TYPE REF TO cl_salv_table.
ENDCLASS.

CLASS lcl_report IMPLEMENTATION.
  METHOD format.
    TRY.
        DATA(columns) = alv->get_columns( ).
        columns->set_optimize( abap_true ).
        columns->get_column( 'CLAIM_ID' )->set_medium_text( 'Claim' ).
        columns->get_column( 'EMPLOYEE' )->set_medium_text( 'Employee' ).
        columns->get_column( 'STATUS' )->set_visible( abap_false ).

        DATA(amount) = CAST cl_salv_column_table( columns->get_column( 'AMOUNT' ) ).
        amount->set_medium_text( 'Amount' ).
        amount->set_currency_column( 'CURRENCY' ).

        alv->get_sorts( )->add_sort( 'EMPLOYEE' ).
        alv->get_aggregations( )->add_aggregation( 'AMOUNT' ).
        alv->get_functions( )->set_all( abap_true ).

        DATA(display) = alv->get_display_settings( ).
        display->set_striped_pattern( abap_true ).
        display->set_list_header( 'Expense claims' ).
      CATCH cx_salv_error.
        " a column name that does not exist, or a column sorted twice
        RETURN.
    ENDTRY.
  ENDMETHOD.
ENDCLASS.

Call NEW lcl_report( )->format( alv ) between FACTORY and DISPLAY in the first listing. Putting the formatting in a method of its own keeps the report short and lets you reuse it. The user can still change all of this at run time through the toolbar and save the result as a layout; what you set in code is only the starting point.

Reacting to the user: events

A display-only grid is enough for many reports, but often the user should click a row to see more. ALV tells your program about such an action through an event of the object you get from get_event, the same event mechanism described in the page about events. You write a handler method in a class, register it with SET HANDLER, and ALV calls it when the user double-clicks. The handler receives the number of the clicked row and the name of the clicked column, which you use to read that row from your own internal table. Events for a link click on a column, which needs the column to be given the hotspot cell type, and for a toolbar function that you added follow the same pattern. It requires an SAP system; not run here.

CLASS lcl_handler DEFINITION FINAL.
  PUBLIC SECTION.
    METHODS on_double_click
      FOR EVENT double_click OF cl_salv_events_table
      IMPORTING row column.
ENDCLASS.

CLASS lcl_handler IMPLEMENTATION.
  METHOD on_double_click.
    MESSAGE |Row { row }, column { column }| TYPE 'I'.
  ENDMETHOD.
ENDCLASS.

Register it after the formatting step and before display:

DATA(handler) = NEW lcl_handler( ).
DATA(events) = alv->get_event( ).
SET HANDLER handler->on_double_click FOR events.

In a real report the handler would use row to read the row from the table with a table expression and open a details view, for example the claim items. Keep that logic in a method of a class that you can test with ABAP Unit, and let the handler only pass the row on.

ALV with IDA for large tables

Normal ALV needs the whole data set in an internal table before it starts, and it sorts and filters that table in the memory of the application server. For a table with millions of rows that is slow and expensive. ALV with IDA, the Integrated Data Access, turns this around. The class CL_SALV_GUI_TABLE_IDA receives the name of a database table or a CDS view, not rows, and ALV asks the database for just the page of rows the user is looking at. Sorting, filtering and grouping then run in the database. You write no SELECT and no internal table. The documented one-line call is the following; SFLIGHT is the flight demo table of an SAP demo system. It requires an SAP system; not run here.

cl_salv_gui_table_ida=>create( iv_table_name = 'SFLIGHT' )->fullscreen( )->display( ).

The help states that the large gains need an in-memory database such as SAP HANA: without one, ALV with IDA still displays the table, but the speed advantage does not appear for large data sets (it names about 100,000 records). A second help page describes the database awareness of IDA: the static method db_capabilities returns an object that tells you whether a given table or view is supported and whether it is wise to limit the number of rows. If you limit the rows with set_maximum_number_of_rows, the system falls back to ordinary internal table processing. IDA cannot show trees or hierarchical lists. The code pushdown ideas behind it are in the page ABAP for SAP HANA and Code Pushdown.

The reuse-library function modules, as legacy

You will meet this older style when you open a report from the 1990s or 2000s. The function module takes the name of the calling program, a structure name or a field catalog, and the table. The sketch below shows the shape of the call for the claims table ZEXP_CLAIM, with the table name as the structure that provides column descriptions. It requires an SAP system; not run here, and it is shown only so that you can read it.

DATA claims TYPE STANDARD TABLE OF zexp_claim WITH EMPTY KEY.

SELECT * FROM zexp_claim INTO TABLE @claims.

CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
  EXPORTING
    i_callback_program = sy-repid
    i_structure_name   = 'ZEXP_CLAIM'
  TABLES
    t_outtab           = claims
  EXCEPTIONS
    program_error      = 1
    OTHERS             = 2.
IF sy-subrc <> 0.
  MESSAGE 'ALV could not be shown' TYPE 'I'.
ENDIF.

When you rewrite such a report, replace the call with the object model rather than adding features to the function module. The data selection stays, the call turns into FACTORY and DISPLAY, and the field catalog disappears because the object model reads the column descriptions from the table type.

Where ALV fits today, and what replaces it

ALV belongs to the classic world: a program on the application server draws a table in SAP GUI. ABAP Cloud has no executable programs, no lists and no screens, and new applications there follow a different path. A CDS view entity defines the data, annotations such as the line item and selection field annotations describe how a table should look, a RAP business object or query provides the service, and a Fiori elements list report draws the table in the browser. The functions that ALV gives (sorting, filtering, columns, layouts, export) are provided by the standard table of that list report, so the developer describes the columns instead of coding a grid. The annotations are explained in CDS Annotations and Metadata Extensions; the RAP side comes with the RAP pages of this section.

A practical rule follows from this. In a classic system, a quick list for a user or an administrator is a fine use of ALV with the object model, and the datasets a user may filter are good candidates for ALV with IDA. A new business application that other users will open daily is better written as a CDS view with a Fiori elements list report, even in an on-premise system, because it also works in the browser and carries to ABAP Cloud.

Common mistakes

  • Starting a new program with REUSE_ALV_GRID_DISPLAY or CL_GUI_ALV_GRID because an old example shows it. The SAP help recommends the object model for new work.

  • Forgetting the TRY block. FACTORY and the getters raise exceptions, for example for a column name that does not exist. A name in lower case or with a typing error is the usual cause.

  • Expecting a heading from a local type. The headings come from the ABAP Dictionary data element; a field of a local type has none, so set the texts with the column methods.

  • Calling FACTORY a second time to refresh data. The table is passed to FACTORY the first time; use the refresh methods of the object afterwards.

  • Selecting millions of rows into an internal table to show them. Use ALV with IDA, or restrict the selection on the selection screen.

  • Putting business logic into the event handler. Keep the handler as thin as an event block and call a method of a class that has a unit test.

Summary

  • ALV, the SAP List Viewer, turns an internal table into an interactive table with sorting, filtering, totals, layouts and export, and almost every classic report ends in one.

  • Three generations exist: the reuse-library function modules and the ALV Grid Control are legacy, and the object model (CL_SALV_TABLE, CL_SALV_HIERSEQ_TABLE, CL_SALV_TREE) is what SAP recommends for new programs.

  • The smallest program is cl_salv_table=>factory followed by display; columns, sorts, totals, functions and events are set through sub-objects returned by get_... methods.

  • ALV with IDA shows a database table or CDS view without loading it into an internal table, and pays off on SAP HANA.

  • ALV needs SAP GUI and is not used in ABAP Cloud, where a CDS view, RAP and a Fiori elements list report take its place.

  • No listing on this page was run locally; each one requires an SAP system.