[原创]SM30 保存日志到自建表

2021-08-07 23:51发布


          点击此处--->   EasySAP.com 群内免费提供SAP练习系统(在群公告中)

加入QQ群:457200227(SAP S4 HANA技术交流) 群内免费提供SAP练习系统(在群公告中)

有时候系统关闭了SCU3的日志,所以有些维护视图就需要自己保存一下日志,不然以后麻烦

假如你的SM30的维护视图对应的表名称为:ZTEST

首先建一个这样结构的日志表:比如:ZSM30_LOG

MANDT
主键
数据元素
长度
描述
TAB_NAMEX
TABNAME30
表名
ERDATX
ERDAT8记录的创建日期
UUIDX
SYSUUID_C32字符表单中的 UUID
CHG_FLAGCHG_FLAG1更改的值
ERNAMERNAM12创建对象的人员名称
ERTIMERZET6输入时间
BEFOREPSTRING改之前的值
AFTERPSTRING改之后的值


然后在SM30的事件before_save里加入如下代码:


 FORM save_log.

   FIELD-SYMBOLS:
         <fs_field> TYPE ANY.
   DATA: lv_field TYPE string,
         lv_before TYPE string,
         lv_after TYPE string,
         wa_ZTEST TYPE ZTEST,
         wa_ZSM30_LOG TYPE ZSM30_LOG,
         it_ZSM30_LOG TYPE TABLE OF ZSM30_LOG.

   DATA BEGIN OF ls_total.
           INCLUDE STRUCTURE ZTEST.
           INCLUDE STRUCTURE vimflagtab.
   DATA END OF ls_total.

   FIELD-SYMBOLS:
        <ls_total> LIKE ls_total.

   LOOP AT total ASSIGNING <ls_total> CASTING.

     CLEAR:lv_field,
           lv_after,
           lv_before,
           wa_ZTEST.

     MOVE-CORRESPONDING <ls_total> TO wa_ZTEST.
     IF  <ls_total>-action = 'I' OR
         <ls_total>-action = 'U' OR
         <ls_total>-action = 'N' OR
         <ls_total>-action = 'D'.
       DO.
         ASSIGN COMPONENT sy-index OF STRUCTURE <ls_total>
                                   TO <fs_field>.
         IF sy-subrc EQ 0 AND sy-index <= 46.
           lv_field = <fs_field>.
           IF lv_after IS INITIAL.
             lv_after = lv_field.
           ELSE.
             CONCATENATE lv_after
                         '#'
                         lv_field
                         INTO lv_after.
           ENDIF.
         ELSE.
           EXIT.
         ENDIF.
       ENDDO.

       wa_ZSM30_LOG-tab_name = x_header-viewname.
       wa_ZSM30_LOG-erdat = sy-datum.

       CALL FUNCTION 'SYSTEM_UUID_C_CREATE'
         IMPORTING
           uuid = wa_ZSM30_LOG-uuid.

       wa_ZSM30_LOG-chg_flag = <ls_total>-action.
       wa_ZSM30_LOG-ernam = sy-uname.
       wa_ZSM30_LOG-ertim = sy-uzeit.

       IF <ls_total>-action = 'I'.
         wa_ZSM30_LOG-before = ''.
         wa_ZSM30_LOG-after = lv_after.
       ELSEIF <ls_total>-action = 'U'.

         SELECT SINGLE * FROM ZTEST INTO wa_ZTEST
           WHERE 主键1 = wa_ZTEST-主键1
             AND 主键2 = wa_ZTEST-主键1
             AND 主键3 = wa_ZTEST-主键3
               ...
               ...
             .
         DO.
           ASSIGN COMPONENT sy-index OF STRUCTURE wa_ZTEST
                                     TO <fs_field>.
           IF sy-subrc EQ 0.
             lv_field = <fs_field>.
             IF lv_before IS INITIAL.
               lv_before = lv_field.
             ELSE.
               CONCATENATE lv_before
                           '#'
                           lv_field
                           INTO lv_before.
             ENDIF.
           ELSE.
             EXIT.
           ENDIF.
         ENDDO.
         wa_ZSM30_LOG-before = lv_before.
         wa_ZSM30_LOG-after  = lv_after.
       ELSEIF <ls_total>-action = 'N'.
         wa_ZSM30_LOG-before = ''.
         wa_ZSM30_LOG-after  = lv_after.
       ELSEIF <ls_total>-action = 'D'.
         wa_ZSM30_LOG-before = lv_after.
         wa_ZSM30_LOG-after  = ''.
       ENDIF.

       APPEND wa_ZSM30_LOG TO it_ZSM30_LOG.
       CLEAR wa_ZSM30_LOG.

     ENDIF.

   ENDLOOP.

   IF it_ZSM30_LOG[] IS NOT INITIAL.
     INSERT ZSM30_LOG FROM TABLE it_ZSM30_LOG
                     ACCEPTING DUPLICATE KEYS.
   ENDIF.

 ENDFORM.                    "save_log


赞赏支持