Messages

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

A program often has to tell someone that something went wrong, that something worked, or that a question needs an answer. ABAP does this with messages: short texts that are kept outside the program in a message class, so that they can be translated, reused and changed without touching the code. A newcomer meets them the first time a transaction shows a red line at the bottom of the screen, and a developer meets them every time a check fails. After reading this page you can write a message with placeholders, choose the right message type for the place it is raised, read the system fields that ABAP fills in, turn a message into plain text without showing it, and keep messages for later in an application log. The page is written against Standard ABAP release 8.16 and ABAP for Cloud Development release 9.20 (2608). The MESSAGE statement and message classes exist in both modes, but screens and reports with a status bar are classic Standard ABAP (on premise and Private Edition); in ABAP Cloud, which is the only mode in the Public Edition, messages mostly travel as exception texts and as RAP messages instead of as status-bar lines.

Before you read

Key terms

  • Message class: a named collection of numbered message texts, kept in the system and not in the program.

  • Message type: a one-letter code (A, E, I, S, W or X) that decides how a message is shown and whether the program goes on.

  • Placeholder: a marker such as &1 in a message text that is replaced by a value when the message is raised.

  • System field: a field of the structure sy that ABAP fills automatically, for example the message fields sy-msgid and sy-msgno.

  • Status bar: the line at the bottom of a classic screen where success, warning and error messages appear.

  • Application log: a stored list of messages, written by a program and read later, for example after a background job.

Message classes and numbers

A message text is identified by three things: the message class, a three-digit number, and the language. A class is a named group, for example ZEXP for the Expense claims application, and each number inside it holds one text such as "Claim &1 has no items". A class is created and changed with the message maintenance tool in ADT or in the classic transaction SE91. The short texts are stored per language in the database table T100; a class name has at most 20 characters, a number at most three digits, and a short text at most 73 characters. In the Expense claims example, message 001 of ZEXP could read Claim &1 has no items and message 002 Amount &1 &2 exceeds the limit of &3 &4.

The text may contain up to four placeholders, written &1 to &4. The first value named after WITH replaces &1, the second replaces &2, and so on. A bare & also works as a placeholder, but only the numbered form should be used in a text that is translated, because the word order of a sentence may change in another language. A literal & is written && in the short text. Texts live in the message class and are translated there; the translation tools are described in Text Elements and Translation.

The MESSAGE statement

The statement MESSAGE raises a message. The usual short form names the type letter, the number and the class in one token, then the values:

" requires an SAP system; not run here
DATA claim_id TYPE c LENGTH 10 VALUE 'EXP0000042'.

MESSAGE e001(zexp) WITH claim_id.

The token e001(zexp) means type E, number 001, class ZEXP. The same message can be written with explicit parts, which is useful when type, number or class are held in variables:

" requires an SAP system; not run here
MESSAGE ID 'ZEXP' TYPE 'E' NUMBER '001' WITH claim_id.

In the short form without a class in parentheses, the class is named once for the whole program with the MESSAGE-ID addition of the statement that introduces the program, for example REPORT, and then MESSAGE e001 WITH claim_id is enough. A class in parentheses overrides that addition. This page keeps the class in the statement so that each line stands alone. In the explicit form, the number is held in a data object of type n with length 3, and an invalid type letter raises an exception that cannot be caught.

The six message types

The letter after MESSAGE is the message type. The valid types are A, E, I, S, W and X, which stand for termination (abort) message, error message, information message, status message, warning and exit message. Without the additions RAISING or INTO, the type decides how the message is shown and how the program goes on, and the exact behaviour depends on the context in which the statement runs.

Type Name Effect in a classic dialog or report

S

Status

The program carries on after the statement. The text appears by default in the status bar of the next screen.

I

Information

The text appears in a dialog box. After the user closes it, the program carries on after the statement.

W

Warning

In input checks the text appears in the status bar and the fields are ready for input again; if the user confirms with Enter without a new value, the program carries on. In reporting and list events a warning is turned into an error.

E

Error

In input checks the processing step is interrupted and the fields are ready for input again. In reporting events such as START-OF-SELECTION the program ends and an empty screen with the text in the status bar is shown.

A

Termination

The text appears in a dialog box. After it is closed, the program ends, all internal sessions of the call sequence are deleted and a database rollback is performed.

X

Exit

The program ends with the runtime error MESSAGE_TYPE_X and a database rollback; the short dump shows the message.

The same message therefore behaves differently in different places. In background processing nothing is shown on a screen: all messages are written to the job log. Types S, I and W are written and the program continues. Types E and A are written, and the background job is terminated unless the message is handled with the classic exception error_message; type A also rolls back the database, type E does not. Type X causes a runtime error with a rollback. The Keyword Documentation lists the behaviour per processing block in a table, and also for updates, procedures, RFC and other contexts.

