背景:公司最近在上电子签系统,以实现劳动合同、保密协议等文件的去纸质化,保存为电子档文件,而企业的信息化的中心是SAP ERP,于是领导要求将签好的电子文件存储到HCM中。
题主写了如下代码实现需求:
FUNCTION ZHR_SAVE_FILE.
*"----------------------------------------------------------------------
*"*"局部接口:
*" IMPORTING
*" VALUE(FILE) TYPE STRING OPTIONAL
*" VALUE(USRTY) TYPE CHAR4 OPTIONAL
*" VALUE(USRID) TYPE CHAR30 OPTIONAL
*" VALUE(FILETYPE) TYPE CHAR10 OPTIONAL
*" EXPORTING
*" VALUE(ZTYPE) TYPE TEXT1
*" VALUE(MESSAGE) TYPE CHAR200
*"----------------------------------------------------------------------
DATA: access_info LIKE scms_acinf OCCURS 1 WITH HEADER LINE,
content_txt LIKE sdokcntasc OCCURS 1,
content_bin LIKE sdokcntbin OCCURS 1.
DATA: ls_content_bin LIKE LINE OF content_bin.
DATA: l_string TYPE string.
DATA: l_xstring TYPE xstring.
DATA: doc_id_out TYPE char40.
* DATA: ADOKID TYPE SAPB-SAPADOKID.
* DATA: lt_ztb_icnum LIKE TABLE OF ztb_icnum,
* ls_ztb_icnum LIKE LINE OF lt_ztb_icnum.
access_info-comp_id = 'data'.
IF FILETYPE is INITIAL or FILETYPE = '' .
ztype = 'E'.
message = '请输入文件类型!' .
return.
ENDIF.
IF USRID is INITIAL or USRID = ''.
ztype = 'E'.
message = '请输入人员工号!' .
return.
ENDIF.
IF usrty IS NOT INITIAL AND usrid IS NOT INITIAL AND file IS NOT INITIAL .
* ls_ztb_icnum-ictyp = usrty. "通过证件号码获取人员编号
* ls_ztb_icnum-icnum = usrid.
* APPEND ls_ztb_icnum TO lt_ztb_icnum.CLEAR ls_ztb_icnum.
*
*
* IF lt_ztb_icnum[] IS NOT INITIAL.
*
* CALL FUNCTION 'Z_IF_HR_PA_CHECK'
* TABLES
* tb_hr_icnum = lt_ztb_icnum.
*
* ENDIF.
*
* READ TABLE lt_ztb_icnum INTO ls_ztb_icnum INDEX 1.
*
* IF ls_ztb_icnum-pernr IS NOT INITIAL. "判断是否能找到SAP对应的人员编号
CALL FUNCTION 'SCMS_BASE64_DECODE_STR'
EXPORTING
input = file
* unescape = 'X'
IMPORTING
output = l_xstring
EXCEPTIONS
failed = 1
OTHERS = 2.
IF sy-subrc <> 0.
* Implement suitable error handling here
ENDIF.
CALL FUNCTION 'SCMS_XSTRING_TO_BINARY'
EXPORTING
buffer = l_xstring
* APPEND_TO_TABLE = ' '
* IMPORTING
* OUTPUT_LENGTH =
TABLES
binary_tab = content_bin
.
DATA: lv_nu TYPE i.
CLEAR l_string.
CLEAR access_info-comp_size.
LOOP AT content_bin INTO ls_content_bin.
l_string = ls_content_bin-line.
access_info-comp_size = STRLEN( l_string ) + access_info-comp_size.
ENDLOOP.
access_info-mimetype = 'application/*'.
access_info-binary_flg = 'X'.
access_info-first_line = 1.
access_info-last_line = LINES( content_bin ).
APPEND access_info.
CALL FUNCTION 'SCMS_R3DB_EXPORT'
EXPORTING
mandt = sy-mandt
crep_id = 'A2'
doc_id = ''
doc_prot = 'rud'
overwrite = '-'
vscan_profile = '/SCMS/KPRO_CREATE'
IMPORTING
doc_id_out = doc_id_out
TABLES
access_info = access_info
content_txt = content_txt
content_bin = content_bin
EXCEPTIONS
error_export = 1
error_import = 2
error_parameter = 3
blocked_by_policy = 4
OTHERS = 5.
IF sy-subrc <> 0.
* case sy-subrc.
* when 1. sys_message_raising error_export.
* when 2. sys_message_raising error_export.
* when 3. sys_message_raising error_parameter.
* when 4. sys_message_raising blocked_by_policy.
* when others. sys_message_raising error_export.
* endcase.
ELSE.
DATA: BEGIN OF i_toaom OCCURS 1.
INCLUDE STRUCTURE toaom.
DATA: END OF i_toaom.
DATA: BEGIN OF i_toav0 OCCURS 1.
INCLUDE STRUCTURE toav0.
DATA: END OF i_toav0.
REFRESH i_toaom.
i_toav0-mandt = sy-mandt.
i_toav0-sap_object = 'PREL'.
i_toav0-archiv_id = 'A2'.
i_toav0-object_id = usrid. "pernr-pernr
i_toav0-ar_object = 'HRICONTRAC'.
i_toav0-ar_date = sy-datum.
* I_TOAV0-DEL_DATE = SY-DATUM.
i_toav0-reserve = filetype.
i_toav0-arc_doc_id = doc_id_out.
APPEND i_toav0.
CALL FUNCTION 'ARCHIV_CONNECTIONTABLE_INSERT'
TABLES
entrys_connectiontable = i_toav0
EXCEPTIONS
no_entry_possible = 1
OTHERS = 2.
CASE sy-subrc.
WHEN 1.
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4
RAISING no_entry_possible.
WHEN 2.
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4
RAISING others.
WHEN OTHERS.
ztype = 'S'.
message = '上传成功'.
ENDCASE.
ENDIF.
* ELSE.
* ztype = 'E'.
* CONCATENATE '无法在SAP系统中找到证件号码为:' ls_ztb_icnum-icnum ',的人员!' INTO message .
ENDIF.
* ELSE.
* ztype = 'E'.
* message = '请输入所有必输项!'.
* ENDIF.
ENDFUNCTION.
效果图如下: