cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

Facing Issue in GOS Attachment for MM03

SanketGode45
Explorer
0 Likes
1,030

This Code works fine without any error but there is no attachment against a given material in mm03 ,

I was able to resolve the issue anyone can refer this code for the same now and onward

*&---------------------------------------------------------------------*
*& Report ZZ_TEST_GOC
*&---------------------------------------------------------------------*
*&
*&---------------------------------------------------------------------*
REPORT zz_test_goc.

DATA: lv_matnr      TYPE matnr,
      lv_xstring    TYPE xstring,
      lv_objlen     TYPE i,
      lt_solix      TYPE solix_tab,
      lt_obj_cont   TYPE soli_tab,
      lt_obj_head   TYPE soli_tab,
      ls_hd_dat     TYPE sood1,
      ls_fol_id     TYPE soodk,
      ls_doc_id     TYPE soodk,
      ls_object     TYPE borident,
      ls_new_fm_dat TYPE sofm2,
      ls_new_hd_dat TYPE sood2,
      ls_note       TYPE borident,
      ls_object_a   TYPE borident,
      ls_object_b   TYPE borident,
      lv_log_handle TYPE balloghndl,
      ls_folmem_k   TYPE sofmk,
      lv_ep_note    TYPE borident-objkey,
      lt_return     TYPE STANDARD TABLE OF bapiret2,
      lv_msg_text   TYPE string.


DATA : gv_name           TYPE pcfile-path,
       gv_ext            TYPE char4,
       gv_path           TYPE string,
       gv_filename1      TYPE string,
       gv_down           TYPE string,
       gv_bin_file       TYPE xstring,
       gv_key            TYPE char8,
       gv_vbeln          TYPE vbeln,
       gv_msgty          TYPE msgty,
       gv_message        TYPE string,
       gv_len            TYPE i,
       gt_tab            TYPE tsfixml,
       gv_str            TYPE string,
       gv_filename       TYPE string,
       gv_filename_dummy TYPE string.

DATA: gs_exc           TYPE REF TO cx_root,
      gt_return        TYPE STANDARD TABLE OF bapiret2,
      gt_per_info      TYPE STANDARD TABLE OF smum_xmltb,
      gs_per_info      TYPE smum_xmltb,
      gt_bindata       TYPE solix_tab,
      gv_form          TYPE xstring,
      gv_output_length TYPE i.

SELECTION-SCREEN BEGIN OF BLOCK b1 WITH FRAME.
  PARAMETERS : p_upload TYPE rlgrap-filename OBLIGATORY,
               P_FILE TYPE CHAR10.
SELECTION-SCREEN END OF BLOCK b1.

CALL METHOD cl_gui_frontend_services=>file_save_dialog
  EXPORTING
    default_extension    = 'PDF'
    file_filter          = cl_gui_frontend_services=>filetype_all
  CHANGING
    filename             = gv_filename_dummy
    path                 = gv_filename_dummy
    fullpath             = gv_filename
  EXCEPTIONS
    cntl_error           = 1
    error_no_gui         = 2
    not_supported_by_gui = 3
    OTHERS               = 4.


AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_upload .

  CALL FUNCTION 'F4_FILENAME'
    IMPORTING
      file_name = p_upload.

AT SELECTION-SCREEN OUTPUT.

  IF p_upload IS NOT INITIAL.

    CALL FUNCTION 'SO_SPLIT_FILE_AND_PATH'
      EXPORTING
        full_name     = p_upload
      IMPORTING
        stripped_name = gv_name
        file_path     = gv_path
      EXCEPTIONS
        x_error       = 1
        OTHERS        = 2.

    CALL FUNCTION 'PC_SPLIT_COMPLETE_FILENAME'
      EXPORTING
        complete_filename = gv_name
      IMPORTING
        extension         = gv_ext
      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.

    TRANSLATE gv_ext TO UPPER CASE.

    IF gv_ext NE 'PDF'.
      MESSAGE 'Selected file is not PDF.' TYPE 'W' DISPLAY LIKE 'E'.
    ENDIF.

  ENDIF.

