ตัวอย่าง Code สำหรับแนบไฟล์จาก PC ไปใส่ในเอกสาร(เช่น Material Document)
REPORT zdemo_attach.
*Declarations
*Structure Declarations
TYPES : BEGIN OF ty_table, “Structure for FileName
fname(128) TYPE c,
END OF ty_table.
*Data Declarations
DATA: w_prog TYPE sy-repid, “Current Program Name
w_dynnr TYPE sy-dynnr, “Current Dynpro Number
w_attachement TYPE borident, “Work Area for BOR object identifier
ws_borident TYPE borident, “Work Area for BOR object identifier
w_document TYPE sood4, “Interface for send screen and MOM
folder_id TYPE soodk, “Definition of Object Key
w_h_data TYPE sood2, “Object Definition Workarea
w_fol_data TYPE sofm2, “Folder Contents Work area
w_rec_data TYPE soos6, “Transfer Information of folder Work area
ws_files TYPE ty_table,
wt_files TYPE TABLE OF ty_table.
SELECTION-SCREEN BEGIN OF BLOCK b1 WITH FRAME TITLE text-001.
*Parameter Declarations
PARAMETER: p_mandt TYPE sy-mandt DEFAULT sy-mandt, ” Client Number
p_mblnr TYPE mblnr,
p_mjahr TYPE mjahr, ” Sales Order Number
p_path TYPE ibipparms-path MEMORY ID ad_local_path, ” File Path
p_name(30). ” Name of attachement.
SELECTION-SCREEN END OF BLOCK b1.
*Initialization Event
INITIALIZATION .
w_prog = sy-repid .
w_dynnr = sy-dynnr .
*/ Selection Screen For File Path Selection
AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_path .
*/ F4 Help for file selection
CALL FUNCTION ‘F4_FILENAME’
EXPORTING
program_name = w_prog
dynpro_number = w_dynnr
field_name = ‘P_PATH’
IMPORTING
file_name = p_path.
*Start of selection Evvent
START-OF-SELECTION .
*/Client Validations
IF sy-mandt NE p_mandt .
WRITE ‘Mandt Error’ .
EXIT .
ENDIF .
*/ Assign Object Keys to the Structure BOR
CONCATENATE p_mblnr p_mjahr INTO ws_borident-objkey. “SalesOrder Number
*ws_borident-objtype = ‘EQUI’. “Object Type
ws_borident-objtype = ‘BUS2017’. “BUS Number
*/ Filename Assign to the Structure
ws_files-fname = p_path . “Path
APPEND ws_files TO wt_files .
*/ Folder Root
CALL FUNCTION ‘SO_FOLDER_ROOT_ID_GET’
EXPORTING
region = ‘B’
IMPORTING
folder_id = folder_id
EXCEPTIONS
OTHERS = 1.
*/ Append data to the MOM Structure
w_document-foltp = folder_id-objtp.
w_document-folyr = folder_id-objyr .
w_document-folno = folder_id-objno .
w_document-objdes = p_name . “Name of file
w_document-objnam = p_name . “Name of file
*/ Attachment FileName Assignment
w_h_data-objdes = p_name . “Name of file
*/ Using this function module to read FILE from Presentation server
CALL FUNCTION ‘SO_DOCUMENT_REPOSITORY_MANAGER’
EXPORTING
method = ‘IMPORTFROMPC’
* method = ‘ATTCREATEFROMSPOOL’
* method = ‘ATTCREATEBORLINK’
ref_document = w_document
TABLES
files = wt_files
CHANGING
document = w_document
header_data = w_h_data
folmem_data = w_fol_data
receive_data = w_rec_data.
*/ File Creation OkCodes
IF w_document-okcode = ‘CREA’ OR w_document-okcode = ‘CHNG’.
w_attachement-objtype = ‘MESSAGE’.
w_attachement-objkey = w_document(34).
CALL FUNCTION ‘BINARY_RELATION_CREATE_COMMIT’
EXPORTING
obj_rolea = ws_borident
obj_roleb = w_attachement
relationtype = ‘ATTA’
EXCEPTIONS
no_model = 1
internal_error = 2
unknown = 3
OTHERS = 4.
*/Error Handling
IF sy-subrc <> 0.
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ENDIF.
ENDIF .