<!-- https://abap2ui5.github.io/docs/cookbook/translation_messages/message -->

# 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:

```abap
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:

```abap
METHOD z2ui5_if_app~main.
  client->message_box_display( `this is a message` ).
ENDMETHOD.
```

For error messages, change the type:

```abap
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
```abap
METHOD z2ui5_if_app~main.

  MESSAGE ID `NET` TYPE `I` NUMBER `001` INTO DATA(lv_dummy).
  client->message_box_display( sy ).

ENDMETHOD.
```
### BAPIRET
```abap
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
```abap
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:

```abap
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](./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](https://github.com/abap2UI5-addons/popups).

::: tip **Improvements**
These message functions evolve all the time. Open an issue if you hit errors or incompatibilities, or submit a PR to extend them.
:::

<!-- samples:start (generated by scripts/link-samples.mjs — do not edit) -->

## Working Samples

Complete apps from the [sample catalog](https://github.com/abap2UI5/samples/blob/main/SAMPLES.md)
that use what this page describes. Each is a single class — pull the repository with
[abapGit](https://abapgit.org) and start it with `?app_start=<class>`.

| Sample | Class |
|---|---|
| MessageToast, Text and Duration | [`Z2UI5_CL_SMP_APP_381`](https://github.com/abap2UI5/samples/blob/main/src/01/z2ui5_cl_smp_app_381.clas.abap) |
| MessageBox, Types and Custom Actions | [`Z2UI5_CL_SMP_APP_382`](https://github.com/abap2UI5/samples/blob/main/src/01/z2ui5_cl_smp_app_382.clas.abap) |
| MessageBox from SY, BAPIRET2 or Exception | [`Z2UI5_CL_SMP_APP_008`](https://github.com/abap2UI5/samples/blob/main/src/01/z2ui5_cl_smp_app_008.clas.abap) |
| MessageView and MessagePopover (A) | [`Z2UI5_CL_SMP_APP_452`](https://github.com/abap2UI5/samples/blob/main/src/01/z2ui5_cl_smp_app_452.clas.abap) |
| Message Model and MessageManager (C) | [`Z2UI5_CL_SMP_APP_467`](https://github.com/abap2UI5/samples/blob/main/src/01/z2ui5_cl_smp_app_467.clas.abap) |
| MessagePopover URL Policy (A) | [`Z2UI5_CL_SMP_APP_474`](https://github.com/abap2UI5/samples/blob/main/src/01/z2ui5_cl_smp_app_474.clas.abap) |

<!-- samples:end -->
