Skip to Content
Author's profile photo Ali Akbar Mirnia

Massive GOS attchment upload to FI docuemnt

Hello All,

This program upload FI scan file to FI document ,

note :scan file must pdf format , and pdf file must equal  FI number,


*&---------------------------------------------------------------------*
*& Report  ZGOS_ATTACHMENT
*&
*&---------------------------------------------------------------------*
*& Mass Upload gos file to FI document.
*&
*&---------------------------------------------------------------------*
REPORT  ZGOS_ATTACHMENT.
*report  zgos_attachment.
*Macros.
include <cntn01>.
swc_container lt_message_container.
*top dec.
type-pools: slis, abap, truxs.
data:      gt_filename type standard table of dxfilep with header line,
      wa_filename type dxfilep,
      gt_bin      type solix occurs 0,
      wa_bin      type solix,
      g_filename  type string,
      l_obj       type swc_object,
      gs_obja     type borident,
      gs_objb     type borident,
      gs_binrel   type gbinrel,
      gt_binatt   type standard table of brelattr,
      g_attsize   type wsrm_error-wsrm_direction,
      objkey TYPE BORIDENT-OBJKEY.
DATA : BEGIN OF bin_tab OCCURS 0,
       tx TYPE c  LENGTH 255,
      END OF bin_tab.
selection-screen begin of block b0 with frame title text-001.
parameters: p_file  type rlgrap-filename OBLIGATORY,
            gjahr TYPE gjahr OBLIGATORY,
            bukrs TYPE bukrs DEFAULT '1000'.
selection-screen end of block b0.
DATA: lt_file_table TYPE TABLE OF sdokpath,
      wa_file_table LIKE LINE OF lt_file_table,
      lt_dir_table  TYPE TABLE OF sdokpath,
      lv_belnr TYPE bkpf-belnr,
      lv_char TYPE n LENGTH 10,
      ls_bkpf TYPE bkpf.
DATA : FILEPATH TYPE  STRING,
      FILENAME  TYPE  STRING,
      doc_t TYPE string.
DATA: ls_file_table TYPE sdokpath,
      ls_output     TYPE sdokpath,
      lt_output     TYPE TABLE OF sdokpath.
DATA: lv_dir          TYPE cffile-filename, lv_flag(3)      TYPE n,
      lv_app_path(80) TYPE c,               lv_file_count   TYPE i,
      lv_cnt(3)       TYPE n,               lv_text(200)    TYPE c,
      lv_filename     TYPE string.
DATA : BEGIN OF bel_t OCCURS 0,
    belnr TYPE belnr,
  END OF bel_t.
DATA : lv_text_path(50).
*----------------------------------------------------------------------*
* AT SELECTION-SCREEN  on <parameters>                                 *
*----------------------------------------------------------------------*
at selection-screen on p_file.
  if p_file is initial.
    message e999(yfimc01) with 'Please select the file path'. "#EC NOTEXT
  endif.
*----------------------------------------------------------------------*
* AT SELECTION-SCREEN  on HELP-REQUEST|VALUE-REQUEST                   *
*----------------------------------------------------------------------*
at selection-screen  on value-request for p_file.
*  call function 'F4_FILENAME'
*    exporting
*      program_name  = syst-cprog
*      dynpro_number = syst-dynnr
*      field_name    = 'P_FILE'
*    importing
*      file_name     = p_file.
  PERFORM value_request_pc_file_download USING p_file.
start-of-selection.
  CALL FUNCTION 'TMP_GUI_DIRECTORY_LIST_FILES'
    EXPORTING
      directory  = p_file
      filter     = '*.PDF'
    IMPORTING
      file_count = lv_file_count
    TABLES
      file_table = lt_file_table
      dir_table  = lt_dir_table
    EXCEPTIONS
      cntl_error = 1
      OTHERS     = 2.
  CLEAR wa_file_table.
  LOOP AT lt_file_table INTO wa_file_table.
    CLEAR : ls_bkpf.
    lv_text_path = wa_file_table-PATHNAME.
    REPLACE ALL OCCURRENCES OF '.PDF' IN lv_text_path WITH ''.
    REPLACE ALL OCCURRENCES OF '.pdf' IN lv_text_path WITH ''.
    CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'
      EXPORTING
        INPUT         = lv_text_path
     IMPORTING
       OUTPUT        = lv_belnr
              .
