Code examples · Modern ABAP · Intermediate
Classic ALV report with REUSE_ALV_GRID_DISPLAY: field catalog and double click
The problem
Sales needs the usual WRICEF report: sales orders in a grid users can sort, filter and export.
How we solve it
- 1Read the dataSELECT
- 2Field catalogcolumns
- 3Show the gridREUSE_ALV_GRID_DISPLAY
- 4Handle the clickcallback form
The result
A grid with zebra rows, a total at the bottom of the net value column, and a clickable order number.
SAP objects in play
1. The requirement
Sales needs the usual WRICEF report: sales orders in a grid users can sort, filter and export. The net value must be summed, and a click on an order number must open that order in transaction VA03.
2. How we design it
The function module REUSE_ALV_GRID_DISPLAY draws the grid and gives sorting, filtering, export and layouts. We give it the data table, a field catalog that describes each column and a layout. For the click, we name a callback form; ALV calls it with the clicked field and value, and we open the order from there.
Why we decided it this way
- 1Field catalog built in codeThe data uses a local type, so ALV cannot read column properties from a dictionary structure.
- 2Reference to the dictionary fieldREF_TABNAME and REF_FIELDNAME give each column its type, length and header text, and let currency amounts be shown correctly.
- 3Callback form for user actionsALV hands control back at a click. The form decides what to do.
Before you start
- An on-premise SAP system with sales orders (table VBAK), SAP GUI and authorization to display orders (VA03).
3. The code, step by step
Step 1 of 4: Read the data
A table with the columns we want to show.
" Types and select
REPORT zdemo_alv_classic.
TYPES: BEGIN OF ty_out,
vbeln TYPE vbak-vbeln,
erdat TYPE vbak-erdat,
kunnr TYPE vbak-kunnr,
netwr TYPE vbak-netwr,
waerk TYPE vbak-waerk,
END OF ty_out.
DATA: gt_out TYPE STANDARD TABLE OF ty_out WITH EMPTY KEY,
gt_fcat TYPE slis_t_fieldcat_alv,
gv_vbeln TYPE vbak-vbeln.
SELECT-OPTIONS s_vbeln FOR gv_vbeln.
START-OF-SELECTION.
SELECT vbeln, erdat, kunnr, netwr, waerk
FROM vbak
WHERE vbeln IN @s_vbeln
ORDER BY vbeln
INTO TABLE @gt_out
UP TO 200 ROWS.
Part by part (plain text)
- Types and select. A local type with five fields, an order range on the selection screen, and a select that reads at most 200 orders.
Open in the portal
The SAP objects named in this code are linked in the code above and listed here. Each opens its page in the SAP Library.
SAP tables
Transaction codes
Function modules
If it does not work
4. The final outcome
A grid with zebra rows, a total at the bottom of the net value column, and a clickable order number.
What you should see
Run the report: up to 200 sales orders appear in a grid with the standard toolbar. The net value column can be summed. A click on an order number opens that order in VA03 (display).
Try it and check the result
- Run with F8 and press Execute. The grid shows sales orders with striped rows.
- Look at the bottom of the net value column: the total is shown there because DO_SUM is set. Use the toolbar to sort or filter and watch the total change.
- Click an order number: transaction VA03 opens that order. Go back with F3 to return to the grid.
- Misspell a field name in the catalog, for example 'VBELX', and run: the column is missing or the list does not display properly, which shows why you test the catalog.
Common mistakes
- Field names in the catalog must match the fields of the output table exactly, in capital letters.
- Without CFIELDNAME or a reference, currency amounts can show wrong decimals. Always link an amount to its currency.
- The callback form must be in the same program that is named in I_CALLBACK_PROGRAM.
- REUSE_ALV_GRID_DISPLAY is the classic function module. New reports often use the class CL_SALV_TABLE (see the CL_SALV_TABLE example), but you will meet this module in most existing programs.