5

SAP GOS Objects Migration – Upload Part 2

 2 years ago
source link: https://blogs.sap.com/2022/09/14/sap-gos-objects-migration-upload-part-2/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client
September 14, 2022 5 minute read

SAP GOS Objects Migration – Upload Part 2

Introduction

This blog is the follow up blog for part 1 on downloading the GOS attachments and now we will be uploading and attaching a file to a specific document.

This is like the reverse process of the downloading GOS attachments.

Flow is as below.

  1. Choose the file from the desktop and this can be done by some standard class methods or FM TMP_GUI_DIRECTORY_LIST_FILES
  2. Upload the file using the FM SO_OBJECT_UPLOAD and you will be getting the content
  3. Create the Folder ID using the FM SO_OBJECT_UPLOAD
  4. Use the Function Module SO_OBJECT_INSERT to insert the object in the repository
  5. Now connect the object to the Document by creating a relationship using the function module BINARY_RELATION_CREATE_COMMIT

If you are looking for creating a log table on the files that are being uploaded using the program, it is pretty simple and you can add the same.

Code as follows

REPORT zbc_gos_attch_upload.

DATA: lt_file_table TYPE filetable,
      lv_dir        TYPE salfile-longname,
      lt_file       TYPE TABLE OF  sdokpath,
      lv_file_count TYPE i,
      lt_dir        TYPE TABLE OF  sdokpath.
DATA: lt_goslog TYPE TABLE OF zgos_log,
      ls_goslog TYPE zgos_log.

DATA: lv_rc          TYPE i,
      lv_action      TYPE i,
      lv_len         TYPE so_doc_len,
      lt_content     TYPE soli_tab,
      ls_fol_id      TYPE soodk,
      ls_obj_id      TYPE soodk,
      ls_obj_data    TYPE sood1,
      lt_objhead     TYPE STANDARD TABLE OF soli,
      lv_ext         TYPE string,
      lv_fname       TYPE string,
      lv_path        TYPE string,
      lv_path1       TYPE string,
      lv_filename    TYPE string,
      ls_note        TYPE borident,
      lv_ep_note     TYPE borident-objkey,
      ls_object      TYPE borident,
      lo_objhead     TYPE REF TO cl_bcs_objhead,
      lv_object_type TYPE soodk-objtp,
      lv_put_to_kpro TYPE sonv-flag,
      i_objectno     TYPE string,
      i_otype        TYPE string,
      lv_filetype    TYPE rlgrap-filetype.
*********************************************************************************************************
***Selection of upload folder
*********************************************************************************************************
SELECTION-SCREEN: BEGIN OF BLOCK b1 WITH FRAME TITLE TEXT-000.
  SELECTION-SCREEN COMMENT 1(30) TEXT-003 FOR FIELD p_path.
  PARAMETERS      : p_path TYPE string LOWER CASE OBLIGATORY.
SELECTION-SCREEN: END   OF BLOCK b1.
*SELECTION-SCREEN: BEGIN OF BLOCK b2 WITH FRAME TITLE TEXT-001.
* SELECTION-SCREEN COMMENT 1(30) TEXT-004 FOR FIELD p_file.
*    PARAMETERS: p_file  TYPE string LOWER CASE OBLIGATORY.
*    PARAMETERS: p_otype TYPE char24 DEFAULT 'BKPF'.
*SELECTION-SCREEN: END   OF BLOCK b2.
************************************************************************
* At Selection-screen on Value-request for P_PATH
************************************************************************
AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_path.
  CALL METHOD cl_gui_frontend_services=>directory_browse
    EXPORTING
      window_title         = 'Select the Folder to upload the files'
    CHANGING
      selected_folder      = p_path
    EXCEPTIONS
      cntl_error           = 1
      error_no_gui         = 2
      not_supported_by_gui = 3
      OTHERS               = 4.
  IF sy-subrc <> 0.
    RETURN.
  ENDIF.
************************************************************************
* At Selection-screen on Value-request for P_FILE
************************************************************************
*AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_file.
*  PERFORM f4_p_file CHANGING p_file.
************************************************************************
* Start of Selection
************************************************************************
START-OF-SELECTION.
DATA: lv_root_dir TYPE salfile-longname.
lv_root_dir = p_path.
  DATA(lv_dir1) = 'C:\Users\VS0005\Desktop\GOS Upload'.
  CALL FUNCTION 'TMP_GUI_DIRECTORY_LIST_FILES'
    EXPORTING
      directory  = lv_root_dir
      filter     = '*.*'
    IMPORTING
      file_count = lv_file_count
