SAP ABAP code for FB03 GOS Document attachment

       As we can attach the document in most of the SAP transactions like, FI documents, PO Document, etc. through GOS object assignment manually by going to each document and uploading the file manually.



This code can be use in case if you want to update in bulk.

I have consider the scenario of FB03, for this in a excel file we will maintain the document details along with file name, then upload through this program.

**************************************************************************

*&---------------------------------------------------------------------*
*& Report ZFI_FB03_GOS
*&---------------------------------------------------------------------*
*&
*&---------------------------------------------------------------------*
REPORT ZK_FI_FB03_GOS.

TYPE-POOLStruxs.

TYPESBEGIN OF tp_itab,
        bukrs type bkpf-bukrs,
        belnr type bkpf-belnr,
        gjahr type bkpf-gjahr,
        filename type string,
        END OF tp_itab,

        begin of tp_split,
          data(150),
        end of tp_split.


DATAgt_itab TYPE STANDARD TABLE OF tp_itab,
      gs_itab TYPE tp_itab,
      gt_split type STANDARD TABLE OF tp_split,
      gs_split type tp_split.

DATAit_raw TYPE truxs_t_text_data.

"for GOS
datag_folderid type soodk.

data gv_filename(200),
       gv_ext(4),
       gv_cfile type PCFILE-PATH.

*2. Insert the object
   datag_docdata like sodocchgi1.

   datag_extension type soodk-objtp.

   datagit_objhdr type table of solisti1 with header line,
         git_objcont type table of solisti1 with header line,
         git_hexcont type table of solix with header line,
         g_docinfo type sofolenti1.

   datag_bizojb     type borident,
         g_attachment type borident.

SELECTION-SCREEN begin of BLOCK b1 WITH FRAME TITLE text-001.
PARAMETERSp_file TYPE  rlgrap-filename.
PARAMETERSp_head TYPE char01 DEFAULT 'X'.
SELECTION-SCREEN end of BLOCK b1.

* At selection screen

AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_file.
  CALL FUNCTION 'F4_FILENAME'
    EXPORTING
      field_name 'P_FILE'
    IMPORTING
      file_name  p_file.

START-OF-SELECTION.
    perform excel_to_itab.
    perform create_gos.

end-of-SELECTION.


*&---------------------------------------------------------------------*
*& Form excel_to_itab
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& -->  p1        text
*& <--  p2        text
*&---------------------------------------------------------------------*
FORM excel_to_itab.

  " Convert Excel Data to SAP internal Table Data
  CALL FUNCTION 'TEXT_CONVERT_XLS_TO_SAP'
    EXPORTING
*     I_FIELD_SEPERATOR        =
      i_line_header            =  p_head
      i_tab_raw_data           =  it_raw       " WORK TABLE
      i_filename               =  p_file
    TABLES
      i_tab_converted_data     gt_itab[]  "ACTUAL DATA
   EXCEPTIONS
      conversion_failed        1
      OTHERS                   2.

  IF sy-subrc <> 0.
    MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
            WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
  ENDIF.

  delete gt_itab WHERE bukrs is INITIAL or
                       belnr is INITIAL or
                       gjahr is INITIAL or
                       filename is INITIAL.


  if gt_itab[] is INITIAL.
     message 'Kindly upload the excel file with valid data' TYPE 'I'.
     leave PROGRAM.
  endif.


ENDFORM.
*&---------------------------------------------------------------------*
*& Form create_gos
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& -->  p1        text
*& <--  p2        text
*&---------------------------------------------------------------------*
FORM create_gos.


    loop at gt_itab into gs_itab.

"convert belnr to internal format
    CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'
      EXPORTING
        input         gs_itab-belnr
     IMPORTING
       OUTPUT        gs_itab-belnr.


