Application Development Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 

Attachments in GOS does not appear in FB03 when uploaded through custom code.

naveenmaaroju
Explorer
0 Kudos

Hi Experts,

We have a business requirement to upload the attachments into FB03 through the custom code. so i have written an custom FM to upload the attachments in FB03 using the reference of below thread.

https://archive.sap.com/discussions/thread/1485230

When i execute the FM there is no issue or error message but i see no attachment visible in the FB03 transaction for the FI document. I can see even the relationship is created in the table srgbtbrel

Could you please suggest me if i am missing anything, that is making the attachments invisible in the GOS?

Below is the code i have written.

FUNCTION zgos_create .
*"----------------------------------------------------------------------
*"*"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 OPTIONAL
*" EXCEPTIONS
*" UNKNOWN_ERROR
*" NO_FILE_OR_BIN
*" NO_DOC_TYPE
*"----------------------------------------------------------------------

INCLUDE : <cntn01>.

CONSTANTS: c_docnm TYPE borident-objtype VALUE 'MESSAGE'.

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'
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 = 'ATTA'
EXCEPTIONS
OTHERS = 1.
CASE sy-subrc.
WHEN 0.
COMMIT WORK.
MESSAGE s043(sgos_msg). " The attachment was successfully created
* RAISE EVENT commit_required.
* RAISE EVENT service_succeeded
* EXPORTING eo_service = me.
WHEN OTHERS.
MESSAGE s042(sgos_msg). " The attachment has not been created
ENDCASE.

* Publish the attachment
CALL FUNCTION 'SWU_OBJECT_PUBLISH'
EXPORTING
objtype = objtype
objkey = objkey
EXCEPTIONS
objtype_not_found = 1
OTHERS = 2.
IF sy-subrc <> 0.
* Implement suitable error handling here
ENDIF.

ENDFUNCTION.

1 ACCEPTED SOLUTION

Jelena
Active Contributor

As Michelle correctly pointed out, even if a table entry has been created but you've attached to a wrong object then it won't work.

There should be more recent examples of attaching files using GOS, the post you're referring to is from 2009. We should be using classes these days. Google: "standard class GOS site:sap.com", there are tons of blogs out there.

For more general information I can suggest reading my old post because it explains in plain language how the whole process works. But don't just copy-paste the code from there because it's outdated (predates even the referenced post).

5 REPLIES 5

mmcisme1
Active Contributor
0 Kudos

What is the values for:

objtype

objkey?

Thank you,

Michelle

Former Member

More questions:

- Your really want to create attachment like GOS > Create > Create attachment or
want to have attachment like GOS > Create > Store business document ?

- If you read comments in your posted link, the file length handling might only work for text files, but not for binary files
(the whole part with GUI_UPLOAD and converting BIN_TAB should be rewritten)

- objkey might be wrong composed, if company code is shorter than four characters

- objkey might be wrong composed, if number range is shorter than 10 characters

Jelena
Active Contributor

As Michelle correctly pointed out, even if a table entry has been created but you've attached to a wrong object then it won't work.

There should be more recent examples of attaching files using GOS, the post you're referring to is from 2009. We should be using classes these days. Google: "standard class GOS site:sap.com", there are tons of blogs out there.

For more general information I can suggest reading my old post because it explains in plain language how the whole process works. But don't just copy-paste the code from there because it's outdated (predates even the referenced post).

naveenmaaroju
Explorer
0 Kudos

It is resolved. The problem was with the objkey.

0 Kudos

This still shows as unanswered question. Kindly close it, see this blog.