cancel
Showing results for 
Search instead for 
Did you mean: 

Add a attachment to a package unit

petra_just
Active Participant
1,191

hi experts,

today, I need to attach an attachment (like a picture) to a package unit (tor object). I found the node attachmentfolder under the root node. but, how to add a file? has someone done it already and agrees to send me some sample coding or give me a hint? thanking you in advance

Petra

View Entire Topic

Hı Petra ;

I managed to add documents this way.

FUNCTION ztm_fm_tor_attachmentfolder.
*"----------------------------------------------------------------------
*"*"Local Interface:
*" IMPORTING
*" REFERENCE(IV_TORID) TYPE /SCMTMS/TOR_ID
*" REFERENCE(IV_DATA) TYPE XSTRING
*" REFERENCE(IV_MIME_TYPE) TYPE W3CONTTYPE
*" REFERENCE(IV_NAME) TYPE SDOK_FILNM
*" TABLES
*" ET_RETURN STRUCTURE BAPIRET2 OPTIONAL
*"----------------------------------------------------------------------
TYPES:
BEGIN OF ty_ls_att_with_content.
INCLUDE TYPE /scmtms/s_gw_gen_att.
TYPES: attachmenttocontent TYPE /scmtms/s_gw_gen_att_cont,
END OF ty_ls_att_with_content.
DATA: lo_message TYPE REF TO /bobf/if_frw_message,
lt_tor_key TYPE /bobf/t_frw_key, "tor key of object wto which we attach the picture
ls_tor_key TYPE /bobf/s_frw_key,
lv_action_key TYPE /bobf/conf_key,
lv_tor_key TYPE /bobf/conf_key,
lo_change TYPE REF TO /bobf/if_tra_change,
lt_failed_key TYPE /bobf/t_frw_key,
lt_mod TYPE /bobf/t_frw_modification,
ls_mod TYPE /bobf/s_frw_modification,
lv_rejected TYPE boole_d,
lr_action_par TYPE REF TO /bobf/s_atf_a_create_file,
lr_atf_root TYPE REF TO /bobf/s_atf_root_k,
ls_attachment_data TYPE ty_ls_att_with_content,
ls_parameters TYPE /bobf/s_atf_a_create_file,
lt_document_data TYPE /bobf/t_atf_document_k.
DATA : lt_rej_bo_key TYPE /bobf/t_frw_key2,
lt_bapiret TYPE TABLE OF bapiret2.
"Initiate service and transaction managers
data(lo_srv_tor) = /bobf/cl_tra_serv_mgr_factory=>get_service_manager( /scmtms/if_tor_c=>sc_bo_key )."Srv Manager TOR object
DATA(lo_tra_tor) = /bobf/cl_tra_trans_mgr_factory=>get_transaction_manager( ).
lv_tor_key = /scmtms/cl_tor_helper_root=>return_key_for_torid( iv_torid = iv_torid ).
IF lv_tor_key IS NOT INITIAL.
APPEND VALUE #( key = lv_tor_key ) TO lt_tor_key.
"find attachment key via the association
lo_srv_tor->retrieve_by_association(
EXPORTING
iv_node_key = /scmtms/if_tor_c=>sc_node-attachmentfolder
it_key = lt_tor_key
iv_association = /scmtms/if_tor_c=>sc_association-root-attachmentfolder
iv_edit_mode = /bobf/if_conf_c=>sc_edit_read_only
IMPORTING
et_target_key = DATA(lt_atf_key)
et_failed_key = lt_failed_key ).
ls_attachment_data-attachmenttocontent-content = iv_data.
ls_attachment_data-mime_type = iv_mime_type.
ls_attachment_data-name = iv_name.
"fill the parameter table
CREATE DATA lr_action_par.
GET REFERENCE OF ls_parameters INTO lr_action_par.
ls_parameters-alternative_name = ls_attachment_data-name.
ls_parameters-content = ls_attachment_data-attachmenttocontent-content.
ls_parameters-file_name = ls_attachment_data-name.
ls_parameters-mime_code = ls_attachment_data-mime_type.
ls_parameters-name = ls_attachment_data-name.
ls_parameters-description_language_code = sy-langu.
ls_parameters-description = ls_attachment_data-name.
ls_parameters-description_content = ls_attachment_data-name.
ls_parameters-visibility_type = 'X'.
ls_parameters-attachment_type = 'ATCMT'.
ls_parameters-attachment_schema = 'DEFAULT'.
IF lt_atf_key[] IS INITIAL.
CREATE DATA lr_atf_root.
lr_atf_root->key = lo_srv_tor->get_new_key( ).
lr_atf_root->parent_key = /scmtms/if_tor_c=>sc_node-root.
ls_mod-change_mode = /bobf/if_frw_c=>sc_modify_create.
ls_mod-association = /scmtms/if_tor_c=>sc_association-root-attachmentfolder.
ls_mod-source_node = /scmtms/if_tor_c=>sc_node-root.
ls_mod-node = /scmtms/if_tor_c=>sc_node-attachmentfolder.
ls_mod-source_key = lv_tor_key.
ls_mod-key = lr_atf_root->key.
ls_mod-data = lr_atf_root.
INSERT ls_mod INTO TABLE lt_mod.
lo_srv_tor->modify(
EXPORTING
it_modification = lt_mod ).
lo_tra_tor->save( IMPORTING ev_rejected = lv_rejected
eo_change = lo_change
eo_message = lo_message ).
INSERT VALUE #( key = lr_atf_root->key ) INTO TABLE lt_atf_key.
ENDIF.
CALL METHOD /scmtms/cl_common_helper=>get_do_keys_4_action
EXPORTING
iv_host_bo_key = /scmtms/if_tor_c=>sc_bo_key
iv_host_do_node_key = /scmtms/if_tor_c=>sc_node-attachmentfolder
iv_do_action_key = /bobf/if_attachment_folder_c=>sc_action-root-create_file
IMPORTING
ev_action_key = lv_action_key.
CALL METHOD lo_srv_tor->do_action
EXPORTING
iv_act_key = lv_action_key
it_key = lt_atf_key
is_parameters = lr_action_par
IMPORTING
et_failed_key = DATA(lt_failed_key_atf)
eo_change = lo_change
eo_message = lo_message.
IF lt_failed_key_atf[] IS INITIAL.
lo_tra_tor->save( IMPORTING ev_rejected = lv_rejected
eo_change = lo_change
eo_message = lo_message
et_rejecting_bo_key = lt_rej_bo_key ).
ENDIF.
IF lo_message IS NOT INITIAL.
/scmtms/cl_common_helper=>msg_convert_bopf_2_bapiret2(
EXPORTING
io_message = lo_message
CHANGING
ct_bapiret2 = lt_bapiret ).
ENDIF.
ENDIF.
APPEND LINES OF lt_bapiret TO et_return.
ENDFUNCTION. 
former_member846564
Discoverer
0 Kudos

How i can delete an existing attachment before attaching a new file