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

Create GOS attachment from xstring document

Missschaaa
Participant
19,218

Hello everyone,

I'm now facing a problem which is well discussed here and in other forums. In spite of reading lots of tutorials, blogs and other questions here I was not able to solve it. I'm trying to upload a document (pdf, jpg, xlsx, docx) and attach it as GOS document to a business object. I'm doing this in FIORI app context so the uploaded file will imported to me as an xstring. So all I have to do is put this xstring into GOS document. I followed the guidelines here and tried it like this.

* Get folder id
  CALL FUNCTION 'SO_FOLDER_ROOT_ID_GET'
    EXPORTING
      region                = 'B'
    IMPORTING
      folder_id             = ls_fol_id
    EXCEPTIONS
      communication_failure = 1
      owner_not_exist       = 2
      system_failure        = 3
      x_error               = 4
      OTHERS                = 5.

* To get the extension from the file name
  IF iv_slug IS NOT INITIAL.
    SPLIT iv_slug AT '/' INTO lv_qmnum lv_file.
    CALL FUNCTION 'TRINT_FILE_GET_EXTENSION'
      EXPORTING
        filename  = lv_file
        uppercase = 'X'
      IMPORTING
        extension = lv_extensions.
  ENDIF.


* Transfer Importing Xstring to binary solix table
lt_solix = cl_bcs_convert=>xstring_to_solix( iv_xstring = is_media_resource-value ).

* Fill Doc Data
*  ls_doc_data-doc_size = XSTRLEN( is_media_resource-value ).
  ls_doc_data-obj_descr = 'TEST'.
  ls_doc_data-obj_name = lv_file.
  MOVE lv_extensions TO ld_doc_type.

  ls_object-objkey     =  lv_qmnum.
  ls_object-objtype    = 'BUS2038'.

* Create Document
  CALL FUNCTION 'SO_DOCUMENT_INSERT_API1'
    EXPORTING
      folder_id                  = ls_fol_id
      document_data              = ls_doc_data
      document_type              = ld_doc_type
    IMPORTING
      document_info              = ls_doc_info
    TABLES
      contents_hex               = lt_solix
    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.

* To link the document attachment and business object
  IF sy-subrc = 0 AND ls_object-objkey IS NOT INITIAL.
    ls_object-instid  = lv_qmnum.
    ls_object-typeid  = 'BUS2038'.
    ls_object-catid   = 'BO'.
    ls_objtgt-instid =  ls_doc_info-doc_id.
    ls_objtgt-typeid  = 'MESSAGE'.
    ls_objtgt-catid   = 'BO'.

    TRY.
        cl_binary_relation=>create_link(
          EXPORTING
            is_object_a = ls_object
            is_object_b = ls_objtgt
            ip_reltype  = 'ATTA' ).
        COMMIT WORK AND WAIT.
      CATCH cx_obl_parameter_error cx_obl_model_error cx_obl_internal_error.
    ENDTRY.
  ENDIF.

Can anyone tell me what I did wrong? File gets transfered and uploaded correctly to business object, but when trying to open it, it only works for pdf files. All the other document types get opened with error that data is corrupt or document type gets not supported. Document type can not be the problem because I uploaded the files manually and can open them, so it must have to do with the creation here.

I tried several things, using the 'SO_OBJECT_INSERT' function instead, using the 'SCMS_XSTRING_TO_BINARY' function for convert instead, working with '&SO_FILENAME=' etc., working with document size. Nothing helped. Evertytime I got the same error when opening the uploaded document.

In some blogs it says that it has to do with SAP ECC 6 and unicode system that many user have problems, I have that too and dont know how to solve it. Maybe any ideas here?

Thanks a lot

Regards Michael

View Entire Topic
Missschaaa
Participant
0 Likes

For the record what happens when opening the files.

If I open the docx file it says that content is not readable and asks if I want to correct it. When saying yes, file will be opened correctly.

If I open the xlsx file it says file is damaged or file type invalid and I canot open it.

If I open jpg file it says data format not supported.

If I open pdf file it can be opened correctly.

Sandra_Rossi
Active Contributor
0 Likes

Please use the COMMENT button for comments, questions, adding details, replying to a comment or a proposed solution or to the OP question, etc., ANSWER is only to propose a solution, dixit SAP text at the right of the answer area.

Jakn
Active Participant
0 Likes
✔️ Root cause is missing OBJECT_HEADER metadata, not the XSTRING itself. For non‑PDF files (DOCX/XLSX/JPG), &SO_FILENAME, &SO_FORMAT=BIN and correct DOC_SIZE (XSTRLEN) are mandatory in SO_DOCUMENT_INSERT_API1. Once OBJECT_HEADER is filled correctly, attachments open without corruption — even when XSTRING comes from Fiori/OData.