2023 Feb 23 3:03 PM
Hi Experts,
I was trying to add attachments to a fi document via program and tried the following code and succeeds. The document get attached but while opening the attachments if the type is docs or xlsx there is an error message saying the file is corrupted. We can retrieve the file but is there any way that we can avoid the error message popup. I tried everything found many similar questions and problems but none could help. The common point found was the object size we are passing is not correct .
attaching the code here , So is there any way that we could fetch correct file size
* Get file name and extension
CALL FUNCTION 'CH_SPLIT_FILENAME'
EXPORTING
complete_filename = v_file
IMPORTING
extension = lv_extension
name_with_ext = lv_filename
EXCEPTIONS
invalid_drive = 1
invalid_path = 2
OTHERS = 3.
IF sy-subrc <> 0.
** MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
** WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ENDIF.
CALL FUNCTION 'GUI_UPLOAD'
EXPORTING
filename = v_file
filetype = 'BIN'
IMPORTING
filelength = output
TABLES
data_tab = it_content[]
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.
CALL FUNCTION 'SO_FOLDER_ROOT_ID_GET'
EXPORTING
region = 'B'
IMPORTING
folder_id = ls_fol_id
EXCEPTIONS
OTHERS = 1.
ls_obj_data-objsns = 'O'.
ls_obj_data-objla = sy-langu.
ls_obj_data-objdes = 'Wire Instructions'.
ls_obj_data-file_ext = lv_extension.
CLEAR wa_content.
DESCRIBE TABLE it_content LINES lv_lines.
READ TABLE it_content INTO wa_content INDEX lv_lines.
ls_obj_data-objlen = ( lv_lines - 1 ) * 255 + strlen( wa_content ).
CONDENSE ls_obj_data-objlen.
"Object header
CLEAR wa_content.
CONCATENATE '&SO_FILENAME=' lv_filename INTO wa_content.
APPEND wa_content TO it_objhead.
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 = it_objhead
objcont = it_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.
IF sy-subrc = 0 AND ls_object-objkey IS NOT INITIAL.
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_obj_id-objtp.
ls_folmem_k-docyr = ls_obj_id-objyr.
ls_folmem_k-docno = ls_obj_id-objno.
lv_ep_note = ls_folmem_k.
ls_note-objtype = 'MESSAGE'.
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.
ELSE.
MESSAGE 'Attachment Creation Failed' TYPE 'S' DISPLAY LIKE 'E'.
RETURN.
ENDIF.
IF sy-subrc = 0.
MESSAGE 'Attachment Created' TYPE 'S'.
ELSE.
MESSAGE 'Attachment Creation Failed' TYPE 'S' DISPLAY LIKE 'E'.
ENDIF.
ENDIF
ENDIIF.
2023 Feb 23 4:55 PM
In the component OBJLEN of the parameter object_hd_change of SO_OBJECT_INSERT, you should indicate the actual number of bytes of the file, not an arbitrary number.
In your code, the actual number of bytes is stored in the variable OUTPUT (you have given a weird name).
But for some reason, you do a very strange calculation which is useless and completely incorrect (ls_obj_data-objlen = ( lv_lines - 1 ) * 255 + strlen( wa_content ).)
So, in your code it should be:
ls_obj_data-objlen = output.
2023 Feb 24 4:29 AM
Hi Sandra, Thank you so much it worked.
I was trying different options to get the size. That's calculation was one among them.
I didn't used that file length option in the fm before. I recently added it. I used that variable value Objlen also . But I think I was careless I thought it didn't worked( ya the variable name is weired🚶 just given a variable i already declared since it has same type 🤭. I forgot to remove that part while pasting here. Turns out to be it actually helped