6.2    Messages

Messages allow you to communicate with users from your programs. They’re used mainly when the user has made an invalid entry on a screen. Messages are raised in the program code via the MESSAGE statement. There are different types of messages that can be displayed based on requirements. The message text can be maintained locally in the program or globally using a message class.

An example of a MESSAGE statement in use is as follows:

MESSAGE 'The input is invalid' TYPE 'E'.

The addition TYPE 'E' in the statement specifies it as an error message.

In this section, we’ll look at the various types of messages that can be displayed in an ABAP program. We’ll also explore the different ways messages can be displayed and how messages can be translated to other languages.

6.2.1    Types of Messages

Message processing depends on the message type specified in the MESSAGE statement and the program context in which the statement occurs.

The system behavior after a particular message type is sent is context-dependent. Table 6.1 lists all the possible types of messages that can be sent from ABAP programs. The type of message shown influences how the program behaves in a post-error situation and defines how the ABAP runtime should process the message. Always use the right message type for the condition, and keep the message informative enough for the user to understand the error.

It’s highly recommended to always validate the user inputs and show specific and relevant messages to the user to identify any issues with input values.

Message Type Meaning Notes
A Termination message The message appears in a dialog box and the program terminates. When the user has confirmed the message, the control returns to the next-highest area menu. This type of message should be limited to situations in which the error can’t be handled by the current task or when system-related errors occur.
E Error message Depending on the program context, an error dialog appears or the program terminates. Program execution won’t continue unless the error is corrected. These types of messages are used for input validations or when program execution can’t proceed due to an error.
I Information message The message appears in a dialog box. Once the user has confirmed the message, the program continues immediately after the MESSAGE statement. This type of message is used to show informative messages, such as successful database updates and so on.
S Status message The program continues normally after the MESSAGE statement, and the message is displayed in the status bar of the next screen. This type of message is used to display the status of any action.
W Warning message Depending on the program context, an error dialog appears or the program terminates. The user can press (Enter) to continue with program execution. This type of message is used to warn the user of missing information.
X Exit message No message is displayed, and the program terminates with a short dump. Program terminations with a short dump normally only occur when a runtime error occurs. Message type X allows you to force a program termination. The short dump contains the message ID.

Table 6.1    Types of Messages

The type of message is specified in the MESSAGE statement directly—for example:

MESSAGE 'The input was invalid' TYPE 'E'
MESSAGE 'Database update successful' TYPE 'I'
MESSAGE 'Key parameters are missing' TYPE 'W

6.2.2    Messages Using Text Symbols

One major disadvantage of using text literals to maintain a message in program code is that they can’t be reused in the program or customized for the user logon language. By using text symbols, we can reuse the messages within the program and maintain translations wherein the translated text can be automatically picked up by the system based on a user’s logon language.

To maintain text symbols, follow the menu path GoToText ElementsText Symbols in ABAP Editor, as shown in Figure 6.14.

Menu Path to Maintain Text Symbols in ABAP Program

Figure 6.14    Menu Path to Maintain Text Symbols in ABAP Program

On the Change Text Symbols screen (see Figure 6.15), you can assign a unique three-digit alphanumeric value to each text symbol that can then be called from the ABAP program.

Change Text Symbol Screen

Figure 6.15    Change Text Symbol Screen

The text symbol editor also has the option to maintain translations for each text via the menu path GoToTranslation from the Change Text Symbols screen, as shown in Figure 6.16.

Menu Path to Access Translation Editor

Figure 6.16    Menu Path to Access Translation Editor

To use a text symbol in a message, simply use text-nnn to call the text symbol. Here, nnn is the three-digit text symbol number—for example:

MESSAGE text-001 TYPE 'E'.

The system will automatically take care of pulling the respective translated texts to support multiple logon languages. If there’s no translation maintained for the given logon language, a blank message will be shown.

6.2.3    Messages Using Message Classes

Text symbols maintained in the program are local, so they can only be reused within the program. However, what if you need to share message texts across multiple related programs? Maintaining the same message texts and their translations in multiple programs can be redundant and a waste of time, because any changes to texts should also be manually maintained in each program. To maintain message texts globally, use a message class.

