[ABAP] Daten in einem Baum/Tree in der Listanzeige darstellen und Button- und Maus-Events abfangen

* obsolete Darstellung über die FuBas RS_TREE_CONSTRUCT und RS_TREE_LIST_DISPLAY
* besser ist die Nutzung der Klassen cl_salv_tree, cl_gui_simple_tree, cl_gui_column_tree, cl_gui_list_tree
SELECT kunnr,
       land1,
       name1,
       ort01
  INTO TABLE @DATA(it_kna1)
  FROM kna1
  UP TO 25 ROWS.

SELECT vbeln,
       erdat,
       erzet,
       netwr,
       waerk,
       kunnr
  INTO TABLE @DATA(it_vbak)
  FROM vbak
  FOR ALL ENTRIES IN @it_kna1
  WHERE kunnr = @it_kna1-kunnr.

SELECT vbeln,
       posnr,
       matnr,
       netwr,
       waerk
  INTO TABLE @DATA(it_vbap)
  FROM vbap
  FOR ALL ENTRIES IN @it_vbak
  WHERE vbeln = @it_vbak-vbeln.

* Rootnode (Node-Level 1)
DATA(it_nodes) = VALUE snodetab( ( tlevel  = 1
                                   name    = 'Kundenstamm'
                                   nlength = 27
                                   color   = col_key
                                   text    = 'Verkaufs-Report'
                                   tlength = 50
                                 )
                               ).

LOOP AT it_kna1 ASSIGNING FIELD-SYMBOL(<k>).
* Node-Level 2
  DATA(lv_node_2) = VALUE snodetext( tlevel   = 2
                                     name     = <k>-kunnr
                                     nlength  = 19
                                     color    = col_key
                                     text     = <k>-land1
                                     tlength  = 5
                                     tcolor   = col_normal
                                     text1    = <k>-name1
                                     tlength1 = 40
                                     tcolor1  = col_normal
                                     text2    = <k>-ort01
                                     tlength2 = 25
                                     tcolor2  = col_normal
                                   ).

  APPEND lv_node_2 TO it_nodes.

  LOOP AT it_vbak ASSIGNING FIELD-SYMBOL(<v>) WHERE kunnr = <k>-kunnr.
* Node-Level 3
    DATA(lv_node_3) = VALUE snodetext( tlevel   = 3
                                       name     = <v>-vbeln
                                       nlength  = 15
                                       color    = col_positive
                                       text     = <v>-erdat
                                       tlength  = 12
                                       tcolor   = col_normal
                                       text1    = <v>-erzet
                                       tlength1 = 10
                                       tcolor1  = col_normal
                                       text2    = <v>-netwr
                                       tlength2 = 15
                                       tcolor2  = col_total
                                       text3    = <v>-waerk
                                       tlength3 = 5
                                       tcolor3  = col_total
                                     ).

    APPEND lv_node_3 TO it_nodes.

    LOOP AT it_vbap ASSIGNING FIELD-SYMBOL(<p>) WHERE vbeln = <v>-vbeln.
* Node-Level 4
      DATA(lv_node_4) = VALUE snodetext( tlevel   = 4
                                         name     = <p>-posnr
                                         nlength  = 11
                                         color    = col_group
                                         text     = <p>-matnr
                                         tlength  = 23
                                         tcolor   = col_normal
                                         text1    = <p>-netwr
                                         tlength1 = 15
                                         tcolor1  = col_total
                                         text2    = <p>-waerk
                                         tlength2 = 5
                                         tcolor2  = col_total
                                       ).

      APPEND lv_node_4 TO it_nodes.

    ENDLOOP.

  ENDLOOP.

ENDLOOP.

* Tree-Objekte in Funktionsgruppe SEUT
* Neue Hierarchie aufbauen bzw. Teilbaum einfügen
CALL FUNCTION 'RS_TREE_CONSTRUCT'
  TABLES
    nodetab            = it_nodes
  EXCEPTIONS
    tree_failure       = 1
    id_not_found       = 2
    wrong_relationship = 3
    OTHERS             = 4.

IF sy-subrc = 0.
* Konstanten sind in Typgruppe STREE definiert
* Hierarchie anzeigen
  CALL FUNCTION 'RS_TREE_LIST_DISPLAY'
    EXPORTING
      callback_program      = sy-repid
      callback_user_command = 'ON_USER_COMMAND' " Formroutine für Callback-Funktion für Buttons und Mausklicks
      status                = 'STANDARD'        " GUI-Status des Trees (wird für Callback benötigt, kann sonst weggelassen werden
      use_control           = stree_use_list.   " stree_use_list (Liste), stree_use_list_print (Druckausgabe), stree_use_control (Fullscreen-Control), stree_use_in_dynpro (Dynpro)
