[ABAP] Leeren Transportauftrag anlegen

* TRFUNCTION
*  K  Workbench-Auftrag
*  W  Customizing-Auftrag
*  C  Umzug von Objekten ohne Paketwechsel
*  O  Umzug von Objekten mit Paketwechsel
*  E  Umzug eines kompletten Pakets
*  T  Transport von Kopien
*  S  Entwicklung/Korrektur
*  R  Reparatur
*  X  Unklassifizierte Aufgabe
*  Q  Customizing-Aufgabe
*  G  Stückliste für CTS-Projekt
*  M  Auftrag für Mandantentransport
*  P  Stückliste für Upgrade
*  D  Stückliste für Patch
*  F  Stückliste
*  L  Löschtransport
TRY.
    cl_adt_cts_management=>create_empty_request( EXPORTING iv_type           = 'K'
                                                           iv_text           = 'Text für Transportauftrag'
*                                                           iv_owner          =
*                                                           it_users          =
*                                                           iv_target         =
                                                 IMPORTING es_request_header = DATA(lv_request_header) ).

    cl_demo_output=>write_data( lv_request_header ).

* 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       = 'Übersicht Unterpakete'
                                          html_string = lv_html
                                          container   = cl_gui_container=>default_screen ).

* cl_gui_container=>default_screen erzwingen
    WRITE: space.
  CATCH cx_root INTO DATA(e_txt).
    WRITE: / e_txt->get_text( ).
ENDTRY.

[ABAP] F4-Hilfe für Transportaufträge und Transportaufgaben

* Siehe Typgruppe SCTSC (Konstanten CTS)
DATA: lv_tkorr TYPE e070-trkorr.
DATA: lv_korr_head TYPE trwbo_request_header.

* F4-Hilfe für Aufträge/Aufgaben
CALL FUNCTION 'TR_F4_REQUESTS'
  EXPORTING
    iv_username             = sy-uname
    iv_client               = sy-mandt
    iv_trfunctions          = 'KW'     "TRFUNCTION: Workbench - K, Customizing - W"
    iv_trstatus             = 'DLRO'   "TRSTATUS: Änderbar - D, Geschützt - L, Freigabe gestartet - O, Freigegeben - R"
    iv_via_selection_screen = abap_true
    iv_complete_requests    = abap_true
  IMPORTING
    ev_selected_request     = lv_tkorr
    es_selected_request     = lv_korr_head.

cl_demo_output=>write_data( lv_tkorr ).
cl_demo_output=>write_data( lv_korr_head ).
cl_demo_output=>display( ).

[ABAP] Transport von Kopien aus Transportauftrag anlegen und Aufgaben/Objekte übernehmen

* Siehe Typgruppe SCTSC (Konstanten CTS)

SELECTION-SCREEN BEGIN OF LINE.
  SELECTION-SCREEN COMMENT (20) lbl1 FOR FIELD p_trkorr.
  PARAMETERS: p_trkorr LIKE e070-trkorr OBLIGATORY.
SELECTION-SCREEN END OF LINE.

SELECTION-SCREEN BEGIN OF LINE.
  SELECTION-SCREEN COMMENT (20) lbl2 FOR FIELD p_tar.
  PARAMETERS: p_tar TYPE trheader-tarsystem OBLIGATORY VALUE CHECK DEFAULT 'XYZ'.
SELECTION-SCREEN END OF LINE.

INITIALIZATION.
  lbl1 = 'Transport-Auftrag:'.
  lbl2 = 'Transportziel:'.

START-OF-SELECTION.

  DATA: lv_req TYPE trwbo_request.

* (Nach-)Lesen von Einzelbestandteilen eines Auftrags
  CALL FUNCTION 'TR_READ_REQUEST'
    EXPORTING
      iv_read_e070     = abap_true
      iv_read_e07t     = abap_true
      iv_trkorr        = p_trkorr
    CHANGING
      cs_request       = lv_req
    EXCEPTIONS
      error_occured    = 1
      no_authorization = 2
      OTHERS           = 3.

  IF sy-subrc = 0.

    WRITE: / 'Kopfdaten des Auftrages', p_trkorr, 'gelesen.'.

