Using Workflow Programming Exit for Travel Expense Approval Workflow: Copy attachments of travel expense attachments to workitem
me->wi_context = im_workitem_context.
IF im_event_name <> swrco_event_after_creation.
EXIT.
ENDIF.
*–Call after creation method
me->after_creation( ).
DATA:l_workitem_id TYPE sww_wiid ,
l_value1 TYPE string ,
l_value2 TYPE string .
DATA:l_pernr TYPE persno ,
l_reinr TYPE reinr .
DATA: l_object_key TYPE swr_struct–object_key,
lt_container TYPE STANDARD TABLE OF swr_cont,
ls_container TYPE swr_cont.
*–Local reference to work item container
DATA:lrf_container TYPE REF TO if_swf_ifs_parameter_container.
*–Get work item ID
CALL METHOD wi_context->get_workitem_id
RECEIVING
re_workitem = l_workitem_id.
*–Get work item container
CALL METHOD wi_context->get_wi_container
RECEIVING
re_container = lrf_container.
*–Get Trip Number from container element
TRY.
CALL METHOD lrf_container->get
EXPORTING
name = ‘TripNumber’
IMPORTING
value = l_value1.
* unit =
* returncode =
CATCH cx_swf_cnt_elem_not_found .
CATCH cx_swf_cnt_elem_type_conflict .
CATCH cx_swf_cnt_unit_type_conflict .
CATCH cx_swf_cnt_container .
ENDTRY.
*–Get employee number from container element
TRY.
CALL METHOD lrf_container->get
EXPORTING
name = ‘EmployeeNumber’
IMPORTING
value = l_value2.
* unit =
* returncode =
CATCH cx_swf_cnt_elem_not_found .
CATCH cx_swf_cnt_elem_type_conflict .
CATCH cx_swf_cnt_unit_type_conflict .
CATCH cx_swf_cnt_container .
ENDTRY.
*–Call FM to get relationships if any of trip and copy attachments to work item
l_pernr = l_value2.
l_reinr = l_value1.
*–Create event to attach files
l_object_key = l_reinr.
ls_container–element = ‘PersonnelNumber’.
APPEND ls_container TO lt_container.
ls_container–element = ‘TripNumber’.
APPEND ls_container TO lt_container.
ls_container–element = ‘WorkItem’.
APPEND ls_container TO lt_container.
CALL FUNCTION ‘SAP_WAPI_CREATE_EVENT’
EXPORTING
object_type = ‘ZHRXMTRIP’
object_key = l_object_key
event = ‘SentForApproval’
TABLES
input_container = lt_container.
CALL FUNCTION ‘ZHREXP_ATTACHMENTS_TO_WI’
EXPORTING
im_workitemid = i_workitem
im_pernr = i_pernr
im_reinr = i_reinr
EXCEPTIONS
no_attachment_found = 1
error_reading_attachment = 2
error_instantiating_attachment = 3
OTHERS = 4.
IF sy–subrc <> 0.
IF sy–subrc = 1.
MESSAGE ID sy–msgid TYPE sy–msgty NUMBER sy–msgno
WITH sy–msgv1 sy–msgv2 sy–msgv3 sy–msgv4
ELSEIF sy–subrc = 2.
MESSAGE ID sy–msgid TYPE sy–msgty NUMBER sy–msgno
WITH sy–msgv1 sy–msgv2 sy–msgv3 sy–msgv4
ELSEIF sy–subrc = 3.
MESSAGE ID sy–msgid TYPE sy–msgty NUMBER sy–msgno
WITH sy–msgv1 sy–msgv2 sy–msgv3 sy–msgv4
ENDIF.
ENDIF.
*”*”Local Interface:
*” IMPORTING
*” VALUE(IM_WORKITEMID) TYPE SWW_WIID OPTIONAL
*” VALUE(IM_PERNR) TYPE PERNR
*” TABLES
*” T_ATTA STRUCTURE SWR_ATT_ID OPTIONAL
*” EXCEPTIONS
*” NO_ATTACHMENT_FOUND
*” ERROR_READING_ATTACHMENT
*” ERROR_INSTANTIATING_ATTACHMENT
*”———————————————————————
INCLUDE STRUCTURE soentryi1.
DATA END OF ls_docid_structure.
DATA: ls_lpor TYPE sibflporb,
lt_conn TYPE TABLE OF bdn_con,
ls_conn TYPE bdn_con,
ls_atta TYPE swr_att_id,
ls_key TYPE soodk,
ls_text TYPE soli,
ls_comment TYPE bcss_dbpc,
lt_hex_cont TYPE TABLE OF solix,
ls_hex_cont TYPE solix,
lrf_document TYPE REF TO cl_document_bcs,
lrf_bcs_exception TYPE REF TO cx_root,
ls_att_header TYPE swr_att_header.
DATA: l_att_txt TYPE string,
l_text TYPE string,
l_file_ext TYPE char3,
l_document_id TYPE sofolenti1–doc_id,
l_binfile TYPE xstring.
* Constants
CONSTANTS : lc_x TYPE char1 VALUE ‘X’,
lc_bus2089 TYPE bds_clsnam VALUE ‘BUS2089’,
lc_clstype TYPE char2 VALUE ‘BO’,
lc_e TYPE char1 VALUE ‘E’,
lc_t TYPE char1 VALUE ‘T’,
lc_b TYPE char1 VALUE ‘B’.
CONCATENATE im_pernr im_reinr INTO ls_lpor–instid.
ls_lpor–typeid = lc_bus2089.
ls_lpor–catid = lc_clstype.
CALL FUNCTION ‘BDS_ALL_CONNECTIONS_GET’
EXPORTING
classname = lc_bus2089
classtype = lc_clstype
objkey = ls_lpor–instid
all = lc_x
no_gos_docs = ”
TABLES
all_connections = lt_conn
EXCEPTIONS
no_objects_found = 1
error_kpro = 2
internal_error = 3
not_authorized = 4
OTHERS = 5.
IF sy–subrc <> 0.
MESSAGE e681(zhr_msg) WITH im_pernr im_reinr RAISING
error_reading_attachment.
ELSEIF lt_conn[] IS INITIAL.
MESSAGE e680(zhr_msg) WITH im_pernr im_reinr RAISING
no_attachment_found.
ENDIF.
*–Process the attachment list
LOOP AT lt_conn INTO ls_conn.
l_document_id = ls_conn–loio_id.
CLEAR ls_docid_structure.
MOVE l_document_id TO ls_docid_structure.
*–Check class of document
IF ls_conn–docuclass = ‘txt’ OR ls_conn–docuclass = ‘TXT’ OR
ls_conn–docuclass = ‘asc’ OR ls_conn–docuclass = ‘ASC’.
CLEAR ls_key.
ls_key–objtp = ls_docid_structure–objtp.
ls_key–objyr = ls_docid_structure–objyr.
ls_key–objno = ls_docid_structure–objno.
TRY.
CALL METHOD cl_document_bcs=>getu_instance_by_key
EXPORTING
i_sood_key = ls_key
i_no_enqueue = lc_x
RECEIVING
result = lrf_document.
CATCH cx_document_bcs .
CALL METHOD lrf_bcs_exception->get_text
RECEIVING
result = l_text.
MESSAGE e681(zhr_msg) WITH im_pernr im_reinr RAISING
error_instantiating_attachment.
ENDTRY.
*–Read content
TRY.
CALL METHOD lrf_document->if_document_bcs~get_body_part_content
EXPORTING
im_part = 1
RECEIVING
re_content = ls_comment.
CATCH cx_document_bcs .
CALL METHOD lrf_bcs_exception->get_text
RECEIVING
result = l_text.
MESSAGE e681(zhr_msg) WITH im_pernr im_reinr RAISING
error_reading_attachment.
ENDTRY.
CLEAR:l_att_txt,ls_text.
LOOP AT ls_comment–cont_text INTO ls_text.
CONCATENATE l_att_txt ls_text INTO l_att_txt.
ENDLOOP.
l_file_ext = ls_conn–docuclass.
TRANSLATE l_file_ext TO UPPER CASE. “#EC TRANSLANG
ls_att_header–file_type = lc_t.
ls_att_header–file_extension = l_file_ext.
CONCATENATE ls_conn–descript l_file_ext INTO ls_att_header–file_name.
ls_att_header–language = lc_e.
CALL FUNCTION ‘SAP_WAPI_ATTACHMENT_ADD’
EXPORTING
workitem_id = im_workitemid
att_header = ls_att_header
att_txt = l_att_txt
document_owner = ls_conn–crea_user
do_commit = lc_x
IMPORTING
att_id = ls_atta.
APPEND ls_atta TO t_atta.
CLEAR ls_atta.
ELSE.
CLEAR lt_hex_cont[].
*–Read the data
CALL FUNCTION ‘SO_DOCUMENT_READ_API1’
EXPORTING
document_id = l_document_id
TABLES
contents_hex = lt_hex_cont
EXCEPTIONS
document_id_not_exist = 1
operation_no_authorization = 2
x_error = 3
OTHERS = 4.
IF sy–subrc NE 0.
MESSAGE e681(zhr_msg) WITH im_pernr im_reinr RAISING error_reading_attachment.
ENDIF.
CLEAR l_binfile.
LOOP AT lt_hex_cont INTO ls_hex_cont.
ASSIGN ls_hex_cont TO <p> CASTING.
CONCATENATE l_binfile <p> INTO l_binfile IN BYTE MODE.
ENDLOOP.
IF im_workitemid IS NOT INITIAL.
l_file_ext = ls_conn–docuclass.
TRANSLATE l_file_ext TO UPPER CASE. “#EC TRANSLANG
ls_att_header–file_type = lc_b.
ls_att_header–file_extension = l_file_ext.
CONCATENATE ls_conn–descript l_file_ext INTO ls_att_header–file_name.
ls_att_header–language = lc_e.
CALL FUNCTION ‘SAP_WAPI_ATTACHMENT_ADD’
EXPORTING
workitem_id = im_workitemid
att_header = ls_att_header
att_bin = l_binfile
document_owner = ls_conn–crea_user
do_commit = lc_x
IMPORTING
att_id = ls_atta.
APPEND ls_atta TO t_atta.
CLEAR ls_atta.
ENDIF.
ENDIF. “Binary or text
ENDLOOP.
IF t_atta[] IS INITIAL.
MESSAGE e680(zhr_msg) WITH im_pernr im_reinr RAISING
no_attachment_found.
ENDIF.
ENDFUNCTION.