flowchart TD M["MESSAGE type"] --> S["S: status bar, program continues"] M --> I["I: dialog box, program continues"] M --> W["W: status bar, input checked again"] M --> E["E: status bar, current step stops"] M --> A["A: dialog box, program ends"] M --> X["X: runtime error, short dump"]

The Keyword Documentation advises using MESSAGE without INTO or RAISING only in the presentation layer of an application, never in the application logic, with the exception of type X; this is also why logic classes raise exceptions instead.

Classic dialogs are not supported in ABAP Cloud, so the type letter is not relevant there for the display. What remains is the form MESSAGE ... INTO ..., described next, and messages carried by exceptions.

The system fields and MESSAGE INTO

Whenever MESSAGE runs, ABAP fills system fields: sy-msgid (class), sy-msgty (type), sy-msgno (number) and sy-msgv1 to sy-msgv4 (the four values). Function modules and methods can leave a message in these fields for their caller. To turn a message into text without showing it, use the INTO addition:

" requires an SAP system; not run here
DATA(claim_id) = 'EXP0000042'.

MESSAGE e001(zexp) WITH claim_id INTO DATA(text).

" text now holds the finished sentence; sy-msgid, sy-msgty, sy-msgno and sy-msgv1 are set.

With INTO, the program flow is not interrupted and the short text goes into the target field; the system fields are still set. A way to collect messages in a list is therefore to raise a message with INTO, then read the sy-msg* fields to store the class, number and values, or use the text directly. The same fields can be turned back into text later with a MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4 INTO text statement. The INTO variant is also possible in ABAP for Cloud Development.

Messages in exceptions and in ABAP Cloud

A modern way to carry a message is an exception class. A class that implements the interface IF_T100_MESSAGE holds a structure T100KEY that names a message: its class in MSGID, its number in MSGNO, and in ATTR1 to ATTR4 the names of class attributes whose content fills the placeholders. In a global exception class the methods get_text( ) and get_longtext( ) are already provided by CX_ROOT, so the caller catches the exception and either shows the text or passes it on. The interface IF_T100_DYN_MSG includes IF_T100_MESSAGE and also allows the additions MESSAGE and WITH on RAISE EXCEPTION, so that any message can be attached to an exception without a constant per text. Messages as exception texts are the usual route in ABAP Cloud. The details of the RAP message objects belong to the RAP pages of this section.

The application log

A message shown in the status bar is gone after the next screen. A background job, an interface or a posting run needs messages that can be read afterwards. The application log, also called the Business Application Log or BAL, stores them under an application log object and a subobject. The objects are defined in transaction SLG0 and the logs are analysed in transaction SLG1; expired logs are deleted with SLG2. In classic code, a log is opened with the function module BAL_LOG_CREATE, messages are added with BAL_LOG_MSG_ADD and BAL_LOG_EXCEPTION_ADD, and BAL_DSP_LOG_DISPLAY shows them. In ABAP Cloud, the class CL_BALI_LOG creates a log, a header with object, subobject and external ID is set with CL_BALI_HEADER_SETTER, items such as free texts or exceptions are added, and CL_BALI_LOG_DB saves it; the class and its helpers are listed in the cheat sheet of released classes. Which objects may be used in ABAP Cloud is explained in ABAP Language Versions and Released APIs. The idea is the same in both: a log object is created, messages are added to it, and the log is saved. Reading a log in the released classes needs the authorization object S_APPL_LOG.

Older material you may meet

Older code differs from the current style in three ways. First, every variable had to be declared before the statement that fills it, so MESSAGE ... INTO appeared with a separate DATA text TYPE string line; today MESSAGE ... INTO DATA(text) declares the target in the same statement. Second, older code often read the sy-msg* fields by hand and passed them on; newer code throws an exception that carries the message. Third, the variant MESSAGE 'text' TYPE 'E' with a literal text still exists, but the Keyword Documentation says it should be used only rarely: the system fields are then filled in a non-specific way, the additions WITH and INTO are not allowed, and the language-independent access to the message text is lost. Prefer message classes.

Summary

  • A message class holds numbered, translatable texts; placeholders &1 to &4 are filled from the values after WITH.

  • The message types are S, I, W, E, A and X; their effect depends on the context, and classic dialogs are not supported in ABAP Cloud.

  • MESSAGE ... INTO returns the text without showing it and sets sy-msgid, sy-msgty, sy-msgno and sy-msgv1 to sy-msgv4.

  • Exception classes with IF_T100_MESSAGE or IF_T100_DYN_MSG carry messages as exception texts.

  • The application log stores messages for later reading; classic code uses the BAL_* function modules and SLG1, ABAP Cloud uses CL_BALI_* classes.