ABAP SM30表维护程序 自动更新隐藏字段值

背景

在表维护程序SM30的使用过程中,经常需要自动填充和修改一些值,比较常规的做法是修改屏幕逻辑流,但是如果重新激活了SM30,那么逻辑流就要重写,相对比较麻烦,之前接触过表维护程序中的‘事件’,经过研究得出以下方法

原理

事件编号 事件功能
01 保存前修改数据
05 创建数据时修改
21 退出单元格编辑时填充隐藏字段

步骤

1、生成表维护程序,建议做一个维护视图

2、针对事件分别创建子例程

3、实现如下代码,可根据实际需求调整

本例的版本号ZZVER自增,故在事件01中实现,对于实时填充字段的需求,可以在05和21中实现

参考代码

*&---------------------------------------------------------------------*
*&  新增条目事件
*&---------------------------------------------------------------------*
form sub_new_entry.

  assign component 'CREATED_BY' of structure  to field-symbol().
  if  is assigned and  is initial.
     = sy-uname.
    unassign .
  endif.

  assign component 'CREATED_ON' of structure  to .
  if  is assigned.
     = sy-datum.
    unassign .
  endif.

  assign component 'CREATED_AT' of structure  to .
  if  is assigned.
     = sy-uzeit.
    unassign .
  endif.


endform.
*&---------------------------------------------------------------------*
*&  修改条目事件
*&---------------------------------------------------------------------*
form sub_change_entry.

  field-symbols: type standard table.

  read table total with key .
  check sy-subrc eq 0.
  data(lv_index) = sy-tabix.


  assign component 'ACTION' of structure total to field-symbol().
  if sy-subrc ne 0.
    "对于视图簇,可能需要转换total至表结构
    data(lr_data) = zcl_common_fm=>build_sm30_total(
          i_total = total[]
          i_tabname = view_name
          ).

    assign lr_data->* to .
    read table  assigning field-symbol() index lv_index.
    assign component 'ACTION' of structure  to .
  endif.

  if  is assigned and  ne 'N'."不是新增行则继续 .

    assign component 'CHANGED_BY' of structure  to field-symbol().
    if  is assigned.
       = sy-uname.
      unassign .
    endif.

    assign component 'CHANGED_ON' of structure  to .
    if  is assigned.
       = sy-datum.
      unassign .
    endif.

    assign component 'CHANGED_AT' of structure  to .
    if  is assigned.
       = sy-uzeit.
      unassign .
    endif.

  endif.

endform.
*&---------------------------------------------------------------------*
*&  保存前事件,可用于修改和校验值
*&---------------------------------------------------------------------*
form sub_before_save.
  field-symbols: type standard table.


  if objh-objecttype eq 'C'."视图簇
    "对于视图簇,可能需要转换total至表结构
    data(lr_data) = zcl_common_fm=>build_sm30_total(
          i_total = total[]
          i_tabname = view_name
          ).

    assign lr_data->* to .
    loop at   assigning field-symbol().
      assign component 'ACTION' of structure  to field-symbol().
      check sy-subrc eq 0.
      check  eq 'N' or  eq 'U'.
      unassign .

      assign component 'ZZVER' of structure  to .
      if  is assigned and  is initial.
         = 1.
      else.
         =  + 1.
      endif.

      condense .
      unassign .
    endloop.

    total[] =  .
  else."视图
    loop at total.
      data(lv_index) = sy-tabix.

      assign component 'ACTION' of structure total to .
      if sy-subrc eq 0.
        check  eq 'N' or  eq 'U'.
        unassign .
      endif.

      assign component 'ZZVER' of structure total to .
      if  is assigned and  is initial.
         = 1.
      else.
         =  + 1.
      endif.

      condense .
      modify total index lv_index.
      unassign .
    endloop.
  endif.
* ---------------------------------------------------------------------------------------+
* | Static Public Method ZCL_COMMON_FM=>BUILD_SM30_TOTAL
* +-------------------------------------------------------------------------------------------------+
* | [--->] I_TOTAL                        TYPE        ANY TABLE
* | [--->] I_TABNAME                      TYPE        TABNAME
* | [<-()] E_TOTAL                        TYPE REF TO DATA
* +--------------------------------------------------------------------------------------
  method build_sm30_total.
    data:lr_table type ref to data.

    "获取数据库表对应的结构
    data(lo_struct) = cast cl_abap_structdescr( cl_abap_tabledescr=>describe_by_name( i_tabname ) ).
    data(lt_comp) = lo_struct->get_components( ).

    "添加action && mark字段
    data(lo_element) = cast cl_abap_datadescr( cl_abap_elemdescr=>describe_by_name( 'CHAR01') ).
    lt_comp = value #( base lt_comp ( name = 'ACTION' type = lo_element ) ( name = 'MARK' type = lo_element ) ).
    lo_struct = cl_abap_structdescr=>create( p_components = lt_comp ).
    data(lo_table) = cl_abap_tabledescr=>create( p_line_type = lo_struct ).

    create data lr_table type handle lo_table.
    assign lr_table->* to field-symbol().

     = i_total.

    e_total = lr_table.
  endmethod.

 

你可能感兴趣的:(Enhancement)