[ABAP] Projektdefinition zu einem PSP-Element lesen

* PRPS-POSID (Projektstrukturplanelement (PSP-Element))
PARAMETERS: p_wbs TYPE bapi_wbs_list MATCHCODE OBJECT h_wbselem DEFAULT ''.

START-OF-SELECTION.

  DATA: it_wbs_element_table TYPE STANDARD TABLE OF bapi_wbs_elements WITH DEFAULT KEY.
  DATA: it_wbs_hierarchie_table TYPE STANDARD TABLE OF bapi_wbs_hierarchie WITH DEFAULT KEY.

  it_wbs_element_table = VALUE #( ( wbs_element = p_wbs ) ).

* Read detailed information for work breakdown structures
  CALL FUNCTION 'BAPI_PROJECT_GETINFO'
    EXPORTING
      with_subtree           = abap_true
    TABLES
      i_wbs_element_table    = it_wbs_element_table
      e_wbs_hierarchie_table = it_wbs_hierarchie_table.

  cl_demo_output=>write_data( it_wbs_element_table ).
  cl_demo_output=>write_data( it_wbs_hierarchie_table ).

*   HTML-Code vom Demo-Output holen
  DATA(lv_html) = cl_demo_output=>get( ).

*   Daten im Inline-Browser im SAP-Fenster anzeigen
  cl_abap_browser=>show_html( EXPORTING title       = 'Daten'
                                        html_string = lv_html
                                        container   = cl_gui_container=>default_screen ).

*   cl_gui_container=>default_screen erzwingen
  WRITE: space.

[ABAP] DMS: Dokumentenstatus lesen

* Dokumentart (TA DC10, Tabelle TDWA)
PARAMETERS: p_type TYPE bapi_doc_aux-doctype OBLIGATORY.
* Dokumentnummer, mit Suchhilfe auf Tabelle DRAW (Dokumentinformationssatz)
PARAMETERS: p_num TYPE bapi_doc_aux-docnumber OBLIGATORY DEFAULT '10000000001' MATCHCODE OBJECT /plmb/dir_elm.
* Teildokument
PARAMETERS: p_part TYPE bapi_doc_aux-docpart OBLIGATORY DEFAULT '000'.
* Dokumentversion
PARAMETERS: p_ver TYPE bapi_doc_aux-docversion OBLIGATORY DEFAULT '00'.

START-OF-SELECTION.

  DATA: lv_statusextern TYPE bapi_doc_draw-statusextern.
  DATA: lv_statusintern TYPE bapi_doc_draw-statusintern.
  DATA: lv_statusdescription TYPE bapi_doc_tdws-description.
  DATA: lv_return TYPE bapiret2.

* Read Document Status
  CALL FUNCTION 'BAPI_DOCUMENT_GETSTATUS'
    EXPORTING
      documenttype      = p_type
      documentnumber    = p_num
      documentpart      = p_part
      documentversion   = p_ver
    IMPORTING
      statusextern      = lv_statusextern
      statusintern      = lv_statusintern      " Statuswerte in Tabelle TDWS
      statusdescription = lv_statusdescription
      return            = lv_return.

  IF lv_return-type CA 'EA'.

  ELSE.
    cl_demo_output=>write_data( lv_statusextern ).
    cl_demo_output=>write_data( lv_statusintern ).
    cl_demo_output=>write_data( lv_statusdescription ).
    cl_demo_output=>write_data( lv_return ).

*     HTML-Code vom Demo-Output holen
    DATA(lv_html) = cl_demo_output=>get( ).

*     Daten im Inline-Browser im SAP-Fenster anzeigen
    cl_abap_browser=>show_html( EXPORTING title       = 'Daten'
                                          html_string = lv_html
                                          container   = cl_gui_container=>default_screen ).

*     cl_gui_container=>default_screen erzwingen
    WRITE: space.
  ENDIF.

[ABAP] DMS: Dokumentenstatus setzen