*     DIR_COUNT  =
    TABLES
      file_table = lt_file_table
      dir_table  = lt_dir
    EXCEPTIONS
      cntl_error = 1
      OTHERS     = 2.
  IF sy-subrc <> 0.
  ENDIF.
  IF lt_dir IS NOT INITIAL. "Root 1
*Reading of files for each object folder
    LOOP AT lt_dir INTO DATA(ls_dir).
*lv_dir = 'C:\Users\VS0005\Desktop\GOD Testing\1000\660\0048115569'.
*lv_dir = 'C:\Users\VS0005\Desktop\GOD Testing\1000\660\0048115569'.
*CONCATENATE lv_path '\' lv_file INTO lv_dir..
      DATA: lv_fold TYPE salfile-longname.
      CONCATENATE p_path '\' ls_dir-pathname into lv_fold.
      CALL FUNCTION 'TMP_GUI_DIRECTORY_LIST_FILES'
        EXPORTING
          directory  = lv_fold  "ls_dir-pathname
          filter     = '*.*'
        IMPORTING
          file_count = lv_file_count
*         DIR_COUNT  =
        TABLES
          file_table = lt_file_table
          dir_table  = lt_dir
        EXCEPTIONS
          cntl_error = 1
          OTHERS     = 2.
      IF sy-subrc <> 0.
* Implement suitable error handling here
      ENDIF.
*   Logic to add to multiple attachments
      IF lt_file_table IS NOT INITIAL.
        CLEAR: i_objectno,i_otype.
        SPLIT ls_dir-pathname AT '_' INTO i_objectno i_otype.
        LOOP AT lt_file_table ASSIGNING FIELD-SYMBOL(<lfs_file>).
          CLEAR: lv_path, lv_filename, lv_fname, lv_ext, lv_len, lt_content, ls_fol_id,
               ls_obj_data, ls_obj_id, ls_note, lo_objhead, lv_object_type,lv_put_to_kpro,
               lv_filetype, lt_objhead .
          CONCATENATE lv_fold '\' <lfs_file>-filename into lv_path.
          BREAK-POINT.

          TRY .
*         method to split directory and filename
              cl_bcs_utilities=>split_path( EXPORTING iv_path = lv_path IMPORTING ev_path = lv_path1 ev_name = lv_filename ).
*         method to split filename to name & extension
              cl_bcs_utilities=>split_name( EXPORTING iv_name = lv_filename IMPORTING ev_name = lv_fname ev_extension = lv_ext ).
            CATCH cx_bcs.
              CLEAR: lv_path,lv_path1,lv_filename, lv_fname, lv_ext.
          ENDTRY.
          IF lv_path IS NOT INITIAL.
            CALL FUNCTION 'SO_OBJECT_UPLOAD'
              EXPORTING
                filetype                = lv_filetype
                path_and_file           = lv_path
                no_dialog               = 'X'
              IMPORTING
                filelength              = lv_len
                act_filetype            = lv_filetype
                act_objtype             = lv_object_type
                file_put_to_kpro        = lv_put_to_kpro
              TABLES
                objcont                 = lt_content
              EXCEPTIONS
                file_read_error         = 1
                invalid_type            = 2
                x_error                 = 3
                object_type_not_allowed = 4
                kpro_insert_error       = 5
                file_to_large           = 6
                OTHERS                  = 7.
            ##NEEDED
            IF sy-subrc <> 0.
*         Implement suitable error handling here
            ENDIF.
            lo_objhead = cl_bcs_objhead=>create( lt_objhead[] ).
            lo_objhead->set_filename( lv_filename ).
            lo_objhead->set_format( lv_filetype ).
            lo_objhead->set_vsi_profile( cl_bcs_vsi_profile=>get_profile( ) ).
            lt_objhead[] = lo_objhead->mt_objhead.

*       get the folder id where to add attachment for the BO
            ##FM_SUBRC_OK
            CALL FUNCTION 'SO_FOLDER_ROOT_ID_GET'
              EXPORTING
                region    = 'B'
              IMPORTING
                folder_id = ls_fol_id
              EXCEPTIONS
                OTHERS    = 1.
            ##NEEDED
            IF sy-subrc <> 0.

            ENDIF.
            ls_obj_data-objdes   = lv_fname.
            ls_obj_data-file_ext = lv_object_type.
            ls_obj_data-objlen   = lv_len.
            IF NOT lv_put_to_kpro IS INITIAL.
              ls_obj_data-extct = 'K'.
            ENDIF.

