[ABAP] String in Messagetexte wandeln/aufteilen

Variante 1 (eigener Klasse, ohne Überschreiben der Systemvariablen)

* Hilfsklasse
CLASS lcl_msg_convert DEFINITION.
  PUBLIC SECTION.
    TYPES: BEGIN OF ty_s_msgtext,
             msgv1 TYPE symsgv,
             msgv2 TYPE symsgv,
             msgv3 TYPE symsgv,
             msgv4 TYPE symsgv,
           END OF ty_s_msgtext.

    CLASS-METHODS: get_msgv_struct
      IMPORTING
                msg_text             TYPE string
      RETURNING VALUE(rv_msgv_stuct) TYPE ty_s_msgtext.
ENDCLASS.

CLASS lcl_msg_convert IMPLEMENTATION.
  METHOD get_msgv_struct.
* String in Struktur wandeln
    rv_msgv_stuct = msg_text.
  ENDMETHOD.
ENDCLASS.

START-OF-SELECTION.
* Struktur für Messagetexte
  DATA(lv_msg_text) = VALUE lcl_msg_convert=>ty_s_msgtext( ).

  TRY.
* künstlich eine Exeption auslösen
      DATA(a) = 1 / 0.
    CATCH cx_root INTO DATA(e_txt).
* Messagelangtext in Message-Struktur übergeben
      lv_msg_text = lcl_msg_convert=>get_msgv_struct( e_txt->get_text( ) ).
  ENDTRY.

* einzelne Messagestrings ausgeben
  WRITE: / lv_msg_text-msgv1, lv_msg_text-msgv2, lv_msg_text-msgv3, lv_msg_text-msgv4.

Variante 2 (CL_MESSAGE_HELPER)

TRY.
    DATA(a) = 1 / 0.
  CATCH cx_root INTO DATA(e_txt).
* Übergabe des Exception-Strings in die Systemvariablen
    cl_message_helper=>set_msg_vars_for_clike( e_txt->get_text( ) ).
ENDTRY.

* Ausgabe der Systemvariablen
WRITE: / sy-msgv1, sy-msgv2, sy-msgv3, sy-msgv4.

Links

Kommentare sind geschlossen.