Classic Reports and Event Blocks
|
This content was generated with the assistance of AI and should be verified against the official documentation before being relied on in production. |
For decades the typical ABAP program was a report: you start it, it may ask you a few questions on a form, it reads data
and it prints a list on the screen. A report is not written as one long script. It is written as a set of short blocks,
and each block runs when the runtime reaches a certain moment, such as "the program has just been loaded", "the user
pressed Execute" or "the user double-clicked a line of the list". Those blocks are called event blocks, and knowing which
block runs when is the key to reading any older ABAP report and to fixing one. After reading this page you can name the
event blocks of a report, say in which order the runtime calls them, decide what belongs in each one and avoid the
classic traps (a declaration inside a block, a wrongly placed prefill, a STOP in the wrong place). The page is written
against Standard ABAP release 8.16. Everything here belongs to classic ABAP, in S/4HANA on premise and Private Edition and
in older systems alike. Executable programs, selection screens and classic lists are not part of ABAP for Cloud
Development (release 9.20, 2608), so none of this applies in ABAP Cloud. These events are also a
different thing from the object events of the class model described in Events.
Before you read
-
Program Types and Structure: what an executable program is and what a processing block is.
-
Data Types and Data Objects:
DATA, the system fieldsy-ucommand the othersy-fields. -
ABAP SQL Basics: the
SELECTstatement of the running example. -
Selection screens, classic lists and ALV have pages of their own in this section (Selection Screens and Variants, Classic Lists and Interactive Reports, ALV); this page only shows where they fit into the event flow.
Key terms
-
Event block: a block of statements that the runtime runs when one particular event occurs, introduced by an event keyword and ended by the next block.
-
Reporting event: one of the events of an executable program from loading to the end of data processing, for example
INITIALIZATIONorSTART-OF-SELECTION. -
Selection screen: the form that a report shows first so that the user can enter values to restrict the data.
-
Classic list: the page-oriented output that
WRITEcreates and that the user sees after the report has run. -
List event: an event that occurs while a list is built or after the user acts on a displayed list, for example
TOP-OF-PAGEorAT LINE-SELECTION. -
GUI status: the menu bar, toolbar and function keys of a screen or list, which decide which function codes the user can trigger.
What a classic report is
An executable program starts with the statement REPORT and is started either with the statement SUBMIT or through a
transaction code. The Keyword Documentation says that in general every executable program is started implicitly with
SUBMIT; only a start through a regular transaction code or an external call of its procedures is not tied to a SUBMIT.
The classic shape is: a selection screen asks for values, the program reads data, and a list shows the result. A report
does not run top to bottom. Instead, the ABAP runtime framework raises events one after another, and whatever event block
you wrote for an event is executed. If you wrote none for an event, nothing happens at that moment.
An event block starts with an event keyword and has no closing statement: it ends where the next processing block starts, so it is good style to close it with a comment line. Three rules matter every day.
-
No local data. Declarations inside an event block are not local. They become global data of the program and are visible in all later blocks. The two exceptions are
AT SELECTION-SCREENandGET, which are implemented internally as procedures and may have local data. Because of this rule, keep declarations at the top of the program. -
Several blocks of one event. Apart from
AT SELECTION-SCREENandGET, an event block may appear more than once, and all blocks of one event run in the order in which they occur in the source. The extended program check warns about explicit duplicates, so do this only for a reason. -
A new line at the end. After each event block the statement
NEW-LINEis executed implicitly, so list output of the next block starts on a new line.
The recommendation of the documentation is to keep very little in event blocks and to call methods instead. This page follows it: every example block is one call, and the logic sits in a local class.
The order of events
The next diagram shows what the runtime does when a report is started with the statement SUBMIT, which is also what
happens when a user starts it from a transaction code. It follows the order given in the Keyword Documentation page "Flow
of an Executable Program After SUBMIT".
Read the diagram from the top. The program is loaded and its constructor LOAD-OF-PROGRAM runs. The start values given
with DEFAULT in PARAMETERS and SELECT-OPTIONS are passed to the fields, then INITIALIZATION runs. Next the runtime
calls the selection screen, and AT SELECTION-SCREEN OUTPUT runs each time the screen is about to be sent. When the user
chooses Execute, the selection screen events of the input check run, and then START-OF-SELECTION. After it comes
END-OF-SELECTION, and then the runtime calls the basic list: if it is empty the program ends, otherwise it is shown.
While the list is shown, TOP-OF-PAGE runs at the start of each page, and a user action on the list raises a list event.
The Keyword Documentation adds a detail that surprises newcomers: if a selection screen was displayed, the program is
called again after the list is left, and everything starts over at LOAD-OF-PROGRAM, until the user chooses Back, Exit
or Cancel on the selection screen. That is why a report that sets something only in INITIALIZATION seems to forget it
on the second round.
The event blocks one by one
LOAD-OF-PROGRAM
LOAD-OF-PROGRAM is the program constructor. It runs once whenever the program is loaded into an internal session, which
happens for each SUBMIT and for each transaction start. It exists in executable programs, module pools, function pools
and subroutine pools, but not in class pools: a global class uses its static constructor instead. Its job is small: to
initialize global data. The block has to run to its end. Statements that leave it, such as RETURN, raise the runtime
error SYSTEM_LOAD_OF_PROGRAM_FAILED, and the documentation advises against LEAVE PROGRAM and against messages that
wait for the user, because loading a program is the wrong moment for a dialog. Most reports do not need this block.
INITIALIZATION
INITIALIZATION runs directly after LOAD-OF-PROGRAM and before the selection screen is processed. Its classic use is to
prefill input fields: assigning a value to a parameter here puts that value on the screen, for example
p_status = 'S'. Remember that the block runs again when the program is called again after a list, but the new
assignments no longer have an effect, because the fields are filled with the user’s earlier entries. If the screen must be
prepared on every display, use AT SELECTION-SCREEN OUTPUT.
AT SELECTION-SCREEN
AT SELECTION-SCREEN is a family of events, written with an addition for the screen element you want to react to. The
information about which selection screen raised the event is in sy-dynnr. The most important variants are:
| Statement | When it runs |
|---|---|
|
Each time the selection screen is about to be sent. Assigning values to the parameters here overwrites earlier user
input, and |
|
After the user has entered values and an action is chosen, for one parameter or one selection criterion. This is the place to check one field. |
|
The same for all fields of a block, to check fields against each other. |
|
Without addition: after all input fields were checked, the last check before the report starts. |
|
When the user asks for the input help (F4) of a field, to supply your own list of values. |
|
When the user chooses Back, Exit or Cancel. It is the only selection screen event raised for those functions. |
How to define the fields themselves (PARAMETERS, SELECT-OPTIONS, blocks, variants) is the topic of the page
Selection Screens and Variants. What matters here is what a message does in these blocks. According to the Keyword
Documentation, a message of type E (error) in AT SELECTION-SCREEN interrupts the processing and returns to the same
screen without AT SELECTION-SCREEN OUTPUT: the text appears in the status bar and the fields named in the event block’s
addition are ready for input again. So a check such as "the status must be S, A or R" is written as a type E message in
AT SELECTION-SCREEN ON p_status, and the user stays on the form. The same type E message in START-OF-SELECTION does
the opposite: the program ends and an empty screen is left. Move every check to the selection screen events. The
function code of the button that the user pressed on a selection screen is in the component ucomm of the structure
sscrfields (declare it with TABLES sscrfields), and the documentation advises against reading sy-ucomm there.
START-OF-SELECTION
START-OF-SELECTION is the standard processing block. It runs after the selection screen when the user executes the
report. Statements that stand loose in the program (not declarations, and before the first explicit processing block) are
assigned to an implicit START-OF-SELECTION block, and so are all functional statements of a program without any explicit
block. That is why tiny reports work without writing the keyword. The recommended style is an explicit block that calls a
method, so that the real work is in a class.
END-OF-SELECTION
END-OF-SELECTION is marked obsolete in the Keyword Documentation and is intended only for programs linked to a logical
database, an old shared data-reading program. There it runs when the logical database has finished and lets you process
everything it delivered in one piece. In a program without a logical database it runs directly after
START-OF-SELECTION, and the documentation says there is no need to implement it. Older books and old reports use it
as the place where a program collects its results and prints them; in new code, do the printing in the same method that
read the data. The statement STOP jumps to this event: the blocks AT SELECTION-SCREEN (without addition) and
START-OF-SELECTION (and GET) are left at once and the runtime raises END-OF-SELECTION. STOP is forbidden in
methods, so it can only be used directly in an event block.
TOP-OF-PAGE
TOP-OF-PAGE is raised when a page of the list is begun, just before the first line of that page is written. Whatever you
write in the block lands below the standard page header, as the header of the page. The statement NEW-PAGE is ignored
inside it. With the addition DURING LINE-SELECTION the block applies to details lists instead of the basic list, and
system fields such as sy-lsind (the number of the current list level) tell the levels apart. Its counterpart for the
page foot is END-OF-PAGE.
AT LINE-SELECTION
AT LINE-SELECTION runs when the list is on screen, the cursor is on a line and the function code PICK is chosen. By
writing this block you automatically link the function key F2, and so the double click of the mouse, to PICK. This is
the event of interactive reports: the block typically reads the values that HIDE stored for the chosen line and writes a
details list (the page Classic Lists and Interactive Reports shows this in full).
AT USER-COMMAND
AT USER-COMMAND runs when the user chooses a function code that you defined in the GUI status of the list. The code is
in sy-ucomm when the block starts. The function codes PICK and PFnn do not raise it (they raise AT LINE-SELECTION
and the obsolete AT PFnn), codes that start with % are system functions, and the list processor handles scrolling
and printing codes such as BACK, P+ and PRI itself. A program can also raise a list event itself with the statement
SET USER-COMMAND.
Example: a claim report skeleton
The skeleton below lists claims of the Expense claims example, one line each. It has a selection screen with one
parameter that is prefilled and checked, a list with a page header, a double click that opens a details list and a
function code that counts the lines. All event blocks only call methods of the local class. The program reads the table
ZEXP_CLAIM and needs a GUI status named LIST with a function code COUNT. This listing requires an SAP system; not
run here (the local playground does not offer the dictionary table, the selection screen, the GUI status or the events
LOAD-OF-PROGRAM, INITIALIZATION, AT SELECTION-SCREEN, TOP-OF-PAGE, AT LINE-SELECTION and AT USER-COMMAND).
REPORT zexp_claim_report.
PARAMETERS p_status TYPE zexp_claim-status.
TYPES: BEGIN OF ty_claim,
claim_id TYPE zexp_claim-claim_id,
status TYPE zexp_claim-status,
total_amount TYPE zexp_claim-total_amount,
currency_code TYPE zexp_claim-currency_code,
END OF ty_claim.
DATA gv_claim_id TYPE zexp_claim-claim_id.
DATA gv_lines TYPE i.
CLASS lcl_report DEFINITION FINAL.
PUBLIC SECTION.
CLASS-METHODS main IMPORTING status TYPE zexp_claim-status.
CLASS-METHODS header.
CLASS-METHODS detail.
CLASS-METHODS count.
ENDCLASS.
CLASS lcl_report IMPLEMENTATION.
METHOD main.
SELECT claim_id, status, total_amount, currency_code
FROM zexp_claim
WHERE status = @status
ORDER BY claim_id
INTO TABLE @DATA(claims).
gv_lines = lines( claims ).
SET PF-STATUS 'LIST'.
LOOP AT claims INTO DATA(claim).
WRITE: / claim-claim_id, claim-status,
claim-total_amount CURRENCY claim-currency_code,
claim-currency_code.
gv_claim_id = claim-claim_id.
HIDE gv_claim_id.
ENDLOOP.
ENDMETHOD.
METHOD header.
WRITE: / 'Claim', 12 'St', 16 'Amount'.
ULINE.
ENDMETHOD.
METHOD detail.
WRITE: / 'Details of claim', gv_claim_id.
ENDMETHOD.
METHOD count.
WRITE: / 'Lines in the basic list:', gv_lines.
ENDMETHOD.
ENDCLASS.
LOAD-OF-PROGRAM.
CLEAR gv_lines.
INITIALIZATION.
p_status = 'S'.
AT SELECTION-SCREEN ON p_status.
IF p_status <> 'S' AND p_status <> 'A' AND p_status <> 'R'.
MESSAGE 'Status must be S, A or R' TYPE 'E'.
ENDIF.
START-OF-SELECTION.
lcl_report=>main( p_status ).
TOP-OF-PAGE.
lcl_report=>header( ).
AT LINE-SELECTION.
lcl_report=>detail( ).
AT USER-COMMAND.
CASE sy-ucomm.
WHEN 'COUNT'.
lcl_report=>count( ).
ENDCASE.
Read it in the order of the diagram. LOAD-OF-PROGRAM clears the line counter, INITIALIZATION puts S (submitted) into
the field of the selection screen, and AT SELECTION-SCREEN ON p_status rejects any other status with a type E message,
which keeps the user on the form. After Execute, START-OF-SELECTION calls main, which reads the claims and writes
one line per claim. HIDE gv_claim_id stores the claim number together with the line that was just written. When the
basic list is shown, TOP-OF-PAGE writes the heading above the first line of the page. A double click on a line raises
AT LINE-SELECTION: the runtime has restored the hidden value of that line into gv_claim_id, so detail knows which
claim was chosen. A user who chooses the function COUNT raises AT USER-COMMAND, where sy-ucomm holds COUNT.
No END-OF-SELECTION is needed, because the program has no logical database.
Example: the part that runs in the playground
The playground can run START-OF-SELECTION, END-OF-SELECTION, a PARAMETERS default and WRITE, which is enough to
see how a report is split into a start value, a processing block and a final block. It cannot run the other event
blocks (it stops with an error or skips the block), and a STOP statement does not behave like real ABAP
there, so that statement is not part of the listing. The claims come from an internal table, and p_min has the start
value 100, the same effect as a DEFAULT in a real selection screen. The listing ran in the local playground (open-abap,
a community transpiler that runs a subset of ABAP in Node.js).
REPORT zclaim_events.
PARAMETERS p_min TYPE i DEFAULT 100.
TYPES: BEGIN OF ty_claim,
claim_id TYPE string,
amount TYPE i,
END OF ty_claim.
TYPES ty_claims TYPE STANDARD TABLE OF ty_claim WITH EMPTY KEY.
CLASS lcl_report DEFINITION.
PUBLIC SECTION.
CLASS-METHODS main IMPORTING min_amount TYPE i
RETURNING VALUE(shown) TYPE i.
CLASS-METHODS summary.
PRIVATE SECTION.
CLASS-DATA total TYPE i.
ENDCLASS.
CLASS lcl_report IMPLEMENTATION.
METHOD main.
DATA(claims) = VALUE ty_claims(
( claim_id = `C-1001` amount = 120 )
( claim_id = `C-1002` amount = 80 )
( claim_id = `C-1003` amount = 450 ) ).
LOOP AT claims INTO DATA(claim) WHERE amount >= min_amount.
shown = shown + 1.
WRITE / |{ claim-claim_id } { claim-amount }|.
ENDLOOP.
total = shown.
ENDMETHOD.
METHOD summary.
WRITE / |end-of-selection: { total } claims shown|.
ENDMETHOD.
ENDCLASS.
START-OF-SELECTION.
WRITE / 'start-of-selection'.
IF lcl_report=>main( p_min ) = 0.
WRITE / 'nothing to show'.
ENDIF.
END-OF-SELECTION.
lcl_report=>summary( ).
Output with the start value 100:
start-of-selection C-1001 120 C-1003 450 end-of-selection: 2 claims shown
Output after changing the start value to 1000:
start-of-selection nothing to show end-of-selection: 0 claims shown
Source: ABAP Keyword Documentation: START-OF-SELECTION and ABAP Keyword Documentation: END-OF-SELECTION
Both outputs show the order: the processing block runs first and the final block runs after it, even when there was
nothing to show. END-OF-SELECTION printed in both cases because, without a logical database, it is raised directly
after START-OF-SELECTION. In real ABAP the lines of WRITE form the basic list, which the user sees after the last
event block.
Common mistakes
-
Declaring data inside an event block and expecting it to be local. It is global. Declare at the top, and keep each block to a method call.
-
Prefilling a parameter in
INITIALIZATIONand expecting it to be there every time. On the second round the field holds what the user entered before. UseAT SELECTION-SCREEN OUTPUTfor values that must be set on every display. -
Checking input in
START-OF-SELECTION. A type E message there ends the program with an empty screen. Check inAT SELECTION-SCREEN, where the user stays on the form. -
Writing
STOPin a method. It is forbidden there. Put it in the event block, or better, return a value and decide in the block. -
Leaving
LOAD-OF-PROGRAMearly.RETURNraises the runtime errorSYSTEM_LOAD_OF_PROGRAM_FAILED, andLEAVE PROGRAMand messages that wait for the user are discouraged. -
Relying on
END-OF-SELECTIONorAT PFnnin new code. Both are marked obsolete in the Keyword Documentation. Old reports contain them, and a book written for older releases may presentEND-OF-SELECTIONas the normal place for output. Treat that advice as outdated: print where you read, and useAT USER-COMMANDwith your own function codes. -
Several explicit blocks of one event. They all run, in source order, but the extended program check warns, and the order is easy to misread.
Where this fits today
A classic report is the oldest way to give users a list. ABAP Cloud has no executable programs, so new development there
uses classes, RAP business objects and Fiori elements apps instead. In classic systems the hand-written WRITE list is
often replaced by an ALV list (page ALV). You still meet reports daily in maintenance work and in background jobs, because a
report can be scheduled and run without a user. When you change one, keep its event flow in your head, change as little
as possible and move the new logic into a class with a unit test.
Summary
-
A classic report is an executable program that the runtime drives through events; you write an event block for each moment you want to react to.
-
The order is
LOAD-OF-PROGRAM,INITIALIZATION, the selection screen with itsAT SELECTION-SCREENevents,START-OF-SELECTION,END-OF-SELECTION, then the list withTOP-OF-PAGEand the list eventsAT LINE-SELECTIONandAT USER-COMMAND. -
Data declared in an event block is global, and the recommended style is one method call per block.
-
Check user input in
AT SELECTION-SCREEN, where a type E message keeps the user on the form. -
END-OF-SELECTIONis obsolete outside logical databases,STOPis allowed only in event blocks, and none of these statements are available in ABAP Cloud.