* Dokumentart (TA DC10, Tabelle TDWA)
PARAMETERS: p_type TYPE bapi_doc_aux-doctype OBLIGATORY.
* Dokumentnummer, mit Suchhilfe auf Tabelle DRAW (Dokumentinformationssatz)
PARAMETERS: p_num TYPE bapi_doc_aux-docnumber OBLIGATORY DEFAULT '10000000001' MATCHCODE OBJECT /plmb/dir_elm.
* Teildokument
PARAMETERS: p_part TYPE bapi_doc_aux-docpart OBLIGATORY DEFAULT '000'.
* Dokumentversion
PARAMETERS: p_ver TYPE bapi_doc_aux-docversion OBLIGATORY DEFAULT '00'.
* Dokumentstatus, Statuswerte in Tabelle TDWS
PARAMETERS: p_status TYPE bapi_doc_draw-statusintern OBLIGATORY DEFAULT '1' MATCHCODE OBJECT cbglwb_report_status.

START-OF-SELECTION.

  DATA: lv_return TYPE bapiret2.

* Set Document Status
  CALL FUNCTION 'BAPI_DOCUMENT_SETSTATUS'
    EXPORTING
      documenttype    = p_type
      documentnumber  = p_num
      documentpart    = p_part
      documentversion = p_ver
*     statusextern    = ' '
      statusintern    = p_status " Statuswerte in Tabelle TDWS
*     statuslog       = ' '
    IMPORTING
      return          = lv_return.

  IF lv_return-type CA 'EA'.
* Rollback bei Fehler
    CALL FUNCTION 'BAPI_TRANSACTION_ROLLBACK'.
  ELSE.
* Daten verbuchen
    CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
      EXPORTING
        wait = abap_true.

    cl_demo_output=>write_data( lv_return ).

*     HTML-Code vom Demo-Output holen
    DATA(lv_html) = cl_demo_output=>get( ).

*     Daten im Inline-Browser im SAP-Fenster anzeigen
    cl_abap_browser=>show_html( EXPORTING title       = 'Daten'
                                          html_string = lv_html
                                          container   = cl_gui_container=>default_screen ).

*     cl_gui_container=>default_screen erzwingen
    WRITE: space.
  ENDIF.

[ABAP] Daten zu einem PSP-Element lesen

* PRPS-POSID (Projektstrukturplanelement (PSP-Element))
PARAMETERS: p_wbs TYPE bapi_wbs_list MATCHCODE OBJECT h_wbselem DEFAULT ''.

START-OF-SELECTION.

  DATA(it_wbs_element) = VALUE bapi_wbs_list_tab( ( wbs_element = p_wbs ) ).
  DATA: it_e_wbs_element TYPE STANDARD TABLE OF bapi_bus2054_detail WITH DEFAULT KEY.
  DATA(it_return) = VALUE bapiret2_t( ).

* Detail Data for WBS Elements
  CALL FUNCTION 'BAPI_BUS2054_GETDATA'
*    EXPORTING
*      i_project_definition =
    TABLES
      it_wbs_element = it_wbs_element
      et_wbs_element = it_e_wbs_element
      et_return      = it_return.


* Hier Objekte für die Ausgabe hinzufügen
  cl_demo_output=>write_data( it_wbs_element ).
  cl_demo_output=>write_data( it_e_wbs_element ).
  cl_demo_output=>write_data( it_return ).

* HTML-Code vom Demo-Output holen
  DATA(lv_html) = cl_demo_output=>get( ).

* Daten im Inline-Browser im SAP-Fenster anzeigen
  cl_abap_browser=>show_html( EXPORTING title       = 'Daten'
                                        html_string = lv_html
                                        container   = cl_gui_container=>default_screen ).

* cl_gui_container=>default_screen erzwingen
  WRITE: space.

[ABAP] IDOC als XML anzeigen

* IDoc-Nummer
PARAMETERS: p_idoc TYPE edidc-docnum MATCHCODE OBJECT edi_docnum DEFAULT '12345'.

START-OF-SELECTION.

  DATA: lv_action TYPE sy-ucomm.

* Anzeigen eines IDocs in XML-Format mit XSL
  CALL FUNCTION 'IDOC_XML_TRANSFORM'
    EXPORTING
      docnum                        = p_idoc