* Text übernehmen
    DATA(lv_as4text) = CONV as4text( |TvK:{ lv_req-h-as4text }| ).
    DATA: lv_tvk TYPE trwbo_request_header.

* Auftrag mit Aufgaben anlegen (ohne Dialog)
    CALL FUNCTION 'TR_INSERT_REQUEST_WITH_TASKS'
      EXPORTING
        iv_type            = sctsc_type_transport " TvK anlegen
        iv_text            = lv_as4text
        iv_owner           = sy-uname
        iv_target          = p_tar
        iv_with_badi_check = abap_true
      IMPORTING
        es_request_header  = lv_tvk
      EXCEPTIONS
        insert_failed      = 1
        enqueue_failed     = 2
        OTHERS             = 3.

    IF sy-subrc = 0.

      WRITE: / 'Ziel-Auftrag', lv_tvk-trkorr, 'angelegt.'.

* Quell-Auftrag kopieren
* Kopieren von Aufträgen/Aufgaben
      CALL FUNCTION 'TR_COPY_COMM'
        EXPORTING
          wi_dialog                = abap_false
          wi_trkorr_from           = p_trkorr
          wi_trkorr_to             = lv_tvk-trkorr
          wi_without_documentation = abap_true
        EXCEPTIONS
          db_access_error          = 1
          trkorr_from_not_exist    = 2
          trkorr_to_is_repair      = 3
          trkorr_to_locked         = 4
          trkorr_to_not_exist      = 5
          trkorr_to_released       = 6
          user_not_owner           = 7
          no_authorization         = 8
          wrong_client             = 9
          wrong_category           = 10
          object_not_patchable     = 11
          OTHERS                   = 12.

      IF sy-subrc = 0.

        WRITE: / 'Objekte des Quell-Auftrages', p_trkorr, 'nach', lv_tvk-trkorr, 'kopiert'.

        DATA(lv_ok) = abap_true.

* Quell-Aufgaben kopieren
        SELECT FROM e070
          FIELDS trkorr
          WHERE strkorr = @p_trkorr
          INTO TABLE @DATA(it_trkorr_aufg).

        WRITE: / lines( it_trkorr_aufg ), 'Aufgaben des Quell-Auftrages', p_trkorr, 'gelesen.'.

* alle Quell-Aufgaben des Quell-Auftrages
        LOOP AT it_trkorr_aufg ASSIGNING FIELD-SYMBOL(<a>).
* Kopieren von Aufträgen/Aufgaben
          CALL FUNCTION 'TR_COPY_COMM'
            EXPORTING
              wi_dialog                = abap_false
              wi_trkorr_from           = <a>-trkorr
              wi_trkorr_to             = lv_tvk-trkorr
              wi_without_documentation = abap_true
            EXCEPTIONS
              db_access_error          = 1
              trkorr_from_not_exist    = 2
              trkorr_to_is_repair      = 3
              trkorr_to_locked         = 4
              trkorr_to_not_exist      = 5
              trkorr_to_released       = 6
              user_not_owner           = 7
              no_authorization         = 8
              wrong_client             = 9
              wrong_category           = 10
              object_not_patchable     = 11
              OTHERS                   = 12.
          IF sy-subrc = 0.
            WRITE: / 'Aufgabe',<a>-trkorr,'des Quell-Auftrages', p_trkorr, 'nach', lv_tvk-trkorr, 'kopiert'.
          ELSE.
            lv_ok = abap_false.
            WRITE: / 'Fehler (', sy-subrc, ') beim Kopieren der Quell-Aufgabe:', <a>-trkorr.
            EXIT.
          ENDIF.
        ENDLOOP.

        IF lv_ok = abap_true.