*       add the attachment data to the folder
            ##FM_SUBRC_OK
            CALL FUNCTION 'SO_OBJECT_INSERT'
              EXPORTING
                folder_id             = ls_fol_id
                object_type           = 'EXT'
                object_hd_change      = ls_obj_data
              IMPORTING
                object_id             = ls_obj_id
              TABLES
                objhead               = lt_objhead
                objcont               = lt_content
              EXCEPTIONS
                active_user_not_exist = 35
                folder_not_exist      = 6
                object_type_not_exist = 17
                owner_not_exist       = 22
                parameter_error       = 23
                OTHERS                = 1000.
            ##NEEDED
            IF sy-subrc <> 0.

            ENDIF.
            ls_object-objkey  = i_objectno. "'1000238392'.
            "gs_lporb-instid.
            ls_object-objtype = i_otype. "'BUS2091'.
            "gs_lporb-typeid.
            ls_note-objtype   = 'MESSAGE'.

            CONCATENATE ls_fol_id-objtp ls_fol_id-objyr ls_fol_id-objno ls_obj_id-objtp ls_obj_id-objyr ls_obj_id-objno INTO ls_note-objkey.

*       link the folder data and BO for attachment in gos
            ##FM_SUBRC_OK
            CALL FUNCTION 'BINARY_RELATION_CREATE_COMMIT'
              EXPORTING
                obj_rolea    = ls_object
                obj_roleb    = ls_note
                relationtype = 'ATTA'
              EXCEPTIONS
                OTHERS       = 1.
            ##NEEDED
            IF sy-subrc <> 0.

            ENDIF.


          ENDIF.
          CLEAR <lfs_file>.
            CLEAR: ls_goslog.
        ls_goslog-busobj = i_otype.
        ls_goslog-cat = 'U'.
        ls_goslog-ddate = sy-datum.
        ls_goslog-dtime = sy-uzeit.
        ls_goslog-filename = lv_fname.
        ls_goslog-filetype = lv_object_type.
        ls_goslog-objectid = i_objectno.
        ls_goslog-userid = sy-uname.
        APPEND ls_goslog TO lt_goslog.
        TRY.
            MODIFY zgos_log FROM TABLE lt_goslog.
          CATCH cx_sy_open_sql_db INTO DATA(cx_sy_ref_is_initial).
        ENDTRY.
*        INSERT  zgos_log FROM TABLE lt_goslog.
        COMMIT WORK.
        CLEAR: ls_goslog.
        ENDLOOP.
      ENDIF.
      clear:ls_dir.
    ENDLOOP.
  ENDIF.
*&---------------------------------------------------------------------*
*& Form f4_p_file
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*&      <-- P_FILE
*&---------------------------------------------------------------------*
FORM f4_p_file CHANGING p_upfile TYPE string..
*----------------------------------------------------------------------*
* Description:
* -----------
* -
*----------------------------------------------------------------------*

************************************************************************
* Declaration                                                          *
************************************************************************

*--- Local internal tables --------------------------------------------*
  DATA: lt_dynp_values TYPE TABLE OF dynpread,
        lt_fields      TYPE dynpread_tabtype,
        lt_files       TYPE filetable.

*--- Local stuctures --------------------------------------------------*
  DATA: ls_field  LIKE LINE OF lt_fields.

*--- Local variables --------------------------------------------------*
  DATA: lv_repid  TYPE syrepid.


************************************************************************
* Programming                                                          *
************************************************************************
  lv_repid = sy-repid.

  CALL FUNCTION 'DYNP_VALUES_READ'
    EXPORTING
      dyname               = lv_repid
      dynumb               = '1000'
      request              = 'A'
    TABLES
      dynpfields           = lt_fields
    EXCEPTIONS
      invalid_abapworkarea = 01
      invalid_dynprofield  = 02
      invalid_dynproname   = 03
      invalid_dynpronummer = 04
      invalid_request      = 05
      no_fielddescription  = 06
      undefind_error       = 07.

  IF sy-subrc = 0.
    READ TABLE lt_fields
      INTO ls_field
      WITH KEY fieldname = 'P_FILE'.
    IF sy-subrc = 0.
      p_upfile = ls_field-fieldvalue.

      cl_gui_frontend_services=>file_open_dialog(
        EXPORTING
          default_filename        = p_upfile
          file_filter             = zcl_excel_common=>c_xlsx_file_filter
        CHANGING
          file_table              = lt_files
          rc                      = sy-tabix
        EXCEPTIONS
          OTHERS                  = 1 ).

      IF sy-subrc = 0.
        READ TABLE lt_files
          INDEX 1
          INTO p_upfile.
        IF sy-subrc <> 0.
          CLEAR : p_upfile.
        ENDIF.
      ENDIF.
    ENDIF.
  ENDIF.

ENDFORM.

In the above program we tried uploading all the document under a folder and folder name is the combinqtion of Document number and the Object Type e.g 100025445565852022-BKPF

This way it is easier to upload lot of files pertaining to a document.

Thanks and Regards,

Vivek


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK