Message
Showing messages is an everyday task for ABAP developers. The functions below cover the most common cases.
Message Toast
For brief notifications like success confirmations, use the message toast:
METHOD z2ui5_if_app~main.
client->message_toast_display( `this is a message` ).
ENDMETHOD.Message Box
When the user needs to acknowledge the message, show a message box they must close:
METHOD z2ui5_if_app~main.
client->message_box_display( `this is a message` ).
ENDMETHOD.For error messages, change the type:
METHOD z2ui5_if_app~main.
client->message_box_display(
text = `This is an error message`
type = `error` ).
ENDMETHOD.SY, BAPIRET, CX_ROOT
You can pass common message structures, objects, and variables straight to these functions:
SY
METHOD z2ui5_if_app~main.
MESSAGE ID `NET` TYPE `I` NUMBER `001` INTO DATA(lv_dummy).
client->message_box_display( sy ).
ENDMETHOD.BAPIRET
METHOD z2ui5_if_app~main.
DATA lt_bapiret TYPE STANDARD TABLE OF bapiret2.
CALL FUNCTION `BAPI_USER_GET_DETAIL`
EXPORTING
username = sy-uname
TABLES
return = lt_bapiret.
client->message_box_display( lt_bapiret ).
ENDMETHOD.CX_ROOT
METHOD z2ui5_if_app~main.
TRY.
DATA(lv_val) = 1 / 0.
CATCH cx_root INTO DATA(lx).
client->message_box_display( lx ).
ENDTRY.
ENDMETHOD.The framework accepts other inputs too — pass your message structure and the message box shows it.
All of Them in One App
The fragments above are each one call. Press Run to see the whole family in a single app:
CLASS z2ui5_cl_sample_message DEFINITION PUBLIC.
PUBLIC SECTION.
INTERFACES z2ui5_if_app.
PROTECTED SECTION.
PRIVATE SECTION.
ENDCLASS.
CLASS z2ui5_cl_sample_message IMPLEMENTATION.
METHOD z2ui5_if_app~main.
IF client->check_on_navigated( ).
DATA(view) = z2ui5_cl_ui5_view_builder=>factory(
)->ele( n = `View` ns = `mvc`
)->a( n = `xmlns` v = `sap.m`
)->a( n = `xmlns:mvc` v = `sap.ui.core.mvc`
)->ele( `Page`
)->a( n = `title` v = `Messages`
)->tag( `Button`
)->a( n = `text` v = `toast`
)->a( n = `press` v = client->_event( `TOAST` )
)->tag( `Button`
)->a( n = `text` v = `box`
)->a( n = `press` v = client->_event( `BOX` )
)->tag( `Button`
)->a( n = `text` v = `from an exception`
)->a( n = `press` v = client->_event( `EXC` ) ) ).
client->view_display( view->stringify( ) ).
ELSEIF client->check_on_event( `TOAST` ).
client->message_toast_display( `this is a message` ).
ELSEIF client->check_on_event( `BOX` ).
client->message_box_display( text = `This is an error message`
type = `error` ).
ELSEIF client->check_on_event( `EXC` ).
TRY.
DATA(lv_val) = 1 / 0.
CATCH cx_root INTO DATA(lx).
client->message_box_display( lx ).
ENDTRY.
ENDIF.
ENDMETHOD.
ENDCLASS.More Than One Message at a Time
The message box shows one message — but it also takes a whole set of them. Pass a BAPI return table, a message log or the result of a validation run and the framework flattens it into the lines the box shows; see Logging for the sources it reads.
For something richer than a list of lines — a sortable table with severities and long texts — build the view from the same data, or take the ready-made message dialog from the popups add-on.
Improvements
These message functions evolve all the time. Open an issue if you hit errors or incompatibilities, or submit a PR to extend them.
Working Samples
Complete apps from the sample catalogue that use what this page describes. Each is a single class — pull the repository with abapGit and start it with ?app_start=<class>.
| Sample | Class |
|---|---|
| MessageToast, Text and Duration | Z2UI5_CL_SMP_APP_381 |
| MessageBox, Types and Custom Actions | Z2UI5_CL_SMP_APP_382 |
| MessageBox from SY, BAPIRET2 or Exception | Z2UI5_CL_SMP_APP_008 |
| MessageView and MessagePopover (A) | Z2UI5_CL_SMP_APP_452 |
| Message Model and MessageManager (C) | Z2UI5_CL_SMP_APP_467 |
| MessagePopover URL Policy (A) | Z2UI5_CL_SMP_APP_474 |