* Objekte und Schlüssel eines Auftrags sortieren
          CALL FUNCTION 'TR_SORT_AND_COMPRESS_COMM'
            EXPORTING
              iv_trkorr                      = lv_tvk-trkorr
            EXCEPTIONS
              trkorr_not_found               = 1
              order_released                 = 2
              error_while_modifying_obj_list = 3
              tr_enqueue_failed              = 4
              no_authorization               = 5
              OTHERS                         = 6.

          IF sy-subrc = 0.
            WRITE: / 'Ziel-Transportauftrag', lv_tvk-trkorr, 'erfolgreich sortiert und verdichtet.'.
          ELSE.
            WRITE: / 'Fehler (', sy-subrc, ') beim Verdichten von Transportauftrag:', lv_tvk-trkorr.
          ENDIF.

        ELSE.
          WRITE: / 'Fehler beim Kopieren der Quell-Aufgaben in Transportauftrag:', lv_tvk-trkorr.
        ENDIF.

      ELSE.
        WRITE: / 'Fehler (', sy-subrc, ') beim Lesen des Quell-Auftrages:', p_trkorr.
      ENDIF.

    ELSE.
      WRITE: / 'Fehler (', sy-subrc, ') beim Anlegen des Ziel-Auftrages'.
    ENDIF.

  ELSE.
    WRITE: / 'Fehler (', sy-subrc, ') beim Lesen des Quell-Auftrages:', p_trkorr.
  ENDIF.

[ABAP] Transportauftrag freigeben

* Siehe Typgruppe SCTSC (Konstanten CTS)

SELECTION-SCREEN BEGIN OF LINE.
  SELECTION-SCREEN COMMENT (20) lbl1 FOR FIELD p_trkorr.
  PARAMETERS: p_trkorr LIKE e070-trkorr OBLIGATORY.
SELECTION-SCREEN END OF LINE.

INITIALIZATION.
  lbl1 = 'Transport-Auftrag:'.

START-OF-SELECTION.
* Freigabe eines Auftrags/einer Aufgabe
  CALL FUNCTION 'TR_RELEASE_REQUEST'
    EXPORTING
      iv_trkorr                  = p_trkorr
      iv_dialog                  = abap_true
      iv_as_background_job       = abap_false
      iv_success_message         = abap_false
      iv_display_export_log      = abap_false
    EXCEPTIONS
      cts_initialization_failure = 1
      enqueue_failed             = 2
      no_authorization           = 3
      invalid_request            = 4
      request_already_released   = 5
      repeat_too_early           = 6
      error_in_export_methods    = 7
      object_check_error         = 8
      docu_missing               = 9
      db_access_error            = 10
      action_aborted_by_user     = 11
      export_failed              = 12
      OTHERS                     = 13.

  IF sy-subrc = 0.
    WRITE: / 'Auftrag', p_trkorr, 'freigegeben.'.
  ENDIF.

[ABAP] Transportauftrag prüfen

* Siehe Typgruppe SCTSC (Konstanten CTS)

SELECTION-SCREEN BEGIN OF LINE.
  SELECTION-SCREEN COMMENT (20) lbl1 FOR FIELD p_trkorr.
  PARAMETERS: p_trkorr LIKE e070-trkorr OBLIGATORY.
SELECTION-SCREEN END OF LINE.

INITIALIZATION.
  lbl1 = 'Transport-Auftrag:'.

START-OF-SELECTION.

  DATA: lv_req TYPE trwbo_request.

* (Nach-)Lesen von Einzelbestandteilen eines Auftrags
  CALL FUNCTION 'TR_READ_REQUEST'
    EXPORTING
      iv_read_e070     = abap_true
      iv_read_e07t     = abap_true
      iv_trkorr        = p_trkorr
    CHANGING
      cs_request       = lv_req
    EXCEPTIONS
      error_occured    = 1
      no_authorization = 2
      OTHERS           = 3.

  IF sy-subrc = 0.

    DATA: lv_messages TYPE ctsgerrmsgs.