START-OF-SELECTION.

  IF p_upload IS NOT INITIAL.

    gv_filename1 = p_upload.

    CALL METHOD cl_gui_frontend_services=>gui_upload
      EXPORTING
        filename   = gv_filename1
        filetype   = 'BIN'
      IMPORTING
        filelength = gv_output_length
      CHANGING
        data_tab   = gt_bindata[].

    IF sy-subrc = 0.

      CALL FUNCTION 'SCMS_BINARY_TO_XSTRING'
        EXPORTING
          input_length = gv_output_length
        IMPORTING
          buffer       = gv_bin_file
        TABLES
          binary_tab   = gt_bindata
        EXCEPTIONS
          failed       = 1
          OTHERS       = 2.
    ENDIF.
  ENDIF.
  lv_matnr = 'DS-13D809-260'.
*lv_matnr = '0029800NCED'.
  lv_xstring = gv_bin_file.
  " Convert MATNR to 18-char format
  CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'
    EXPORTING
      input  = lv_matnr
    IMPORTING
      output = lv_matnr.

  " Get SAPOffice folder
  CALL FUNCTION 'SO_FOLDER_ROOT_ID_GET'
    EXPORTING
      region    = 'B'
    IMPORTING
      folder_id = ls_fol_id.

  " Convert XSTRING to SOLIX
  CALL FUNCTION 'SCMS_XSTRING_TO_BINARY'
    EXPORTING
      buffer        = lv_xstring
    IMPORTING
      output_length = lv_objlen
    TABLES
      binary_tab    = lt_solix.

  " Convert SOLIX to SOLITAB
  CALL FUNCTION 'SO_SOLIXTAB_TO_SOLITAB'
    EXPORTING
      ip_solixtab = lt_solix
    IMPORTING
      ep_solitab  = lt_obj_cont.

  " Prepare document metadata
  ls_hd_dat-objdes   = P_FILE.
  ls_hd_dat-objlen   = lv_objlen.
  ls_hd_dat-objpri   = '5'.
  ls_hd_dat-file_ext = 'PDF'. " or PDF, etc.

  APPEND VALUE #( line = |&SO_FILENAME=Uploaded_File.PDF| ) TO lt_obj_head.
  APPEND VALUE #( line = |&SO_FORMAT=BIN| ) TO lt_obj_head.

  " Insert document
  CALL FUNCTION 'SO_OBJECT_INSERT'
    EXPORTING
      folder_id         = ls_fol_id
      object_hd_change  = ls_hd_dat
      object_type       = 'EXT'
      owner             = sy-uname
    IMPORTING
      object_id         = ls_doc_id
      object_fl_display = ls_new_fm_dat
      object_hd_display = ls_new_hd_dat
    TABLES
      objcont           = lt_obj_cont
      objhead           = lt_obj_head.

  " Link document to material (GOS)
  ls_object-objtype = 'BUS1001006'.
  ls_object-objkey  = lv_matnr.

  SELECT SINGLE logsys INTO ls_object-logsys FROM t000 WHERE mandt = sy-mandt.
  "ls_object-logsys = sy-sysid.
  ls_folmem_k-foltp = ls_fol_id-objtp.
  ls_folmem_k-folyr = ls_fol_id-objyr.
  ls_folmem_k-folno = ls_fol_id-objno.
  ls_folmem_k-doctp = ls_doc_id-objtp.
  ls_folmem_k-docyr = ls_doc_id-objyr.
  ls_folmem_k-docno = ls_doc_id-objno.

  lv_ep_note        = ls_folmem_k.
  ls_note-objtype   = 'MESSAGE'.
  ls_note-logsys = ls_object-logsys.
  ls_note-objkey    = lv_ep_note.

  CALL FUNCTION 'BINARY_RELATION_CREATE_COMMIT'
    EXPORTING
      obj_rolea    = ls_object
      obj_roleb    = ls_note
      relationtype = 'ATTA'
    EXCEPTIONS
      OTHERS       = 1.


  IF sy-subrc <> 0.
    " Step 3: Create BAL log
    CALL FUNCTION 'BAL_LOG_CREATE'
      EXPORTING
        object     = 'ZMAT_ATTACH'
        subobject  = 'ATTACHMENT'
      IMPORTING
        log_handle = lv_log_handle.

    " Step 4: Add error message
    CALL FUNCTION 'BAL_LOG_MSG_ADD'
      EXPORTING
        log_handle = lv_log_handle
        msgid      = 'ZMSG'         " Your custom message class
        msgno      = '001'          " Message number
        msgty      = 'E'
        msgv1      = 'Attachment failed for material'
        msgv2      = lv_matnr.

    " Step 5: Save log
    CALL FUNCTION 'BAL_DB_SAVE'
      EXPORTING
        log_handle = lv_log_handle.
  ELSE.
    CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
      EXPORTING
        wait = 'X'.
    MESSAGE 'Document successfully linked to material' TYPE 'S'.
  ENDIF.

 

Accepted Solutions (1)

Accepted Solutions (1)

cyril_sayeh
Explorer

Hello @SanketGode45 

Instead of custom file upload handling, you can use the standard class CL_GOS_DOCUMENT_SERVICE for uploading GOS documents. The CREATE_ATTACHMENT method already manages imports from PC. Please refer to the sample code below:

*&---------------------------------------------------------------------*

*& Report ZFIX_ERROR_UPLOAD

*&

*&---------------------------------------------------------------------*

*&

*&

*&---------------------------------------------------------------------*

REPORT zfix_error_upload.

" ---------------------------------------------------------------------

" 10: Data Declaration

" ---------------------------------------------------------------------

DATA lo_gos_document TYPE REF TO cl_gos_document_service.

DATA ls_object TYPE borident.

DATA lf_atta TYPE borident-objkey.

DATA lf_brelguid TYPE srgbtbrel-brelguid.

" ---------------------------------------------------------------------

" 20: Selection screen

" ---------------------------------------------------------------------

SELECTION-SCREEN BEGIN OF BLOCK b1 WITH FRAME TITLE t1.

PARAMETERS p_matnr TYPE mara-matnr OBLIGATORY.

SELECTION-SCREEN END OF BLOCK b1.

" ---------------------------------------------------------------------

" 30: Initialization

" ---------------------------------------------------------------------

INITIALIZATION.

lo_gos_document = NEW cl_gos_document_service( ).

" ---------------------------------------------------------------------

" 40:Start of selection

" ---------------------------------------------------------------------

START-OF-SELECTION.

" ---------------------------------------------------------------------

" 50: Obj. key

" ---------------------------------------------------------------------

ls_object = VALUE #( objtype = 'BUS1001006'

objkey = p_matnr ).

" ---------------------------------------------------------------------

" 60: GOS - Create attachment

" *********************************************************************

lo_gos_document->create_attachment( EXPORTING is_object = ls_object

IMPORTING ep_attachment = lf_atta ).

" ---------------------------------------------------------------------

" 70: Check attachment is returned

" *********************************************************************

IF xsdbool( lf_atta IS INITIAL ) = abap_false.

COMMIT WORK AND WAIT.

" ---------------------------------------------------------------------

" 80: Read obj. table

" *********************************************************************

SELECT SINGLE brelguid FROM srgbtbrel

INTO lf_brelguid

WHERE instid_b = lf_atta.

" ---------------------------------------------------------------------

" 90: Success

IF xsdbool( lf_brelguid IS NOT INITIAL ) = abap_true.

MESSAGE 'Document successfully linked to material' TYPE 'S'.

ENDIF.

ELSE.

" handle failed keys

ENDIF.

 

SanketGode45
Explorer

Hi @cyril_sayeh, thank you for the approach you suggested 👍, I was able to find the bug it was folder and file extension parameters now i have updated the code it is working.

Answers (1)

Answers (1)

juveria_sap_integrity
Active Participant
0 Likes

This usually happens because the attachment is created but not correctly linked to the material object, so MM03 doesn’t show it. For GOS on materials, the object type must be BUS1001006 and the material number has to be in 18‑char internal format. Also make sure the folder, file extension, and header data are filled correctly before calling the link (ATTA). An easier and safer option is to use the standard class CL_GOS_DOCUMENT_SERVICE → create_attachment, which handles the upload and linking automatically. Once the link exists in SRGBTBREL, the attachment will show up in MM03.