*    WRITE lv_text_path to lv_char .
*    move lv_char to lv_belnr.
    SELECT SINGLE * FROM bkpf INTO ls_bkpf WHERE belnr = lv_belnr AND
                                    bukrs = bukrs  AND
                                    GJAHR = GJAHR.
    IF sy-subrc = 0.
      CLEAR : g_filename , g_attsize , gt_bin.
      CONCATENATE p_file wa_file_table-PATHNAME INTO g_filename.
      concatenate    bukrs  "company code
                       lv_belnr "FI Document
                       gjahr  "fiscal year
                       into
                       objkey.
****************************************************************************8
     MOVE p_file TO FILEPATH.
     MOVE wa_file_table-PATHNAME TO FILENAME.
     MOVE lv_belnr to doc_t.
     CALL FUNCTION 'ZHR_GOS_UPLOAD'
      EXPORTING
        OBJTYPE        = 'BKPF'
        OBJKEY         = OBJKEY
        FILEPATH       = FILEPATH
        FILENAME       = FILENAME
        DOCTITLE       = doc_t
        DOCTYPE        = 'PDF'
      TABLES
        BIN_TAB        = bin_tab
      EXCEPTIONS
        UNKNOWN_ERROR  = 1
        NO_FILE_OR_BIN = 2
        NO_DOC_TYPE    = 3
        OTHERS         = 4.
*******************************************************************************
*      call function 'GUI_UPLOAD'
*        exporting
*          filename                = g_filename
*          filetype                = 'BIN'
*        importing
*          filelength              = g_attsize
*        tables
*          data_tab                = gt_bin
*        exceptions
*          file_open_error         = 1
*          file_read_error         = 2
*          no_batch                = 3
*          gui_refuse_filetransfer = 4
*          invalid_type            = 5
*          no_authority            = 6
*          unknown_error           = 7
*          bad_data_format         = 8
*          header_not_allowed      = 9
*          separator_not_allowed   = 10
*          header_too_long         = 11
*          unknown_dp_error        = 12
*          access_denied           = 13
*          dp_out_of_memory        = 14
*          disk_full               = 15
*          dp_timeout              = 16
*          others                  = 17.
*
*      if sy-subrc <> 0.
*      else.
**    file Uploaded successfully.
**    convert uploaded file contents into BIN format.
*        data: l_seq type i.
*        swc_container      l_cont.
*        swc_create_object  l_obj  'MESSAGE'       ''.
*        swc_set_element    l_cont 'NO_DIALOG'     'X'.
*        swc_set_element    l_cont 'DOCUMENTTITLE' g_filename.
*        swc_set_table      l_cont 'Content_Hex'   gt_bin.
*        swc_set_element    l_cont 'DOCUMENTTYPE'  'TXT'.
*        swc_set_element    l_cont 'DOCUMENTSIZE'  g_attsize.
*        swc_refresh_object l_obj.
*        swc_call_method    l_obj  'CREATE'        l_cont.
*        swc_get_object_key l_obj  gs_objb-objkey.
*        gs_objb-objtype = 'MESSAGE'.   "type of attach document
*        gs_obja-objtype = 'BKPF'.      "BO of SAP Document.
*        concatenate    bukrs  "company code
*                       lv_belnr "FI Document
*                       gjahr  "fiscal year
*                       into
*                       gs_obja-objkey.
*        call function 'BINARY_RELATION_CREATE_COMMIT'
*          exporting
*            obj_rolea      = gs_obja
*            obj_roleb      = gs_objb
*            relationtype   = 'ATTA'
*          importing
*            binrel         = gs_binrel
*          tables
*            binrel_attrib  = gt_binatt
*          exceptions
*            no_model       = 1
*            internal_error = 2
*            unknown        = 3
*            others         = 4.
*        if sy-subrc eq 0.
*          message s043(sgos_msg).
*        endif.
*      endif.
    ENDIF.
  ENDLOOP.