* Prüfe kompletten Auftrag
    CALL FUNCTION 'TR_CHECK_REQUEST'
      EXPORTING
        is_request           = lv_req
        iv_check_lockability = abap_true
        iv_collect_mode      = abap_true
      IMPORTING
        et_messages          = lv_messages
      EXCEPTIONS
        header_error         = 1
        attribute_error      = 2
        obj_or_key_error     = 3
        OTHERS               = 4.

    IF sy-subrc = 0.
      cl_demo_output=>write_data( lv_messages ).
      cl_demo_output=>display( ).
    ENDIF.
  ENDIF.

[ABAP] Transportaufträge: Kommandodatei (Cofile) laden

Variante 1 (TR_READ_GLOBAL_INFO_OF_REQUEST)

SELECTION-SCREEN BEGIN OF LINE.
  SELECTION-SCREEN COMMENT (20) lbl1 FOR FIELD p_trkorr.
  PARAMETERS: p_trkorr LIKE e070-trkorr OBLIGATORY.
SELECTION-SCREEN END OF LINE.

INITIALIZATION.
  lbl1 = 'Transport-Auftrag:'.

START-OF-SELECTION.

  DATA: lv_cofile TYPE ctslg_cofile.
  DATA: lv_user TYPE e070-as4user.
  DATA: lv_project TYPE trkorr.

* Kommandodatei lesen und abhängig vom Aufruf aufbereiten
  CALL FUNCTION 'TR_READ_GLOBAL_INFO_OF_REQUEST'
    EXPORTING
      iv_trkorr   = p_trkorr
      iv_dir_type = 'T'          " Typ Directory: T(ransport), P(ut)
    IMPORTING
      es_cofile   = lv_cofile
      ev_user     = lv_user
      ev_project  = lv_project.

  cl_demo_output=>write_data( lv_cofile ).
  cl_demo_output=>write_data( lv_user ).
  cl_demo_output=>write_data( lv_project ).
* html-code holen
data(lv_html) = cl_demo_output=>get( ).
* Daten im Inline-Browser im SAP-Fenster anzeigen
  cl_abap_browser=>show_html( EXPORTING title       = 'Transportauftrag Cofile'
                                        html_string = lv_html
                                        container   = cl_gui_container=>default_screen ).
* cl_gui_container=>default_screen erzwingen
  WRITE: space.

Variante 2 (STRF_READ_COFILE)

SELECTION-SCREEN BEGIN OF LINE.
  SELECTION-SCREEN COMMENT (20) lbl1 FOR FIELD p_trkorr.
  PARAMETERS: p_trkorr LIKE e070-trkorr OBLIGATORY.
SELECTION-SCREEN END OF LINE.

INITIALIZATION.
  lbl1 = 'Transport-Auftrag:'.

START-OF-SELECTION.

  DATA: lv_ev_cofi_header TYPE tstrfcofih.
  DATA: lv_ev_project TYPE trkorr_p.
  DATA: lv_et_predecessors TYPE tms_trkorrs.

  DATA: it_tt_cofi_lines TYPE STANDARD TABLE OF tstrfcofil WITH DEFAULT KEY.

* SL-intern: Lesen und Interpretieren Inhalt von cofiles-Dateien
  CALL FUNCTION 'STRF_READ_COFILE'
    EXPORTING
      iv_dirtype             = 'T'         " Typ Directory: T(ransport), P(ut)
      iv_trkorr              = p_trkorr
      iv_read_header         = 'X'
      iv_transport_directory = ' '
    IMPORTING
      ev_cofi_header         = lv_ev_cofi_header
      ev_project             = lv_ev_project
      et_predecessors        = lv_et_predecessors
    TABLES
      tt_cofi_lines          = it_tt_cofi_lines
    EXCEPTIONS
      wrong_call             = 1
      no_info_found          = 2
      OTHERS                 = 3.

  IF sy-subrc = 0.

    cl_demo_output=>write_data( lv_ev_cofi_header ).
    cl_demo_output=>write_data( lv_ev_project ).
    cl_demo_output=>write_data( lv_et_predecessors ).
    cl_demo_output=>write_data( it_tt_cofi_lines ).

* html-code holen
    DATA(lv_html) = cl_demo_output=>get( ).