ELSE.
  WRITE: / 'Fehler:', sy-subrc.
ENDIF.

* Form ON_USER_COMMAND
* Callback-Funktion für Buttons und Mausklicks
FORM on_user_command TABLES   node STRUCTURE seucomm
                     USING    command
                     CHANGING exit
                              list_refresh.

* Messagetext ausgeben, um geklicktes Element zu identifizieren
  MESSAGE |{ node-name }, { command }| TYPE 'S'.
ENDFORM.

[ABAP] ABAP-Proxy-Objekt in XML wandeln und anzeigen

Variante 1 (ABAP_TO_XML_XSTRING)

TRY.
* Proxyobjekt
    DATA(lv_proxy_obj) = VALUE zgenerated_proxy_obj( p_par1 = 'VALUE1'
                                                     p_par2 = 'VALUE2'
                                                   ).

* Proxy-Objekt
    DATA(lv_datatype) = CONV prx_r3name( 'ZGENERATED_PROXY_OBJ' ).

* Simple Transformation mit Namespace
* xml_header
* no
* without_encoding
* full
    DATA(lv_xml) = cl_proxy_xml_transform=>abap_to_xml_xstring( abap_data  = lv_proxy_obj
                                                                ddic_type  = lv_datatype
                                                                xml_header = 'full' " no, without_encoding, full
                                                              ).

* Transformation
    CALL TRANSFORMATION id SOURCE XML lv_xml RESULT XML lv_xml.

* XML anzeigen
    cl_srtg_helper=>write_utf8_xmldoc( doc              = lv_xml
                                       use_html_control = abap_true " Anzeige im Browserfenster
                                     ).

  CATCH cx_root INTO DATA(e_txt).
    WRITE: / e_txt->get_text( ).
ENDTRY.

Variante 2 (CL_SXML_STRING_WRITER)

TRY.
* Proxyobjekt
    DATA(lv_proxy_obj) = VALUE zgenerated_proxy_obj( p_par1 = 'VALUE1'
                                                     p_par2 = 'VALUE2'
                                                   ).

* Proxy-Objekt
    DATA(lv_datatype) = CONV prx_r3name( 'ZGENERATED_PROXY_OBJ' ).

    DATA(o_xml_writer) = cl_sxml_string_writer=>create( type = if_sxml=>co_xt_xml10 ).

* Simple Transformation mit Namespace
    cl_proxy_xml_transform=>abap_to_xml( abap_data  = lv_proxy_obj
                                         ddic_type  = lv_datatype
                                         xml_writer = o_xml_writer
                                       ).

    DATA(lv_xml) = o_xml_writer->get_output( ).

* Transformation
    CALL TRANSFORMATION id SOURCE XML lv_xml RESULT XML lv_xml.

* XML anzeigen
    cl_srtg_helper=>write_utf8_xmldoc( doc              = lv_xml
                                       use_html_control = abap_true " Anzeige im Browserfenster
                                     ).

  CATCH cx_root INTO DATA(e_txt).
    WRITE: / e_txt->get_text( ).
ENDTRY.