*&---------------------------------------------------------------------*
*&      Form  VALUE_REQUEST_PC_FILE_DOWNLOAD
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->P_P_FILE  text
*----------------------------------------------------------------------*
FORM VALUE_REQUEST_PC_FILE_DOWNLOAD  USING   field.
  CALL FUNCTION '/SAPDMC/LSM_F4_FRONTEND_FILE'
    CHANGING
      pathfile         = p_file
    EXCEPTIONS
      canceled_by_user = 1
      system_error     = 2
      OTHERS           = 3.
  CLEAR lv_dir.
* Below functionality used to delete the file name form the selected path.
  DO.
    SEARCH p_file FOR '\'.
    IF sy-subrc EQ 0.
      lv_flag = strlen( p_file ).
      sy-fdpos = sy-fdpos + 1.
      CONCATENATE  lv_dir p_file+0(sy-fdpos)  INTO lv_dir.
      p_file+0(sy-fdpos) = ''.
      CONDENSE p_file.
    ELSE.
      EXIT.
    ENDIF.
  ENDDO.
  CLEAR p_file.
  p_file = lv_dir.
  CONCATENATE 'Directory selected: --> ' p_file INTO ls_output SEPARATED BY space.
  APPEND ls_output TO lt_output.
  CLEAR ls_output.
ENDFORM. " value_request_pc_file_download




FUNCTION ZHR_GOS_UPLOAD.