* Daten im Inline-Browser im SAP-Fenster anzeigen
    cl_abap_browser=>show_html( EXPORTING title       = 'Transportauftrag Cofile'
                                          html_string = lv_html
                                          container   = cl_gui_container=>default_screen ).
* cl_gui_container=>default_screen erzwingen
    WRITE: space.
  ENDIF.

[ABAP] Umwandlung SAP Codepage / HTTP Encoding Name

Tabellen

TCP00 (SAP Codepage Katalog)
TCP00A (Codepage-Attribute)

Variante 1 (cl_abap_codepage)

TRY.
    DATA(lv_http_codepage) = |utf-8|.
* utf-8 -> 4110
* get sap codepage by external name
    WRITE: / lv_http_codepage, '->', cl_abap_codepage=>sap_codepage( lv_http_codepage ).
  CATCH cx_root INTO DATA(e_txt).
    WRITE: / e_txt->get_text( ).
ENDTRY.

Variante 2 (cl_i18n_utils)

* Encoding Name in SAP Codepage
DATA(lv_codepage_name) = |UTF-8|.
*H  HTTP-Name
*M  MIME-Name (wird nicht mehr benutzt)
*J  Java-Name (java.io und java.lang APIs)
*W  Microsoft-Windows Codepage-Nummer
*D  Dokumentenreferenz (ID(2),OBJ(60))
*T  Kurzer, internationaler Text
*A  Passende, ASCII-basierte Codepage
*C  RTF: /cpg Wert
*F  RTF: /fcharsetN Wert
*S  Sequenz von Code Units
*G  Enthält Filter Segmente
DATA(lv_kind) = CONV cpattrkind( 'H' ).
DATA(lv_sap_codepage) = CONV cpcodepage( '' ).

* Get a codepage from an encoding name
DATA(rv) = cl_i18n_utils=>get_cp_from_name( EXPORTING im_name = lv_codepage_name
                                                      im_kind = lv_kind
                                            IMPORTING ex_cp   = lv_sap_codepage ).

CASE rv.
* successful
  WHEN 0.
    WRITE: / lv_codepage_name, '->', lv_sap_codepage.
* internal error such as old kernel
  WHEN 1.
* I/O error such as missing TCP00A
  WHEN 8.
* invalid arguments
  WHEN 16.
* not found
  WHEN 128.
ENDCASE.

Variante 3 (cl_abap_codepage)

TRY.
    DATA(lv_sap_codepage) = CONV cpcodepage( '4110' ).
* 4110 -> utf-8
* convert sap codepage to http codepage
    WRITE: / lv_sap_codepage, '->', cl_abap_codepage=>sap_to_http( lv_sap_codepage ).
  CATCH cx_root INTO DATA(e_txt).
    WRITE: / e_txt->get_text( ).
ENDTRY.

Variante 4 (cl_abap_codepage)

TRY.
    DATA(lv_sap_codepage) = CONV cpcodepage( '4110' ).
* 4110 -> UTF8
* convert sap codepage to JAVA codepage
    WRITE: / lv_sap_codepage, '->', cl_abap_codepage=>sap_to_java( lv_sap_codepage ).
  CATCH cx_root INTO DATA(e_txt).
    WRITE: / e_txt->get_text( ).
ENDTRY.

[ABAP] Verschlüsselung mit AES256

TRY.
* get sap codepage by external name
    DATA(lv_codepage_utf8) = cl_abap_codepage=>sap_codepage( 'utf-8' ).

* AES-256 Encryption Key
    DATA(lv_key_xstr) = cl_sec_sxml_writer=>generate_key( algorithm = cl_sec_sxml_writer=>co_aes256_algorithm ).

* Plaintext
    DATA(lv_plain_str) = |Testtext_123|.

* STRING --> XSTRING
    DATA(lv_plain_xstr) = cl_bcs_convert=>string_to_xstring( iv_codepage = CONV #( lv_codepage_utf8 )
                                                             iv_string   = lv_plain_str ).

* 16 Bit-Initialisierungsvector für CBC-(Cypher Block Chaining)-Modes der AES Encryption
    DATA(lv_iv_xstr) = CONV xstring( '00000000000000000000000000000000' ).

* Encrypt data with Initialization Vector

    DATA: lv_encrypted_xstr TYPE xstring.

    cl_sec_sxml_writer=>encrypt_iv( EXPORTING plaintext  = lv_plain_xstr
                                              key        = lv_key_xstr
                                              iv         = lv_iv_xstr
                                              algorithm  = cl_sec_sxml_writer=>co_aes256_algorithm_pem
                                    IMPORTING ciphertext = lv_encrypted_xstr ).


* Decrypt data
    DATA: lv_decrypted_xstr TYPE xstring.

    cl_sec_sxml_writer=>decrypt( EXPORTING ciphertext = lv_encrypted_xstr
                                           key        = lv_key_xstr
                                           algorithm  = cl_sec_sxml_writer=>co_aes256_algorithm_pem
                                 IMPORTING plaintext  = lv_decrypted_xstr ).

* XSTRING --> STRING
    DATA(lv_decrypted_str) = cl_bcs_convert=>xstring_to_string( iv_cp   = lv_codepage_utf8
                                                                iv_xstr = lv_decrypted_xstr ).

* Ausgabe
    WRITE: / 'Plain:', lv_plain_str.
    WRITE: / 'Encrypted xstring:', lv_encrypted_xstr.
    WRITE: / 'Decrypted xstring:', lv_decrypted_xstr.
    WRITE: / 'Decrypted:', lv_decrypted_str.

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

[ABAP] Datei-Upload

Variante 1 (cl_gui_frontend_services)

DATA: lv_rc TYPE i.
DATA: it_files TYPE filetable.
DATA: lv_action TYPE i.

* FileOpen-Dialog aufrufen
TRY.
        cl_gui_frontend_services=>file_open_dialog( EXPORTING file_filter    = |pdf (*.pdf)\|*.pdf\|{ cl_gui_frontend_services=>filetype_all }|
                                                              multiselection = abap_false
                                                    CHANGING  file_table     = it_files
                                                              rc             = lv_rc
                                                              user_action    = lv_action ).

    IF lv_action = cl_gui_frontend_services=>action_ok.
* wenn Datei ausgewählt wurde
      IF lines( it_files ) > 0.
* ersten Tabelleneintrag lesen
        DATA(lv_filename) = it_files[ 1 ]-filename.

        DATA: lv_filesize TYPE i.
        DATA: it_bin_data TYPE STANDARD TABLE OF raw255.

* Bild auf Appl. Server hochladen (binary)
        cl_gui_frontend_services=>gui_upload( EXPORTING filename   = |{ lv_filename }|
														filetype   = 'BIN'
										      IMPORTING filelength = lv_filesize
										      CHANGING  data_tab   = it_bin_data ).														

        DATA: lv_xstr TYPE xstring.

* RAW (binary) nach xstring
        CALL FUNCTION 'SCMS_BINARY_TO_XSTRING'
          EXPORTING
            input_length = lv_filesize
          IMPORTING
            buffer       = lv_xstr
          TABLES
            binary_tab   = it_bin_data
          EXCEPTIONS
            failed       = 1
            OTHERS       = 2.

        ...
      ENDIF.
    ENDIF.

  CATCH cx_root INTO DATA(e_text).
    MESSAGE e_text->get_text( ) TYPE 'I'.
ENDTRY.

Variante 2 (cl_secxml_helper)

TRY.
    cl_secxml_helper=>file_f4( EXPORTING initial_directory = ''
                                         window_title      = 'Dateiauswahl'
                               IMPORTING filename          = DATA(lv_xfile) ).


    IF lv_xfile IS NOT INITIAL.
      cl_secxml_helper=>upload_file( EXPORTING filename = lv_xfile
                                     IMPORTING bindata  = DATA(lv_data_xstr) ).
      
      ...
    ENDIF.

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