Introduction
Recently a requirement came, where the user wanted to distribute the DMS documents along with its attachment to Outlook and SAP inbox. DMS which is a module in SAP for managing Documents stands for Document Management System. Users here create documents using different document types, and inside can attach any documents such as MS Word, Excel, PDF etc.
Solution
The requirement was targeted using transaction code CVI8, which is the SAP standard document distribution functionality. This transaction can send multiple documents to multiple receivers using standard workflows. The standard workflow creates a distribution package and sends mails to specified recipients, which has a predefined text.
The task was to enhance the standard workflow and include attachments along with the names of the DMS documents.
SAP stores the attachments in the DMS document in a content server, the challenge was to retrieve the documents from the content server and attach it in the distribution package which is being generated by the standard TCODE CVI8.
Call customer function:
The standard workflow uses functions defined in table V_TDIEX to create the MESSAGE object.The MESSAGE object contains the subject, body etc for the mail. First step is to change the standard function with the customer function.
Note: Copy Function CVV4_GENERATE_RMA to Z_DMS_CVV4_GENERATE_RMA.
Changing the copied function
Some functions and performs are being called in the function, you need to copy them from the standard function group into your customer function group, if you want the rest of the logic to stay the same. In my case I have rewritten two performs.
create_message_object
Here the subject and body of the mail can be changed, by updating variable "l_title" and internal table "content" respectively.
swc_set_element container 'DOCUMENTTITLE' l_title.
swc_set_table container 'DocumentContent' content.
attach_orig_to_message_object
Here the code to update the MESSAGE_OBJ of type OBJ_RECORD with the DMS attachments is written.
Since I want to attach all files from all DMS documents I first need to get the list of DMS documents. This is done by fetching the OBJKEY from the TABLES parameter DRZOC_TAB. The list of documents along with the MESSAGE_OBJ is sent to the subroutine attach_orig_to_message_object, which calls a customized function Z_DMS_ATTACH_FILES_TO_MESSGOBJ to attach mails to the MESSAGE_OBJ.
Function to attach DMS files in mail
*Loop on the list of files in the DMS document
*Split the file name from the path
*Check if the file is existing in the content repository
CALL FUNCTION 'CV120_KPRO_CONTENT_CHECK'
EXPORTING
ps_phio_id = ls_phio_id
IMPORTING
pfx_content = lv_content.
IF lv_content = 'X' AND <fs>-checked_out IS INITIAL.
REFRESH lt_att_content_hex.
*Get the content of the file from the content repository in binary format
PERFORM get_file_in_binary_format TABLES lt_att_content_hex "Binary content
USING ls_phio_id "File handle
CHANGING l_bin_filesize. "File size
*The binary content received is then attached to the MESSAGE_OBJ.
l_binsize = l_bin_filesize.
******************************Subroutines********************************
*&----
*& Form GET_FILE_IN_BINARY_FORMAT
&----
* text
----
* -->P_LS_PHIO_ID text
* -->P_LT_ATT_CONTENT_HEX text
----
FORM get_file_in_binary_format
TABLES ft_att_content_hex STRUCTURE solix
USING fs_phio_id STRUCTURE sdokobject
CHANGING p_bin_filesize.
TYPES: BEGIN OF ty_data,
line(2550) TYPE x.
TYPES: END OF ty_data.
DATA: lt_data TYPE STANDARD TABLE OF ty_data,
ls_data TYPE ty_data,
ls_comp TYPE sdokcomprp,
lt_comp TYPE TABLE OF sdokcomprp,
lt_bad_obj TYPE TABLE OF sdokerrkey,
lv_comp_get TYPE c,
lt_dms_comp TYPE dms_tbl_comp,
lt_content TYPE TABLE OF drao,
ls_content TYPE drao,
lv_file_xstring TYPE xstring,
lt_orblk TYPE TABLE OF drao-orblk WITH HEADER LINE.
* Get phys. data of the PHIO
ls_comp-class = fs_phio_id-class.
ls_comp-objid = fs_phio_id-objid.
APPEND ls_comp TO lt_comp.
*Get properties of the file
CALL FUNCTION 'SDOK_PHIOS_FILE_PROPERTIES_GET'
TABLES
components = lt_comp
bad_objects = lt_bad_obj.
READ TABLE lt_comp INTO ls_comp INDEX 1.
p_bin_filesize ls_comp-file_size.
*Standard function to get the file content
CALL FUNCTION 'CV120_KPRO_CHECKOUT_TO_TABLE'
EXPORTING
ps_phio_id = fs_phio_id
pf_comp_get = lv_comp_get
TABLES
pt_components = lt_dms_comp
ptx_content = lt_content
EXCEPTIONS
error = 1
no_content = 2
OTHERS = 3.
Note: The data received from the function 'CV120_KPRO_CHECKOUT_TO_TABLE' is concatenated into an XSTRING and then passed to standard function 'SCMS_XSTRING_TO_BINARY' to be converted into SOLIX table type format. Which will then be attached to the MESSAGE object.
IF lt_content[] IS NOT INITIAL.
LOOP AT lt_content INTO ls_content .
CONCATENATE lv_file_xstring ls_content-orblk INTO lv_file_xstring IN BYTE MODE.
ENDLOOP.
IF sy-subrc EQ 0.
CALL FUNCTION 'SCMS_XSTRING_TO_BINARY'
EXPORTING
buffer = lv_file_xstring
TABLES
binary_tab = ft_att_content_hex.
ENDIF.
CLEAR lv_file_xstring.
ENDIF.
ENDFORM. "GET_FILE_IN_BINARY_FORMAT
&----
*& Form ATTACH_FILE_TO_MESSAGE_OBJECT
&----
* attach file to message object
----
* --> PT_GRAPHICS graphics data table
* <-- P_MESSAGE_OBJ message object
----
FORM attach_file_to_message_object TABLES pt_attachment STRUCTURE solix
USING p_attname
p_bin_filesize
CHANGING p_message_obj LIKE obj_record.
DATA: object TYPE swc_object.
DATA: content LIKE solix OCCURS 0 WITH HEADER LINE.
DATA: l_name LIKE drzof-filep.
DATA: l_suffix(3).
swc_container container.
swc_clear_container container.
* create dummy object
swc_create_object object 'MESSAGE' space.
swc_set_element container 'ATTACHMENT' object.
* get file suffix to determine document type
SPLIT p_attname AT '.' INTO l_name l_suffix.
IF l_suffix IS INITIAL.
l_suffix = 'BIN'.
ELSE.
TRANSLATE l_suffix TO UPPER CASE.
ENDIF.
swc_set_element container 'ATTACHMENTTITLE' l_name.
swc_set_element container 'ATTACHMENTTYPE' l_suffix.
CLEAR: content. REFRESH: content.
* fill content with binary data
LOOP AT pt_attachment.
content-line = pt_attachment-line.
APPEND content.
ENDLOOP.
swc_set_table container 'CONTENT_HEX' content.
swc_set_element container 'DOCUMENTSIZE' p_bin_filesize.
* append attachment to message
swc_call_method p_message_obj 'Attach' container.
ENDFORM. " ATTACH_FILE_TO_MESSAGE_OBJECT