[ABAP] DMS: Dateien an Dokumenteninfosätze anhängen (BAPI_DOCUMENT_CREATE2)

* Dokumentart (TA DC10, Tabelle TDWA)
PARAMETERS: p_dtype TYPE bapi_doc_aux-doctype OBLIGATORY.
* Teildokument
PARAMETERS: p_dpart TYPE bapi_doc_aux-docpart OBLIGATORY DEFAULT '000'.
* Dokumentversion
PARAMETERS: p_dver TYPE bapi_doc_aux-docversion OBLIGATORY DEFAULT '00'.
* Beschreibung
PARAMETERS: p_desc TYPE bapi_doc_drat-description DEFAULT 'Test 123'.
* Ablagekategorie (TA OACT, Tabelle SDOKSTCAE, View V_SDOKSTCA)
PARAMETERS: p_stor TYPE bapi_doc_files2-storagecategory OBLIGATORY.
* Logisches Dokument/Workstation Application (TA S_ALR_87008829, Tabelle TDWP, Bild -> JPG)
PARAMETERS: p_appl TYPE bapi_doc_files2-application_id OBLIGATORY DEFAULT 'JPG'.
* Objekttyp (Material -> MARA)
PARAMETERS: p_otype TYPE bapi_doc_drad-objecttype OBLIGATORY DEFAULT 'MARA'.
* Objektkey (Material -> MATNR)
PARAMETERS: p_okey TYPE bapi_doc_drad-objectkey OBLIGATORY DEFAULT '000000001122334455'.

START-OF-SELECTION.

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

* FileOpen-Dialog aufrufen
      cl_gui_frontend_services=>file_open_dialog( EXPORTING file_filter = |jpg (*.jpg)\|*.jpg\|{ cl_gui_frontend_services=>filetype_all }|
                                                  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.

          DATA(lv_bapi_doc_draw2) = VALUE bapi_doc_draw2(
                                                          documenttype    = p_dtype
                                                          documentversion = p_dver
                                                          documentpart    = p_dpart
                                                        ).

          DATA: lv_documenttype TYPE bapi_doc_aux-doctype.
          DATA: lv_documentnumber TYPE bapi_doc_aux-docnumber.
          DATA: lv_documentpart TYPE bapi_doc_aux-docpart.
          DATA: lv_documentversion TYPE bapi_doc_aux-docversion.
          DATA: lv_return TYPE bapiret2.

          DATA: it_documentdescriptions TYPE STANDARD TABLE OF bapi_doc_drat WITH DEFAULT KEY.
          it_documentdescriptions = VALUE #( (
                                                description  = p_desc
                                                language     = 'D'
                                                language_iso = 'DE'
                                             )
                                           ).

          DATA: it_documentfiles TYPE STANDARD TABLE OF bapi_doc_files2 WITH DEFAULT KEY.

          it_documentfiles = VALUE #( (
                                        originaltype      = '1'
*                                        sourcedatacarrier = 'SAP-SYSTEM' " oder 'DEFAULT'
                                        created_by        = sy-uname
                                        storagecategory   = p_stor
                                        docfile           = it_files[ 1 ]-filename
                                        wsapplication     = p_appl
*                                        checkedin         = abap_true
                                      )
                                    ).

          DATA: it_objectlinks TYPE STANDARD TABLE OF bapi_doc_drad WITH DEFAULT KEY.

          it_objectlinks = VALUE #( (
                                      objecttype    = p_otype
                                      objectkey     = p_okey
                                    )
                                  ).

* Create Document
          CALL FUNCTION 'BAPI_DOCUMENT_CREATE2'
            EXPORTING
              documentdata         = lv_bapi_doc_draw2
            IMPORTING
              documenttype         = lv_documenttype
              documentnumber       = lv_documentnumber
              documentversion      = lv_documentversion
              documentpart         = lv_documentpart
              return               = lv_return
            TABLES
              documentdescriptions = it_documentdescriptions
              documentfiles        = it_documentfiles
              objectlinks          = it_objectlinks.

          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_documenttype ).
            cl_demo_output=>write_data( lv_documentnumber ).
            cl_demo_output=>write_data( lv_documentversion ).
            cl_demo_output=>write_data( lv_documentpart ).
            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       = 'BAPI_DOCUMENT_CREATE2'
                                                  html_string = lv_html
                                                  container   = cl_gui_container=>default_screen ).

* cl_gui_container=>default_screen erzwingen
            WRITE: space.
          ENDIF.
        ENDIF.
      ENDIF.
    CATCH cx_root INTO DATA(e_text).
      MESSAGE e_text->get_text( ) TYPE 'S' DISPLAY LIKE 'E'.
  ENDTRY.

Links