# 自开发表变更历史记录
原创 ABAP三叔 [ ABAP三叔 ](javascript:void\(0\);)
__ _ _ _ _
顾问们应该都知道,SAP中有很多标准表的字段变动,会有变更历史记录,这些记录存在于CDHDR/CDPOS这两个表中,本文将介绍如何把自开发表的变更记录写入这两个表。
SE11先创建一个自定义表ZTMARA,字段如下
需要记录变更的字段,数据元素的"更改文档"需要勾选上!!!重要
1.打开TCODE: S CDO,输入ZTMARA后,点新建图标
6.点信息图标,双击ZCL_ZTMARA_CHDO,或者SE24直接进
7\. ZCL_ZTMARA_CHDO 类中已经自动生成了变更记录的write方法,我们再额外增加两个方法和一个数据类型定义
• *
TYPES:tt_changenr TYPE STANDARD TABLE OF cdhdr-changenr WITH EMPTY KEY.• * * * * * * * * *
CLASS-METHODS save_single IMPORTING !is_ztmara TYPE ty_ztmara RETURNING VALUE(rv_changenr) TYPE cdhdr-changenr . CLASS-METHODS save_multi IMPORTING VALUE(it_ztmara) TYPE tt_ztmara RETURNING VALUE(rt_changenr) TYPE tt_changenr .• * * * * * * * * * * *
METHOD save_multi. CHECK it_ztmara[] IS NOT INITIAL. SORT it_ztmara BY matnr. DELETE ADJACENT DUPLICATES FROM it_ztmara COMPARING matnr. LOOP AT it_ztmara INTO DATA(ls_ztmara). DATA(rv_changenr) = save_single( ls_ztmara ). IF rv_changenr IS NOT INITIAL. APPEND rv_changenr TO rt_changenr. ENDIF. CLEAR rv_changenr. ENDLOOP. ENDMETHOD.7.4 save_single 方法实现,
将自动生成的write方法的尾部注释的代码段,拷贝过来,然后取消注释,增加部分处理逻辑,可参考下面 这部分为自己新增逻辑 代码部分
• * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
METHOD save_single. " Code snippets to be used with COPY+PASTE " uncomment the needed parts " change names if needed" Start of default parameter part DATA: objectid TYPE cdhdr-objectid, tcode TYPE cdhdr-tcode, planned_change_number TYPE cdhdr-planchngnr, utime TYPE cdhdr-utime, udate TYPE cdhdr-udate, username TYPE cdhdr-username, cdoc_planned_or_real TYPE cdhdr-change_ind, cdoc_upd_object TYPE cdhdr-change_ind VALUE 'U', cdoc_no_change_pointers TYPE cdhdr-change_ind. DATA: cdchangenumber TYPE cdhdr-changenr. " End of default parameter part
" Begin of dynamic DATA part for class ZCL_ZTMARA_CHDO " table with the NEW content of: ZTMARA DATA xztmara TYPE zcl_ztmara_chdo=>tt_ztmara. " table with the OLD content of: ZTMARA DATA yztmara TYPE zcl_ztmara_chdo=>tt_ztmara. " change indicator for table: ZTMARA DATA upd_ztmara TYPE if_chdo_object_tools_rel=>ty_cdchngindh.
" Change Number of Document DATA changenumber TYPE if_chdo_object_tools_rel=>ty_cdchangenr." End of dynamic DATA part for class ZCL_ZTMARA_CHDO
*----------------- 这部分为自己新增逻辑 BEGIN------------------* CHECK is_ztmara IS NOT INITIAL. DATA ls_yztmara TYPE ztmara. objectid = is_ztmara-matnr. tcode = sy-tcode. utime = sy-uzeit. udate = sy-datum. username = sy-uname.* 查原记录 SELECT SINGLE * INTO ls_yztmara FROM ztmara WHERE matnr = is_ztmara-matnr. upd_ztmara = COND #( WHEN is_ztmara-kz IS NOT INITIAL THEN is_ztmara-kz WHEN sy-subrc = 0 THEN 'U' ELSE 'I' ). "添加新记录 APPEND CORRESPONDING ty_ztmara( BASE ( VALUE #( mandt = sy-mandt kz = upd_ztmara ) ) is_ztmara EXCEPT mandt kz ) TO xztmara. "添加旧记录 IF sy-subrc = 0. APPEND CORRESPONDING ty_ztmara( BASE ( VALUE #( kz = upd_ztmara ) ) ls_yztmara ) TO yztmara. ENDIF.*----------------- 这部分为自己新增逻辑 END------------------* " Begin of method call part " define needed DATA for error handling DATA err_ref TYPE REF TO cx_chdo_write_error. DATA err_action TYPE string.
TRY. CALL METHOD zcl_ztmara_chdo=>write EXPORTING objectid = objectid tcode = tcode utime = utime udate = udate username = username planned_change_number = planned_change_number object_change_indicator = cdoc_upd_object planned_or_real_changes = cdoc_planned_or_real no_change_pointers = cdoc_no_change_pointers " End of default method call part " Begin of dynamic part for method call " table with the NEW content of: ZTMARA xztmara = xztmara " table with the OLD content of: ZTMARA yztmara = yztmara " change indicator for table: ZTMARA upd_ztmara = upd_ztmara " Change Number of Document IMPORTING changenumber = cdchangenumber. CATCH cx_chdo_write_error INTO err_ref. " MESSAGE err_ref TYPE 'A'. " error information could be determined with default GET_TEXT, GET_LONGTEXT, GET_SOURCE_POSITION methds
ENDTRY. " End of dynamic part for method call rv_changenr = cdchangenumber. ENDMETHOD.• * * * * * * * * * * * * * * * * * * * * * * * * *
REPORT ztestchdo.DATA lt_ztmara TYPE STANDARD TABLE OF ztmara.DATA ls_ztmara TYPE ztmara.lt_ztmara = VALUE #( ( matnr = 'M001RS' color = '红' size1 = 'S' ztag_price = '100' ) ( matnr = 'M001RM' color = '红' size1 = 'M' ztag_price = '100' ) ( matnr = 'M001RL' color = '红' size1 = 'L' ztag_price = '100' ) ).
"写新增日志zcl_ztmara_chdo=>save_multi( CORRESPONDING #( lt_ztmara ) )."插入记录INSERT ztmara FROM TABLE lt_ztmara.COMMIT WORK AND WAIT.
"写变更日志ls_ztmara = VALUE #( matnr = 'M001RS' color = '黑' size1 = 'S' ztag_price = '150' ) .zcl_ztmara_chdo=>save_single( CORRESPONDING #( ls_ztmara ) )."修改记录MODIFY ztmara FROM ls_ztmara.COMMIT WORK AND WAIT.
"写删除日志ls_ztmara = VALUE #( matnr = 'M001RL' color = '红' size1 = 'L' ztag_price = '100' ).zcl_ztmara_chdo=>save_single( CORRESPONDING #( BASE ( VALUE #( kz = 'D') ) ls_ztmara ) )."删除记录DELETE ztmara FROM ls_ztmara.COMMIT WORK AND WAIT.1.打开TCODE: SCD3,输入变更文档对象ZTMARA。
也可直接SE16N查数据库表 CDHDR/CDPOS
— END —
**温馨提示**
如果你喜欢本文,请分享给有需要的朋友,想要获得更多信息,请关注我。
.textContent = new Date().getFullYear();