SAP Attachments Mass Extraction for Sales Documents (ABAP)

SAP ABAP

SAP Attachment: The following ABAP program sample is optimized to Mass extraction of SAP Attachments (GOS) for Sales Documents.

SAP Mass Attachments Extraction using ABAP.

The same ABAP Code can be user to mass extract Attachment for

The original post isSAP GOS Attachment Technical

SAP Attachment : Miscellaneous Tips

Search Help for Directory Selection

The methodcl_gui_frontend_services=>directory_browse offers to select a folder from presentation server ( local ).

The result SELECTED_FOLDER is the full path for a local directory.

Check if a file exists on presentation Server

The method cl_gui_frontend_services=>file_exist checks if a file exists locally ( on presentation server).

The result is initial if the file dosn’t exist.

Jump to the new ABAP Programming, check the following book: Mastering SAP ABAP: A complete guide to developing fast, durable, and maintainable ABAP programs in SAP

SAP Functions Modules Used for SAP Attachment

Here the list of the main SAP Function Modules & Methods used below in order to extract and to download Attachment

cl_gui_frontend_services=>directory_browse

This standard Method cl_gui_frontend_services=>directory_browse will retrieve list of local (desktop) folder.

In the most of cases, it is used to select a folder at presentation server as help search for local Directory Search .

cl_gui_frontend_services=>file_exist

This method cl_gui_frontend_services=>file_exist checks if a file exists already .

SO_OBJECT_DOWNLOAD

SO_OBJECT_DOWNLOAD function module will create a file from internal table in SAP to a given pathname.

ABAP Code for SAP Attachment Mass extraction

Let’s start with the Data declaration we need to Extract Attachment:

*&---------------------------------------------------------------------*
*& Report  ZATTACHMENT_MASS_EXT
*&---------------------------------------------------------------------*
*& Mass extraction of SAP Attachment for Sales Document
*& Files will be download to local directory
*&---------------------------------------------------------------------*

REPORT  Zattachment_mass_ext.
TABLES: vbak.
TYPES: BEGIN OF ty_vbeln,
          vbeln    TYPE vbeln,
          instid_b LIKE srgbtbrel-instid_b,
       END OF ty_vbeln.

DATA: BEGIN OF sood_tab OCCURS 0,
        objtp  LIKE sood-objtp,
        objyr  LIKE sood-objyr,
        objno  LIKE sood-objno,
        objdes LIKE sood-objdes,
        file_ext LIKE sood-file_ext,
        objlen LIKE sood-objlen,
        extct  LIKE sood-extct,                            
      END OF sood_tab.

DATA: lt_vbeln TYPE TABLE OF ty_vbeln.
DATA: bin_filesize LIKE soxwd-doc_length.
DATA: bakfilename_g LIKE rlgrap-filename.
DATA: soc3key_g     LIKE soc3-srtfd.
DATA: lv_file TYPE string.
DATA: lv_vbeln TYPE string.
DATA objcont_tab LIKE soli  OCCURS 0 WITH HEADER LINE.
DATA: l_result         TYPE c,
      l_datfilename    TYPE string,
      rtcode_g         LIKE sy-subrc.

FIELD-SYMBOLS:  LIKE LINE OF sood_tab.
FIELD-SYMBOLS:  LIKE LINE OF lt_vbeln.

The second step is to define the Select Screen

SELECT-OPTIONS: s_erdat FOR vbak-erdat.
SELECT-OPTIONS: s_vbeln FOR vbak-vbeln.
SELECT-OPTIONS: s_ktaar FOR vbak-ktaar.
SELECT-OPTIONS: s_vkorg FOR vbak-vkorg.

PARAMETERS: p_lpath LIKE rlgrap-filename DEFAULT 'C:'.

" Help Search for Local Directory search
AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_lpath.
  DATA : lv_folder TYPE string.

  CALL METHOD cl_gui_frontend_services=>directory_browse
    EXPORTING
      window_title    = 'Please Select a destination folder'
      initial_folder  = 'C:'
    CHANGING
      selected_folder = lv_folder.

  p_lpath = lv_folder .
  CALL METHOD cl_gui_cfw=>flush.

The last step is the extraction of SAP Attachments:

START-OF-SELECTION.

  " Retrieve list of SAP Attachment
  SELECT vbak~vbeln srgbtbrel~instid_b FROM srgbtbrel AS srgbtbrel
  INNER JOIN vbak AS vbak
  ON vbak~vbeln = srgbtbrel~instid_a
  APPENDING TABLE lt_vbeln
  PACKAGE SIZE 5000
  WHERE  vbak~vbeln IN  s_vbeln[]
    AND  vbak~erdat IN s_erdat[]
    AND  vbak~ktaar IN s_ktaar[]
    AND  vbak~vkorg IN s_vkorg[]
    AND  vbak~kunnr IN s_kunnr[]
    AND ( vbak~trvog   = 'A' OR vbak~trvog = '9' )
    AND  srgbtbrel~reltype = 'ATTA'
    AND  ( srgbtbrel~typeid_a = 'BUSXXX' OR srgbtbrel~typeid_a = 'BUSXXX' )
    AND  srgbtbrel~typeid_b = 'MESSAGE'.
  ENDSELECT.

  CHECK  lt_vbeln[] IS NOT INITIAL.
  SORT lt_vbeln BY instid_b.
  DELETE ADJACENT DUPLICATES FROM lt_vbeln COMPARING instid_b.

  " Retrieve File Attributs of SAP Attachments
  SELECT objtp objyr objno objdes file_ext objlen extct
    APPENDING TABLE sood_tab FROM sood
    PACKAGE SIZE 5000
    FOR ALL ENTRIES IN lt_vbeln
    WHERE objtp = lt_vbeln-instid_b+17(3)
    AND   objyr = lt_vbeln-instid_b+20(2)
    AND   objno = lt_vbeln-instid_b+22(12).
  ENDSELECT.

  CHECK sood_tab[] IS NOT INITIAL.
  SORT sood_tab BY objtp objyr objno.
  DELETE ADJACENT DUPLICATES FROM sood_tab COMPARING objtp objyr objno.

  LOOP AT sood_tab ASSIGNING  WHERE objlen GT 0.
    REFRESH objcont_tab[].

    CLEAR:  lv_vbeln, l_result , l_datfilename, lv_file,
            bin_filesize, bakfilename_g, lv_file, soc3key_g.

    " Check VBELN
    LOOP AT lt_vbeln ASSIGNING 
        WHERE instid_b+17(3)  = -objtp
        AND   instid_b+20(2)  = -objyr
        AND   instid_b+22(12) = -objno.

      lv_vbeln = -vbeln.
      EXIT.
    ENDLOOP.

    IF lv_vbeln IS INITIAL .
      CONTINUE.
    ENDIF.

    " Build File Name for SAP Attachment
    CONCATENATE lv_vbeln '_' -objdes '.' -file_ext INTO lv_file.

    CONDENSE lv_file NO-GAPS.

    " Replace unsupported caracters in windows filename
    REPLACE ALL OCCURRENCES OF '/' IN lv_file WITH '_'. " for example 

    IF p_local = 'X'.
      CONCATENATE p_lpath '' lv_file INTO lv_file.
    ENDIF.
    REPLACE ALL OCCURRENCES OF '' IN lv_file WITH ''.

    " Check if file exist
    l_datfilename = lv_file.

    CALL METHOD cl_gui_frontend_services=>file_exist
      EXPORTING
        file                 = l_datfilename
      RECEIVING
        result               = l_result
      EXCEPTIONS
        cntl_error           = 1
        error_no_gui         = 2
        wrong_parameter      = 3
        not_supported_by_gui = 4
        OTHERS               = 5.
    IF sy-subrc <> 0.
      CONTINUE.
    ENDIF.
    IF l_result IS NOT INITIAL.
      APPEND INITIAL LINE TO lt_return ASSIGNING .
      -type        = 'I'.
      -number      = 'XXX'.
      -id          = 'ZMSG'.
      -message_v1  = lv_file.
      CONCATENATE lv_file 'already exists' INTO -message SEPARATED BY space.
      CONTINUE.
    ENDIF.
    
    " Build SAP Attachment Key 
    CONCATENATE -objtp -objyr -objno
            INTO soc3key_g.

    IMPORT objcont_tab FROM DATABASE soc3(dt) ID soc3key_g.
    IF sy-subrc EQ 0.

      bin_filesize = -objlen.
      bakfilename_g = lv_file.

      " Download SAP Attachment
      CALL FUNCTION 'SO_OBJECT_DOWNLOAD'
        EXPORTING
          bin_filesize     = bin_filesize
          filetype         = 'BIN'
          path_and_file    = bakfilename_g
          extct            = -extct
          no_dialog        = 'X'
        TABLES
          objcont          = objcont_tab
        EXCEPTIONS
          file_write_error = 1
          invalid_type     = 2
          x_error          = 3
          kpro_error       = 4
          OTHERS           = 5.
      IF sy-subrc = 0.
        APPEND INITIAL LINE TO lt_return ASSIGNING .
        -type        = 'S'.
        -number      = '000'.
        -id          = 'ZXXX'.
        -message_v1  = lv_file.
        CONCATENATE  lv_file 'SAP Attachment cannot be extracted'  INTO -message SEPARATED BY space.
        CONTINUE.
      ELSE.
        APPEND INITIAL LINE TO lt_return ASSIGNING .
        CALL FUNCTION 'BALW_BAPIRETURN_GET2'
          EXPORTING
            type   = sy-msgty
            cl     = sy-msgid
            number = sy-msgno
            par1   = sy-msgv1
            par2   = sy-msgv2
            par3   = sy-msgv3
            par4   = sy-msgv4
          IMPORTING
            return = .
        CONTINUE.
      ENDIF.
    ENDIF.

    REFRESH objcont_tab[].
  ENDLOOP.

  REFRESH objcont_tab[].
  REFRESH lt_vbeln[].