*     actions                       =
*     styleid                       = 'IE5' " default IE5, siehe auch Tabelle IDOCSTYLE
    IMPORTING
      action                        = lv_action
    EXCEPTIONS
      no_idoc_xml_loaded            = 1
      no_idoc_xml_display_available = 2
      error_loading_object          = 3
      OTHERS                        = 4.

  IF sy-subrc NE 0.
    WRITE: / 'Fehler:', sy-subrc.
  ELSE.
    WRITE: / 'Action:', lv_action.
  ENDIF.

[ABAP] Ergebnisse eines SQL-Ausdrucks in einer Zeile verketten (STRING_AGG)

STRING_AGG verkettet die Ergebnisse eines SQL-Ausdrucks in einer Zeile (Typ SSTRING, Länge 1333). Wenn der String länger als 1333 Zeichen ist, wird eine Ausnahme (CX_SY_OPEN_SQL_DB) geworfen. Die Beschränkung auf 1333 Zeichen kann durch die Funktion TO_CLOB umgangen werden.

Variante 1 (transparente Tabelle)

SELECT FROM t100
  FIELDS arbgb,
         to_clob( STRING_AGG( text, ', ' ) ) AS text_agg
WHERE sprsl = 'D'
GROUP BY arbgb
ORDER BY arbgb
INTO TABLE @DATA(it_t100)
UP TO 10 ROWS.

IF sy-subrc = 0.
  cl_demo_output=>write_data( it_t100 ).

* HTML-Code vom Demo-Output holen
  DATA(lv_html) = cl_demo_output=>get( ).

* Daten im Inline-Browser im SAP-Fenster anzeigen
  cl_abap_browser=>show_html( EXPORTING title       = 'T100'
                                        html_string = lv_html
                                        container   = cl_gui_container=>default_screen ).

* cl_gui_container=>default_screen erzwingen
  WRITE: space.
ENDIF.

Variante 2 (interne Tabelle)

* interne Tabelle anlegen
TYPES: BEGIN OF ty_s_values,
         name TYPE char5,
         text TYPE char3,
       END OF ty_s_values.

TYPES: ty_it_values TYPE STANDARD TABLE OF ty_s_values WITH DEFAULT KEY.

DATA(it_values) = VALUE ty_it_values(
                                      ( name = 'TEXT1' text = '123' )
                                      ( name = 'TEXT1' text = '456' )
                                      ( name = 'TEXT1' text = '789' )
                                      ( name = 'TEXT2' text = 'abc' )
                                      ( name = 'TEXT2' text = 'def' )
                                      ( name = 'TEXT2' text = 'ghi' )
                                      ( name = 'TEXT3' text = '***' )
                                      ( name = 'TEXT3' text = '---' )
                                      ( name = 'TEXT3' text = '+++' )
                                    ).

* Daten aus der internen Tabelle selektieren
SELECT FROM @it_values AS val
  FIELDS val~name,
         to_clob( STRING_AGG( val~text, ', ' ) ) AS text_agg
WHERE val~name = 'TEXT1'
GROUP BY val~name
ORDER BY val~name
INTO TABLE @DATA(it_agg).

IF sy-subrc = 0.
  cl_demo_output=>write_data( it_agg ).

* HTML-Code vom Demo-Output holen
  DATA(lv_html) = cl_demo_output=>get( ).

* Daten im Inline-Browser im SAP-Fenster anzeigen
  cl_abap_browser=>show_html( EXPORTING title       = 'TXT'
                                        html_string = lv_html
                                        container   = cl_gui_container=>default_screen ).

* cl_gui_container=>default_screen erzwingen
  WRITE: space.
ENDIF.

[ABAP] Liste aller Umgebungsvariablen

DATA(it_env) = VALUE spfl_environment_list_t( ).
* Get a list of all environment variables
IF cl_spfl_environment=>get_list( IMPORTING env = it_env ) = 0.
* Hier Objekte für die Ausgabe hinzufügen
  cl_demo_output=>write_data( it_env ).

*   HTML-Code vom Demo-Output holen
  DATA(lv_html) = cl_demo_output=>get( ).

*   Daten im Inline-Browser im SAP-Fenster anzeigen
  cl_abap_browser=>show_html( EXPORTING title       = 'Daten'
                                        html_string = lv_html
                                        container   = cl_gui_container=>default_screen ).

*   cl_gui_container=>default_screen erzwingen
  WRITE: space.
ENDIF.