FUNCTION ZHR_GOS_UPLOAD.
 *"----------------------------------------------------------------------
 *"*"Local Interface:
 *"  IMPORTING
 *"     REFERENCE(OBJTYPE) TYPE  BORIDENT-OBJTYPE
 *"     REFERENCE(OBJKEY) TYPE  BORIDENT-OBJKEY
 *"     REFERENCE(FILEPATH) TYPE  STRING OPTIONAL
 *"     REFERENCE(FILENAME) TYPE  STRING OPTIONAL
 *"     REFERENCE(DOCTITLE) TYPE  STRING OPTIONAL
 *"     REFERENCE(DOCTYPE) TYPE  SOFM-DOCTP OPTIONAL
 *"  TABLES
 *"      BIN_TAB
 *"  EXCEPTIONS
 *"      UNKNOWN_ERROR
 *"      NO_FILE_OR_BIN
 *"      NO_DOC_TYPE
 *"----------------------------------------------------------------------
  include : <cntn01>.
   constants:
             c_docnm  type borident-objtype value 'MESSAGE',
             c_reltyp type breltyp-reltype  value 'ATTA'.
   types: begin of ty_message_key,
           foltp type so_fol_tp,
           folyr type so_fol_yr,
           folno type so_fol_no,
           doctp type so_doc_tp,
           docyr type so_doc_yr,
           docno type so_doc_no,
           fortp type so_for_tp,
           foryr type so_for_yr,
           forno type so_for_no,
          end of ty_message_key.
   data : xobject_b type borident.
   data : xobject_a type borident.
   data : xdoc_size    type i.
   data : xfile_lines   type i.
   data : xmessage_key type ty_message_key.
   data : xmessage     type swc_object.
   data : idoc_content type standard table of soli-line.
   data : xdoc_content type soli-line.
   data : xfilenamepath type string.
   data : xdoctitle type string.
   data : xdoctype type sofm-doctp.
   data : xfname type string.
   if bin_tab[] is initial
     and ( filename is initial
       or  filepath is initial ).
     raise no_file_or_bin.
   endif.
   if not bin_tab[] is initial
      and doctype is initial.
     raise no_doc_type.
   endif.
   if not bin_tab[] is initial.
     xdoctype = doctype.
     translate xdoctype to upper case.
   else.
     split filename at '.' into xfname xdoctype.
     translate xdoctype to upper case.
   endif.
 *  * Create an initial instance of BO 'MESSAGE' - to call the
 * instance-independent method 'Create'.
   swc_create_object xmessage 'MESSAGE' xmessage_key.
 * define container to pass the parameter values to the method call
 * in next step.
   swc_container imessage_container.
 * Populate container with parameters for method
   if doctitle is initial.
     xdoctitle = filename.
   else.
     xdoctitle = doctitle.
   endif.
   swc_set_element imessage_container 'DOCUMENTTITLE'  xdoctitle.
   swc_set_element imessage_container 'DOCUMENTLANGU' 'E'.
   swc_set_element imessage_container 'NO_DIALOG'     'X'.
   swc_set_element imessage_container 'DOCUMENTNAME'   c_docnm.
   swc_set_element imessage_container 'DOCUMENTTYPE'   xdoctype.
   if not bin_tab[] is initial.
 * Translate table to 255, if not 255.
     data: tab_len type i.
     read table bin_tab index 1.
     tab_len = strlen( bin_tab ).
     if tab_len < 255.
       data: xbuffer type string.
       loop at bin_tab.
         translate bin_tab using ' ~'.
         concatenate xbuffer bin_tab into xbuffer.
       endloop.
       translate xbuffer using '~ '.
       do.
         xdoc_content = xbuffer.
         append xdoc_content to idoc_content.
         shift xbuffer left by 255 places.
         if xbuffer is initial.
           exit.
         endif.
       enddo.
     else.
       idoc_content[] =  bin_tab[].
     endif.
   else.
     concatenate filepath filename into xfilenamepath.
 * Upload file from frontend
     call function 'GUI_UPLOAD'
          exporting
               filename = xfilenamepath
               filetype = 'BIN'
 *              HEADER_LENGTH =
         IMPORTING
           FILELENGTH = xdoc_size
          tables
               data_tab = idoc_content
          exceptions
               others   = 17.
   endif.
 *  * 'DocumentContent' is a multi-line element ( itab ).
   swc_set_table imessage_container 'DocumentContent' idoc_content.
 * Size is required in case of File attachments
   describe table idoc_content lines xfile_lines.
   read table idoc_content into xdoc_content index xfile_lines.
 *  xdoc_size = ( 255 * ( xfile_lines - 1 ) ) +
 *              strlen( xdoc_content  ).
   swc_set_element imessage_container 'DOCUMENTSIZE'   xdoc_size .
 * Refresh to get the reference of create 'MESSAGE' object for attachment
   swc_refresh_object xmessage.
   swc_call_method xmessage 'CREATE' imessage_container.
 * Get Key of new object
   swc_get_object_key xmessage xmessage_key.
 * Now we have attachment as a business object instance. We can now
 * attach it to our main business object instance.
 * Create main BO object_a
   xobject_a-objkey = objkey.
   xobject_a-objtype = objtype.
 * Create attachment BO object_b
   xobject_b-objkey = xmessage_key.
   xobject_b-objtype = c_docnm.
   call function 'BINARY_RELATION_CREATE'
        exporting
             obj_rolea    = xobject_a
             obj_roleb    = xobject_b
             relationtype = c_reltyp
        exceptions
             others       = 1.
   if sy-subrc = 1.
     raise unknown_error.
   endif.
   commit work.
 ENDFUNCTION.

Assigned Tags

      4 Comments
      You must be Logged on to comment or reply to a post.
      Author's profile photo cind q
      cind q

      Dear Ali,

      Would you please share the related information about the BAPI: 'ZHR_GOS_UPLOAD' ?

      Thanks. @Ali Akbar Mirnia

      Author's profile photo Former Member
      Former Member
      Author's profile photo Former Member
      Former Member

      Thank you very much! you are awesome!

      Author's profile photo Rafael Ferreira Soares
      Rafael Ferreira Soares

      Thanks so much! It's still very useful!