Global message texts are stored in table T100 and can be configured using Transaction SE91. When creating a new message class, it should be in a customer namespace, starting with Y or Z.

To create a new message class, open Transaction SE91. On the Message Maintenance initial screen, enter a unique name for your message class and click the Create button, as shown in Figure 6.17.

Message Maintenance Initial ScreenMessagesmaintenance

Figure 6.17    Message Maintenance Initial Screen

On the next screen, select the Messages tab to edit your messages. The system will prompt you to assign a package when navigating to the Messages tab, because a message class is a repository object. Message texts are similar to text symbols; each message text is assigned a unique three-digit alphanumeric value.

You can select the SelfExplanatory checkbox (see Figure 6.18) if the message explains itself sufficiently, or you can create long texts for each message to show more information about a message to the user. The long text is displayed when the user double-clicks the message (see Figure 6.19). The long text is maintained by clicking the Long Text button, as shown in Figure 6.18.

Maintaining Messages in Message ClassMessage classmaintaining messages

Figure 6.18    Maintaining Messages in Message Class

Long Text Displayed in Performance Assistant Window

Figure 6.19    Long Text Displayed in Performance Assistant Window

The syntax to display a message from the message class is a bit different than displaying it using a text symbol. Here, the message number is preceded by the type of message as opposed to using the TYPE addition. The message class name is supplied in parenthesis—for example:

MESSAGE e000(ZMSG_CLASS). 

In this statement, we’re displaying the message with the message number 000, maintained in the message class ZMSG_CLASS as an error message. The message type e precedes the message number 000. You can use other message types similarly; for example, w000 will display the message as a warning message.

The message class name can also be maintained in the REPORT statement for executable programs, using the MESSAGE-ID addition, instead of maintaining it in parenthesis for each message—for example:

REPORT  ZDEMO_MESSAGES MESSAGE-ID zmsg_class.

MESSAGE E000.

You can maintain translations for each message via the menu path GoToTranslation from the Message Maintenance screen.

6.2.4    Dynamic Messages

You may have noticed that Figure 6.19 contains some dynamic elements. The message shows the username and the program name as part of the message text. We can use an ampersand (&) as a placeholder in message text, and it can be replaced dynamically in the program via the WITH addition to the MESSAGE keyword. You can use up to four placeholders in the message text.

Figure 6.20 shows the message with two placeholders that will be replaced with a value supplied in the MESSAGE statement via the WITH addition.

The message can be sent using the syntax MESSAGE s000(ZMSG_CLASS) WITH sy-uname sy-repid. Here, sy-uname is a system field that stores the username, and sy-repid is the system field that stores the current program name. The value of the variable sy-uname will replace the first placeholder in the message, and sy-repid will replace the second placeholder. The fields are inserted sequentially into the message text in place of the placeholders.

Using Placeholders in Message TextPlaceholdersMessagesplaceholders

Figure 6.20    Using Placeholders in Message Text

Alternately, you can use the placeholders with position numbers such as &1, &2, &3, and &4 in the message to specify where the value is inserted in the message. Placeholders that are not supplied with a value via the WITH addition will be ignored.

Placeholders can also be used in the long text of a message by maintaining &V1& to &V4& as the placeholders. When the long text is called, the values supplied with the message will replace the placeholders in the long text.

6.2.5    Translation

Because the SAP system supports multiple logon languages, it’s recommended to create translations for all texts maintained in the program (in case users with different logon languages access your program).

The translation editor can be accessed by following the menu path GoToTranslation from ABAP Editor or from text maintenance screens like those for the message class or the text symbols. After selecting the menu path GoToTranslation, the system presents a dialog box to specify the target language into which the text needs to be translated, as shown in Figure 6.21.

Once the target language is selected, you can maintain translations manually for each text individually and click Save, as shown in Figure 6.22.

Target Language Selection

Figure 6.21    Target Language Selection

Maintain Translation Screen

Figure 6.22    Maintain Translation Screen

Translations can be created for the program title, selection texts, text symbols, and list headings.