[ABAP] Muster-Leistungsverzeichnisse lesen

* Muster-LV-Nummer
PARAMETERS: p_spec TYPE tmp_spec-spec_no.

START-OF-SELECTION.

* Muster-Leistungsverzeichnis
  SELECT SINGLE FROM tmp_spec
    FIELDS *
    WHERE spec_no = @p_spec
    INTO @DATA(lv_spec).

  IF sy-subrc = 0.
    DATA(it_esll) = VALUE mmsrv_ml_esll( ).

* Leistungsverzeichnis lesen für Listausgabe im PM/PS
    CALL FUNCTION 'MS_FETCH_SPEC_FOR_LIST'
      EXPORTING
        spec_packno            = lv_spec-packno
      TABLES
        servicetab             = it_esll
*       LIMITTAB               =
      EXCEPTIONS
        no_specification_exist = 1
        OTHERS                 = 2.

    IF sy-subrc = 0.
* Hier Objekte für die Ausgabe hinzufügen
      cl_demo_output=>write_data( it_esll ).

* HTML-Code vom Demo-Output holen
      DATA(lv_html) = cl_demo_output=>get( ).

* Daten im Inline-Browser im SAP-Fenster anzeigen
      cl_abap_browser=>show_html( EXPORTING title       = 'Leistungsverzeichnis'
                                            html_string = lv_html
                                            container   = cl_gui_container=>default_screen ).

* cl_gui_container=>default_screen erzwingen
      WRITE: space.
    ENDIF.
  ENDIF.

[ABAP] ABAP2XLSX: Column Autosize, Verwendung Iterator

SELECT FROM mara
  FIELDS *
  INTO TABLE @DATA(it_mara)
  UP TO 10 ROWS.

IF sy-subrc = 0.
* Excel-Objekt
  DATA(o_xl) = NEW zcl_excel( ).

* aktive Worksheet
  DATA(o_xl_ws) = o_xl->get_active_worksheet( ).
			  
* Name der Sheet
  o_xl_ws->set_title( ip_title = 'Daten' ).

* Tabelle übergeben und Style festlegen
  o_xl_ws->bind_table( ip_table          = it_mara
                       is_table_settings = VALUE #(
*                                                    table_style      = zcl_excel_table=>builtinstyle_medium2
                                                    show_row_stripes = abap_true
                                                  )
                     ).

* alle Spalten mit Iterator durchlaufen und auf Autosize setzen
  DATA(o_col_iterator) = o_xl_ws->get_columns_iterator( ).
  IF o_col_iterator IS BOUND.
	WHILE o_col_iterator->has_next( ).
	  DATA(o_col) = CAST zcl_excel_column( o_col_iterator->get_next( ) ).
	  o_col->set_auto_size( abap_true ).
	ENDWHILE.
  ENDIF.

* Änderungen für alle Spalten übernehmen
  o_xl_ws->calculate_column_widths( ).
ENDIF.

[ABAP] Schlüsselspalten einer DB-Tabelle ermitteln

SELECT FROM mara
  FIELDS *
  INTO TABLE @DATA(it_mara)
  UP TO 10 ROWS.

IF sy-subrc = 0.
* Table: Beschreibung
  DATA(o_tdesc) = CAST cl_abap_tabledescr( cl_abap_tabledescr=>describe_by_data( it_mara ) ).
* Row: Beschreibung
  DATA(o_sdesc) = CAST cl_abap_structdescr( o_tdesc->get_table_line_type( ) ).
* Key Fields
  DATA(it_keyflied_list) = o_sdesc->get_ddic_field_list( ).
* nur die Keyfields zurückgeben
  DELETE it_keyflied_list WHERE keyflag <> abap_true.

  cl_demo_output=>write_data( it_keyflied_list ).
  cl_demo_output=>write_data( it_mara ).

* HTML-Code vom Demo-Output holen
  DATA(lv_html) = cl_demo_output=>get( ).

* Daten im Inline-Browser im SAP-Fenster anzeigen
  cl_abap_browser=>show_html( EXPORTING title       = 'Key-Fields'
                                        html_string = lv_html
                                        container   = cl_gui_container=>default_screen ).

* cl_gui_container=>default_screen erzwingen
  WRITE: space.
ENDIF.