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

背景

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

原理

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

步骤

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

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

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

本例的版本号ZZVER保存时自增,故在事件01中实现,对于实时填充字段值的需求,可以在05(new record)和21(update record)中实现

参考代码

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

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

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

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


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

  field-symbols:<ft_data> type standard table.

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


  assign component 'ACTION' of structure total to field-symbol(<fs_action>).
  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 <ft_data>.
    read table <ft_data> assigning field-symbol(<fs_data>) index lv_index.
    assign component 'ACTION' of structure <fs_data> to <fs_action>.
  endif.

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

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

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

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

  endif.

endform.
*&---------------------------------------------------------------------*
*&  保存前事件,可用于修改和校验值
*&---------------------------------------------------------------------*
form sub_before_save.
  field-symbols:<ft_data> 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 <ft_data>.
    loop at  <ft_data> assigning field-symbol(<fs_data>).
      assign component 'ACTION' of structure <fs_data> to field-symbol(<fs_field>).
      check sy-subrc eq 0.
      check <fs_field> eq 'N' or <fs_field> eq 'U'.
      unassign <fs_field>.

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

      condense <fs_field>.
      unassign <fs_field>.
    endloop.

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

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

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

      condense <fs_field>.
      modify total index lv_index.
      unassign <fs_field>.
    endloop.
  endif.
* <SIGNATURE>---------------------------------------------------------------------------------------+
* | Static Public Method ZCL_COMMON_FM=>BUILD_SM30_TOTAL
* +-------------------------------------------------------------------------------------------------+
* | [--->] I_TOTAL                        TYPE        ANY TABLE
* | [--->] I_TABNAME                      TYPE        TABNAME
* | [<-()] E_TOTAL                        TYPE REF TO DATA
* +--------------------------------------------------------------------------------------</SIGNATURE>
  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(<ft_table>).

    <ft_table> = i_total.

    e_total = lr_table.
  endmethod.

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值