While using mobile phone and websites, you might have seen several messages popping up like security message in Mobile phones regarding photo access or antivirus message in PC regarding virus detection. All these messages can be a success, information or error message. In similar way we can showcase SAP message in ABAP programs too.

Definition

A Message in ABAP is used to display a text message either stored in table T100 or any text [can be in a message class] where type of the message can be a status, warning, error and etc. A message in ABAP can also be used to raise an exception.

SAP Message Class

Message class is a class which holds a list of messages with message id. It is created using transaction code SE91. This helps to maintain all messages related to a project/program all together.

Syntax

MESSAGE { msg | text

| tn

| tn(id)

| { ID mid TYPE mtype NUMBER num }

| {oref TYPE mtype}

| {text TYPE mtype} }

{ { [DISPLAY LIKE dtype] [RAISING exception] }

| [INTO text] }

[WITH dobj1 … dobj4].

Syntax Explanation

The above syntax is explained below:

Syntax Explanation Example
msg | text
or, {text TYPE mtype}
Here msg is taken from class T100 while text is a string. MESSAGE ‘Success Message’ TYPE ‘I’.
tn It needs a message class. Here t is a single character and n is three digit number; both are written together. These particular tn will represent a text that is stored in message class. Here message class name is written at the starting of report. REPORT ztest MESSAGE-ID zmessageclass.

MESSAGE s001.

 

–>  Here s001 is a message having its text stored in zmessageclass.

tn(id) Like above t and n means same. Here id is the message class name and it is written just after the message REPORT ztest.

MESSAGE s001 (zmessageclass).

{ ID mid TYPE mtype NUMBER num } Here mid means message class, mtype means type of message (explained later) and num is a message number (a number of length three) DATA: m_id   TYPE sy-msgid VALUE ‘ zmessageclass ‘,

m_type TYPE sy-msgty VALUE ‘I’, m_num   TYPE sy-msgno VALUE ‘001’.

 

MESSAGE ID m_id TYPE m_type NUMBER m_num.

{oref TYPE mtype} Here oref is object reference variable which points to an object whose class implements the system interface IF_T100_MESSAGE. These are mainly used when we deal with local classes inside a report. Check out the example at given link.
[DISPLAY LIKE dtype] When Display like is used, then instead of type of message the icon will be as per Display Like MESSAGE ‘Success Message’ TYPE ‘I’ DISPLAY LIKE ‘E’.

*** Note: Above message is informative message but will be displayed as error message.

[RAISING exception] It raises a non class based exception and only sends a message if the exception was not handled. It can be handled via sy-subrc. CLASS-METHODS msg1 EXCEPTIONS excp1.

METHOD msg1.

MESSAGE ‘Exception msg in a Method’ TYPE ‘I’ RAISING excp1.

ENDMETHOD.

[INTO text] This type of message does not impact on program flow and it does not matter what is the type of message. It just assigns the value of Message into a user defined field [here text]. MESSAGE i001 INTO DATA(newText).

 

***Note: Here newText is created dynamically and holds the value of i001 in it. This value can be used later in the same program.

[WITH dobj1 … dobj4] This is used to add static text in place of ‘&’.

Suppose, we are bring text from table T100 and the text is “I am a &&”.

Then we can put our values in place of & & according to the situation.

MESSAGE i001 WITH ‘Good’ ‘Player’.

–>  The above will return “I am a good player”.

***Note: We can even give index to ‘&’ in the table. Suppose the text was “I am a 2& 1&”.

Then the above Message will give output : “I am a player good”.

 

Types of SAP Message

Value Type Explanation
A Termination It is displayed in a dialog box and the program is terminated.
E Error It displays an error message in status bar and all the input fields are cleared.
I Information It is displayed in a dialog box.
S Status It is displayed in status bar of next screen.
W Warning It displays a warning message in status bar and all the input fields are cleared.
X Exit Exit messages cancels the running program and returns a dump.

 

Information Message in SAP

Information Message in SAP – Image Illustration

Error Message in SAP

Error Message in SAP – Image Illustration

Status Message in SAP

Status Message in SAP – Image Illustration

Leave a comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Translate »