[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

[ABAP] BAPI zum Lesen der Daten eines Debitoren / Customers

* Typen
TYPES: ty_it_bapicustomer_02 TYPE STANDARD TABLE OF bapicustomer_02 WITH DEFAULT KEY.
TYPES: ty_it_bapicustomer_03 TYPE STANDARD TABLE OF bapicustomer_03 WITH DEFAULT KEY.
TYPES: ty_it_bapicustomer_06 TYPE STANDARD TABLE OF bapicustomer_06 WITH DEFAULT KEY.

* Führende Nullen beim Debitor/Customer beachten!
DATA(lv_deb) = CONV bapicustomer_id-customer( |{ |10000| WIDTH = 10 ALPHA = IN }| ).

* Rückgabestrukturen
DATA(lv_adr) = VALUE bapicustomer_04( ).
DATA(lv_kna1) = VALUE bapicustomer_kna1( ).
DATA(lv_comp) = VALUE bapicustomer_05( ).
DATA(lv_ret) = VALUE bapiret1( ).

* Rückgabetabellen
DATA(it_bank) = VALUE ty_it_bapicustomer_02( ).
DATA(it_iban) = VALUE ty_it_bapicustomer_03( ).
DATA(it_sepa) = VALUE ty_it_bapicustomer_06( ).

* Detailangaben Debitor (KUNNR (KNA1))
CALL FUNCTION 'BAPI_CUSTOMER_GETDETAIL2'
  EXPORTING
    customerno            = lv_deb
*   COMPANYCODE           =
  IMPORTING
    customeraddress       = lv_adr
    customergeneraldetail = lv_kna1
    customercompanydetail = lv_comp
    return                = lv_ret
  TABLES
    customerbankdetail    = it_bank
    customeribandetail    = it_iban
    customersepadetail    = it_sepa.

* Meldungstyp: S Success, E Error, W Warning, I Info, A Abort
CASE lv_ret-type.
  WHEN 'E' OR 'A'.
* Fehler
    cl_demo_output=>write_data( lv_ret ).
    cl_demo_output=>display( ).
  WHEN OTHERS.
* keine Fehler
    cl_demo_output=>write_data( lv_adr ).
    cl_demo_output=>write_data( lv_kna1 ).
    cl_demo_output=>write_data( lv_comp ).
    cl_demo_output=>write_data( it_bank ).
    cl_demo_output=>write_data( it_iban ).
    cl_demo_output=>write_data( it_sepa ).
    cl_demo_output=>write_data( lv_ret ).
    cl_demo_output=>display( ).
ENDCASE.

[ABAP] BAPI für die Prüfung der Existenz eines Business-Partners

DATA(lv_bp) = CONV bu_partner( '10000' ).
DATA(it_bapi_ret) = VALUE bapiret2_t( ).

* Paket BUPA
* SAP-GP, API: Geschäftspartner auf Existenz prüfen
CALL FUNCTION 'BUPA_EXISTENCE_CHECK'
  EXPORTING
    iv_partner = lv_bp
*   IV_PARTNER_GUID       =
  TABLES
    et_return  = it_bapi_ret.

* IS-H: Nachrichten aus BAPIRET2-Tabelle anzeigen
CALL FUNCTION 'ISH_BAPIRET2_DISPLAY'
  EXPORTING
    amodal_window = abap_false " Fenster modal / amodal anzeigen
  TABLES
    ss_bapiret2   = it_bapi_ret.

[ABAP] Warenbewegungen buchen

* https://wiki.scn.sap.com/wiki/display/ERPSCM/Goods+Movements+with+BAPI

TYPES: BEGIN OF ty_s_material,
         matnr         TYPE mara-matnr,                            " Materialnummer
         erfmg         TYPE mseg-erfmg,                            " Menge in Erfassungsmengeneinheit (mseg-erfmg) / Menge (mseg-menge)
         entry_uom_iso TYPE bapi2017_gm_item_create-entry_uom_iso, " ISO-Code Maßeinheit
       END OF ty_s_material.

TYPES: ty_it_material TYPE STANDARD TABLE OF ty_s_material WITH KEY matnr.

* Testmodus
PARAMETERS: p_test AS CHECKBOX DEFAULT 'X'.

START-OF-SELECTION.

  DATA(it_material) = VALUE ty_it_material( ( matnr = '0815' erfmg = '1' entry_uom_iso = 'PCE' )
                                            ( matnr = '0816' erfmg = '2' entry_uom_iso = 'PCE' )
                                          ).

* Header
  DATA(lv_header) = VALUE bapi2017_gm_head_01( pstng_date    = sy-datum " Buchungsdatum im Beleg
                                               doc_date      = sy-datum " Belegdatum im Beleg
                                               pr_uname      = sy-uname " Benutzername
                                               gr_gi_slip_no = '12345'  " Warenbegleitscheinnummer
                                               header_txt    = 'Text1'  " Belegkopftext
                                               ref_doc_no    = 'Refdoc' " Nummer des externen Materialscheins
                                             ).

* gm_code (siehe auch Tabelle T158G)
* 01  MB01 (Wareneingang zur Bestellung buchen)
* 02  MB31 (Wareneingang zum Fertigungsauftrag)
* 03  MB1A (Warenentnahme)
* 04  MB1B (Umbuchung)
* 05  MB1C (Wareneingang Sonstige)
* 06  MB11 (Warenbewegung)
* 07  MB04 (Nachverrechnung von Beistellmaterial)
  DATA(lv_mvt_code) = VALUE bapi2017_gm_code( gm_code = '03' ). " Zuordnung Code zu Transaktion für BAPI Warenbewegung

* Items für Bewegungsdaten füllen
  DATA(it_items_to_move) = VALUE bapi2017_gm_item_create_t( FOR <m> IN it_material
                                                            (
                                                              material  = CONV #( <m>-matnr ) " Materialnummer
                                                              plant     = '10'                " Werk
                                                              stge_loc  = '01'                " Lagerort
                                                              move_type = '262'               " Bewegungsart (Bestandsführung)
* KZBEW	(Bewegungskennzeichen)
* ' '  Warenbewegung ohne Referenz
*  B  Warenbewegung zur Bestellung
*  F  Warenbewegung zum Auftrag
*  L  Warenbewegung zum Lieferschein
*  K  Warenbewegung zur Kanban-Anforderung (Nur WM intern)
*  O  Nachverrechnung von Beistellmaterial
*  W  Nachverrechnung von Anteils-/Produkt ME Material
                                                              mvt_ind       = ' '                     " Bewegungskennzeichen (Warenbewegung ohne Referenz)
                                                              entry_qnt     = CONV #( <m>-erfmg )     " Menge in Erfassungsmengeneinheit
                                                              entry_uom_iso = <m>-entry_uom_iso       " ISO-Code Maßeinheit
                                                              gl_account    = ''                      " Nummer des Sachkontos
                                                              gr_rcpt       = 'ABC'                   " Warenempfänger
                                                              costcenter    = '100200'                " Kostenstelle
                                                              orderid       = ''                      " Auftragsnummer
                                                              wbs_elem      = ''                      " Projektstrukturplanelement (PSP-Element)
                                                            )
                                                          ).
  DATA(lv_headret) = VALUE bapi2017_gm_head_ret( ).
  DATA(it_return) = VALUE bapiret2_t( ).

* Warenbewegung durchführen
  CALL FUNCTION 'BAPI_GOODSMVT_CREATE'
    EXPORTING
      goodsmvt_header  = lv_header
      goodsmvt_code    = lv_mvt_code
      testrun          = p_test
    IMPORTING
      goodsmvt_headret = lv_headret
    TABLES
      goodsmvt_item    = it_items_to_move
      return           = it_return.

* Fehlerauswertung: E - Error, A - Abort
  LOOP AT it_return ASSIGNING FIELD-SYMBOL(<e>).
    WRITE: / |RETURN [{ <e>-type } { <e>-number }]: { <e>-message }|.
  ENDLOOP.

  DATA(lv_return) = VALUE bapiret2( ).

* Sind Fehler vorhanden?
  IF lines( it_return ) > 0.
* Fehler: Rollback
    CALL FUNCTION 'BAPI_TRANSACTION_ROLLBACK'
      IMPORTING
        return = lv_return.

    WRITE: / |ROLLBACK [{ lv_return-type } { lv_return-number }]: { lv_return-message }|.
  ELSE.
* Keine Fehler: Daten verbuchen
    CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
      EXPORTING
        wait   = abap_true
      IMPORTING
        return = lv_return.

    WRITE: / |COMMIT [{ lv_return-type } { lv_return-number }]: { lv_return-message }|.
  ENDIF.

[ABAP] Popup-Dialog zur Anzeige von BAPI-Meldungen

DATA(it_bapi_ret) = VALUE bapiret2_t( ).

* hier BAPI-Aufruf, welcher it_bapi_ret füllt
...

Variante 1 (CBIH_IAALD_BAPIRET2_SHOW)

* EHS: Anzeige einer BAPI-Fehlermeldungstabelle (BAPIRET2)
CALL FUNCTION 'CBIH_IAALD_BAPIRET2_SHOW'
  TABLES
    i_bapiret2_tab = it_bapi_ret.

Variante 2 (ISH_BAPIRET2_DISPLAY)

* IS-H: Nachrichten aus BAPIRET2-Tabelle anzeigen
CALL FUNCTION 'ISH_BAPIRET2_DISPLAY'
  EXPORTING
    amodal_window = abap_false " Fenster modal / amodal anzeigen
  TABLES
    ss_bapiret2   = it_bapi_ret.

Variante 3 (C14ALD_BAPIRET2_SHOW)

* EHS: Anzeige einer BAPI-Fehlermeldungstabelle (BAPIRET2)
CALL FUNCTION 'C14ALD_BAPIRET2_SHOW'
  TABLES
    i_bapiret2_tab = it_bapi_ret.

Variante 4 (RSCRMBW_DISPLAY_BAPIRET2)

CALL FUNCTION 'RSCRMBW_DISPLAY_BAPIRET2'
    TABLES
      it_return = it_bapi_ret.