"document number validation
   select SINGLE from bkpf into @data(gs_bkpfWHERE bukrs @gs_itab-bukrs
                                                   and belnr @gs_itab-belnr
                                                   and gjahr @gs_itab-gjahr.
   if gs_bkpf-belnr is INITIAL.
      write:/5 gs_itab-bukrs20 gs_itab-belnr35 gs_itab-gjahr50 gv_filename100 'Document does not exit' COLOR 6.
      CONTINUE.  "process next record
   else.
      clear gs_bkpf.
   endif.


*1. Get the folder id
    clear g_folderid.

   call function 'SO_FOLDER_ROOT_ID_GET'
    exporting
*   OWNER                       = ' '
      region                      'B'
    importing
      folder_id                   g_folderid
 EXCEPTIONS
   COMMUNICATION_FAILURE       1
   OWNER_NOT_EXIST             2
   SYSTEM_FAILURE              3
   X_ERROR                     4
   OTHERS                      5
             .
   if sy-subrc <> 0.
* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
*         WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
   endif.

   clear gv_extgv_filenamegt_split[]gs_split.

   split gs_itab-filename at '\' into TABLE gt_split[].

   DESCRIBE TABLE gt_split LINES data(gv_lines).

   if gv_lines is NOT INITIAL.
      gs_split gt_split[ gv_lines ].
   else.
     CONTINUE.
   endif.

   split gs_split-data at '.' INTO gv_filename gv_ext.
   gv_filename gs_split-data.

"splt filename  PC_SPLIT_COMPLETE_FILENAME
*           gv_cfile = gs_itab-filename.
*
*    CALL FUNCTION 'PC_SPLIT_COMPLETE_FILENAME'
*      EXPORTING
*        complete_filename       = gv_cfile
**       CHECK_DOS_FORMAT        =
*     IMPORTING
**       DRIVE                   =
*       EXTENSION               = gv_ext
**       NAME                    =
*       NAME_WITH_EXT           = gv_filename
**       PATH                    =
*     EXCEPTIONS
*       INVALID_DRIVE           = 1
*       INVALID_EXTENSION       = 2
*       INVALID_NAME            = 3
*       INVALID_PATH            = 4
*       OTHERS                  = 5
*              .
*    IF sy-subrc <> 0.
** Implement suitable error handling here
*    ENDIF.

   clear git_hexcont[].

   call function 'GUI_UPLOAD'
     exporting
       filename                      gs_itab-filename
       filetype                      'BIN'
*     HAS_FIELD_SEPARATOR           = ' '
*     HEADER_LENGTH                 = 0
*     READ_BY_LINE                  = 'X'
*     DAT_MODE                      = ' '
*     CODEPAGE                      = ' '
*     IGNORE_CERR                   = ABAP_TRUE
*     REPLACEMENT                   = '#'
*     CHECK_BOM                     = ' '
*     VIRUS_SCAN_PROFILE            =
*     NO_AUTH_CHECK                 = ' '
*   IMPORTING
*     FILELENGTH                    =
*     HEADER                        =
     tables
       data_tab                      git_hexcont
    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.
* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
*         WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
   endif.

   clear git_objhdr[].

   g_docdata-obj_name  'MESSAGE'.
   g_docdata-obj_descr gv_filename.
   g_docdata-obj_langu 'EN'.

   concatenate '&SO_FILENAME=' gv_filename into git_objhdr-line.
   append git_objhdr.
   git_objhdr-line '&SO_FORMAT=BIN'.
   append git_objhdr.

   g_extension gv_ext.

   call function 'SO_DOCUMENT_INSERT_API1'
     exporting
       folder_id                        g_folderid
       document_data                    g_docdata
       document_type                    g_extension
    importing
      document_info                    =  g_docinfo
    tables
      object_header                    git_objhdr
*     object_content                   = git_objcont
     contents_hex                     git_hexcont
*   OBJECT_PARA                      =
*   OBJECT_PARB                      =
    exceptions
      folder_not_exist                 1
      document_type_not_exist          2
      operation_no_authorization       3
      parameter_error                  4
      x_error                          5
      enqueue_error                    6
      others                           7
             .
   if sy-subrc <> 0.
     write:'SO_DOCUMENT_INSERT_API1 SY-SUBRC = 'sy-subrc.
   endif.

   clear g_bizojbg_attachment.

   concatenate gs_itab-bukrs
               gs_itab-belnr
               gs_itab-gjahr into g_bizojb-objkey.
   g_bizojb-objtype 'BKPF'.

*Attachment folder id is in g_docinfo
   g_attachment-objkey  g_docinfo-doc_id.
   g_attachment-objtype 'MESSAGE'.

   call function 'BINARY_RELATION_CREATE'
     exporting
       obj_rolea            g_bizojb
       obj_roleb            g_attachment
       relationtype         'ATTA'
*   FIRE_EVENTS          = 'X'
* IMPORTING
*   BINREL               =
* TABLES
*   BINREL_ATTRIB        =
 EXCEPTIONS
   NO_MODEL             1
   INTERNAL_ERROR       2
   UNKNOWN              3
   OTHERS               4
             .
   if sy-subrc <> 0.
* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
*         WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
   write:/5 gs_itab-bukrs20 gs_itab-belnr35 gs_itab-gjahr50 gv_filename100 'Error in document attachment' COLOR 6.

   else.
"
   write:/5 gs_itab-bukrs20 gs_itab-belnr35 gs_itab-gjahr50 gv_filename100 'Success' COLOR 5.

   endif.

   commit work.

    clear gs_itab.
    endloop.

ENDFORM.

Excel format


Selection screen.



Output :


Check the attachment in FB03












Post a Comment

0 Comments

Total Pageviews