Let’s get started on Sample program to attach any file from application server to any Business Object.
* Sample program to attach documents to any business object
REPORT z_gos_attachment NO STANDARD PAGE HEADING
LINE-COUNT 132.
*———————————————————————*
* SELECTION SCREEN *
*———————————————————————*
SELECTION-SCREEN BEGIN OF BLOCK b1 WITH FRAME TITLE text-001.
PARAMETERS: p_file TYPE localfile, ” Application File Path
p_objid TYPE swo_typeid, ” Any object like material no/vendor
“/customer/po/pr etc
p_bo TYPE swo_objtyp. ” Business object like LFA1 for vendor
SELECTION-SCREEN END OF BLOCK b1.
DATA:
li_content TYPE STANDARD TABLE OF soli,
li_objhead TYPE STANDARD TABLE OF soli,
lwa_folmem_k TYPE sofmk,
lwa_note TYPE borident,
lwa_object TYPE borident,
lwa_obj_id TYPE soodk,
lwa_content TYPE soli,
lwa_fol_id TYPE soodk,
lwa_obj_data TYPE sood1,
lv_ep_note TYPE borident-objkey,
lv_lifnr TYPE lifnr,
lv_file TYPE string,
lv_filename TYPE c LENGTH 100, ” file name and ext
lv_extension TYPE c LENGTH 4. ” extension only
* Refresh data
REFRESH: li_content[], li_objhead[].
* Open corresponding DMS files
OPEN DATASET p_file FOR INPUT IN BINARY MODE.
IF sy-subrc EQ 0.
WHILE sy-subrc = 0.
READ DATASET p_file INTO lwa_content.
* Do not put Sy-subrc eq 0 here. Please add the last line of the file,
* though sy-subrc may fail
APPEND lwa_content TO li_content.
ENDWHILE.
* Close file
CLOSE DATASET p_file.
* In this example we are attaching in Vendor.
* Converting according to your requirement.. For example for Material,
* it should be 18 chars
* Convert Vendor
CALL FUNCTION ‘CONVERSION_EXIT_ALPHA_INPUT’
EXPORTING
input = p_objid
IMPORTING
output = lv_lifnr.
* Convert to BIN
CALL FUNCTION ‘SO_CONVERT_CONTENTS_BIN’
EXPORTING
it_contents_bin = li_content[]
IMPORTING
et_contents_bin = li_content[].
* Get folder id
CALL FUNCTION ‘SO_FOLDER_ROOT_ID_GET’
EXPORTING
region = ‘B’
IMPORTING
folder_id = lwa_fol_id
EXCEPTIONS
communication_failure = 1
owner_not_exist = 2
system_failure = 3
x_error = 4
OTHERS = 5.
* Sy-subrc check not required
* Keeping file in string data type
lv_file = p_file.
* You may not need this step. But no harm in adding this
* Get file name and extension
CALL FUNCTION ‘CH_SPLIT_FILENAME’
EXPORTING
complete_filename = lv_file
IMPORTING
extension = lv_extension
name_with_ext = lv_filename
EXCEPTIONS
invalid_drive = 1
invalid_path = 2
OTHERS = 3.
IF sy-subrc EQ 0.
* Object header
CLEAR lwa_content.
CONCATENATE ‘&SO_FILENAME=’ lv_filename INTO lwa_content.
APPEND lwa_content TO li_objhead.
CLEAR lwa_content.
ENDIF.
lwa_object-objkey = lv_lifnr.
* For example, business object name for PO is BUS2012,
* business object for PR is BUS2105,
* business object for Vendor is LFA1 etc
lwa_object-objtype = p_bo.
* lwa_object-logsys = lv_logical_system.
lwa_obj_data-objsns = ‘O’.
lwa_obj_data-objla = sy-langu.
lwa_obj_data-objdes = ‘Attachment by Raju Shrestha’. .
lwa_obj_data-file_ext = lv_extension.
TRANSLATE lwa_obj_data-file_ext TO UPPER CASE.
* This is very important step. If your object size does not match with the input
* file size, then your object might get attached, but it will show error while you
* try to open it.
* If you have a way, where you can read the input file size directly, then assign
* it directly else, use the below formula
lwa_obj_data-objlen = lines( li_content ) * 255.
* Insert data
CALL FUNCTION ‘SO_OBJECT_INSERT’
EXPORTING
folder_id = lwa_fol_id
object_type = ‘EXT’
object_hd_change = lwa_obj_data
IMPORTING
object_id = lwa_obj_id
TABLES
objhead = li_objhead
objcont = li_content
EXCEPTIONS
active_user_not_exist = 1
communication_failure = 2
component_not_available = 3
dl_name_exist = 4
folder_not_exist = 5
folder_no_authorization = 6
object_type_not_exist = 7
operation_no_authorization = 8
owner_not_exist = 9
parameter_error = 10
substitute_not_active = 11
substitute_not_defined = 12
system_failure = 13
x_error = 14
OTHERS = 15.
IF sy-subrc = 0 AND lwa_object-objkey IS NOT INITIAL.
lwa_folmem_k-foltp = lwa_fol_id-objtp.
lwa_folmem_k-folyr = lwa_fol_id-objyr.
lwa_folmem_k-folno = lwa_fol_id-objno.
* Please note: lwa_fol_id and lwa_obj_id are different work areas
lwa_folmem_k-doctp = lwa_obj_id-objtp.
lwa_folmem_k-docyr = lwa_obj_id-objyr.
lwa_folmem_k-docno = lwa_obj_id-objno.
lv_ep_note = lwa_folmem_k.
lwa_note-objtype = ‘MESSAGE’.
* lwa_note-logsys = lv_logical_system.
lwa_note-objkey = lv_ep_note.
* Link it
CALL FUNCTION ‘BINARY_RELATION_CREATE_COMMIT’
EXPORTING
obj_rolea = lwa_object
obj_roleb = lwa_note
relationtype = ‘ATTA’
EXCEPTIONS
no_model = 1
internal_error = 2
unknown = 3
OTHERS = 4.
IF sy-subrc EQ 0.
* Commit it
COMMIT WORK.
WRITE:/ ‘Attached successfully’.
ENDIF.
ELSE.
MESSAGE ‘Error while opening file’ TYPE ‘I’.
LEAVE LIST-PROCESSING.
ENDIF.
ENDIF.
YOU MAY LIKE THIS
Steampunk Chronicles: Navigating the Cloud with SAP BTP ABAP Environment
Tips for Building Custom SAP Applications: A Comprehensive Guide
SAP ABAP on HANA Interview Questions: Mastering the Essentials