最近有朋友问怎样在SM30进行数据维护时, 增加如下红圈内的数据,用来记录创建及更改数据,下面大致介绍一下操作的步骤 ;
首先我们在表维护生成器中,按照如下步骤,打开表维护生成器->环境->修改->事件;
增加两个子程序,分别添加01/05两个事件;
我们分别看一下01代表在数据库中保存数据前触发,05是新增条目的时候触发;
之后我们点击编辑器,在include内添加如下代码,最好是建一个通用的结构,之后可以重复使用:
*&---------------------------------------------------------------------*
*& 包含 ZBC_I_SM30_LOG
*&---------------------------------------------------------------------*
DEFINE assign_field.
ASSIGN COMPONENT &1 OF STRUCTURE <ls_maintview> TO <lv_field>.
IF sy-subrc = 0.
IF <lv_field> IS ASSIGNED.
<lv_field> = &2.
ENDIF.
ENDIF.
END-OF-DEFINITION.
FORM create_entry.
FIELD-SYMBOLS:
<ls_maintview> TYPE any,
<lv_field> TYPE any.
CHECK x_header-maintview IS NOT INITIAL.
ASSIGN (x_header-maintview) TO <ls_maintview>.
CHECK sy-subrc = 0.
CHECK <ls_maintview> IS ASSIGNED.
assign_field ‘CREATED_DATE’ sy-datum.
assign_field ‘CREATED_TIME’ sy-uzeit.
assign_field ‘CREATED_USER’ sy-uname.
ENDFORM.
FORM change_entry.
DATA:
lo_data TYPE REF TO data,
lv_tabix TYPE sy-tabix.
FIELD-SYMBOLS:
<ls_maintview> TYPE any,
<lv_field> TYPE any.
IF x_header-maintview IS NOT INITIAL.
CREATE DATA lo_data TYPE (x_header-maintview).
IF lo_data IS BOUND.
ASSIGN lo_data->* TO <ls_maintview>.
ENDIF.
IF <ls_maintview> IS ASSIGNED.
IF <action> = ‘U’.
LOOP AT total.
READ TABLE extract WITH KEY <vim_xtotal_key>.
IF sy-subrc = 0.
lv_tabix = sy-tabix.
ELSE.
CONTINUE.
ENDIF.
IF <action> = ‘U’.
CLEAR <ls_maintview>.
MOVE-CORRESPONDING <vim_total_struc> TO <ls_maintview>.
assign_field ‘CHANGED_DATE’ sy-datum.
assign_field ‘CHANGED_TIME’ sy-uzeit.
assign_field ‘CHANGED_USER’ sy-uname.
MOVE-CORRESPONDING <ls_maintview> TO <vim_total_struc>.
MODIFY total.
extract = total.
MODIFY extract INDEX lv_tabix.
ENDIF.
ENDLOOP.
ENDIF.
ENDIF.
ENDIF.
sy-subrc = 0.
ENDFORM.