Events in Table Maintenance

Scenario: We have a following custom table which contains the fields “Date on which record was created” and “Name of the person who created the object”. We would like to have these to be filled up with SY-DATUM and SY-UNAME respectively.   

 

Go to Table Maintenance Generator: 

<!--[if gte vml 1]><v:shape id="_x0000_i1026" type="#_x0000_t75" style='width:356.25pt;height:258pt'> <v:imagedata src="file:///C:/DOCUME~1/PARVAT~1/LOCALS~1/Temp/msoclip1/01/clip_image003.png" o:title=""/> </v:shape><![endif]-->  

Enter the details as shown below:

<!--[if gte vml 1]><v:shape id="_x0000_i1027" type="#_x0000_t75" style='width:386.25pt;height:357.75pt'> <v:imagedata src="file:///C:/DOCUME~1/PARVAT~1/LOCALS~1/Temp/msoclip1/01/clip_image005.png" o:title=""/> </v:shape><![endif]--> 

Now click on Environment -> Modification -> Events 

<!--[if gte vml 1]><v:shape id="_x0000_i1028" type="#_x0000_t75" style='width:373.5pt;height:179.25pt'> <v:imagedata src="file:///C:/DOCUME~1/PARVAT~1/LOCALS~1/Temp/msoclip1/01/clip_image007.png" o:title=""/> </v:shape><![endif]--> 

Following screen is displayed.

<!--[if gte vml 1]><v:shape id="_x0000_i1029" type="#_x0000_t75" style='width:285.75pt;height:192.75pt'> <v:imagedata src="file:///C:/DOCUME~1/PARVAT~1/LOCALS~1/Temp/msoclip1/01/clip_image009.png" o:title=""/> </v:shape><![endif]-->  

Click on F4. Following entries are displayed: 

<!--[if gte vml 1]><v:shape id="_x0000_i1030" type="#_x0000_t75" style='width:240.75pt;height:169.5pt'> <v:imagedata src="file:///C:/DOCUME~1/PARVAT~1/LOCALS~1/Temp/msoclip1/01/clip_image011.png" o:title=""/> </v:shape><![endif]-->  

Here you can observe that there are different types of events available like before saving the data, after saving the data, before deleting, after deleting and others. Let us go with “Creating a new entry”. 

<!--[if gte vml 1]><v:shape id="_x0000_i1031" type="#_x0000_t75" style='width:286.5pt;height:195.75pt'> <v:imagedata src="file:///C:/DOCUME~1/PARVAT~1/LOCALS~1/Temp/msoclip1/01/clip_image013.png" o:title=""/> </v:shape><![endif]-->  

Click on the button in the Editor column and enter the following code: (Please note that you should also code FORM and ENDFORM as well).  

<!--[if gte vml 1]><v:shape id="_x0000_i1032" type="#_x0000_t75" style='width:296.25pt;height:126pt'> <v:imagedata src="file:///C:/DOCUME~1/PARVAT~1/LOCALS~1/Temp/msoclip1/01/clip_image015.png" o:title=""/> </v:shape><![endif]-->  

Save and activate the table. 

Testing the scenario: 

Go to SM30 and try creating new entries. Do not enter the values for “Created on” and “Created by”. 

<!--[if gte vml 1]><v:shape id="_x0000_i1033" type="#_x0000_t75" style='width:268.5pt;height:145.5pt'> <v:imagedata src="file:///C:/DOCUME~1/PARVAT~1/LOCALS~1/Temp/msoclip1/01/clip_image017.png" o:title=""/> </v:shape><![endif]-->  

Check your entries in the database table. You can observe that the date and user name are automatically filled-in. See the screenshot below: 

<!--[if gte vml 1]><v:shape id="_x0000_i1034" type="#_x0000_t75" style='width:210pt;height:23.25pt'> <v:imagedata src="file:///C:/DOCUME~1/PARVAT~1/LOCALS~1/Temp/msoclip1/01/clip_image019.png" o:title=""/> </v:shape><![endif]-->

 

 

 

Using the Table Maintenance event you can populate those fields.

 

You need to use the event 01

 

<!--[CodeBlockStart:52654a2c-d684-49e4-9ce0-8539241e8edb][excluded]-->
FORM f_actions_event_01.
IF vim_abort_saving IS INITIAL.
* Update Change history fields
INCLUDE ZZFSAI086_UPD_CHG_HSTY .
ENDIF.
ENDFORM. " f_actions_event_01
<!--[CodeBlockEnd:52654a2c-d684-49e4-9ce0-8539241e8edb]-->
 

 

include code

<!--[CodeBlockStart:01d5fda1-f6ea-423d-b8c9-72415f36fe3e][excluded]-->
*&---------------------------------------------------------------------*
*& Include ZZFSAI086_UPD_CHG_HSTY
*&---------------------------------------------------------------------*

DATA:

lv_index1 TYPE sy-tabix, "Index for Table 'TOTAL'
lv_index2 TYPE sy-tabix, "Index for Table 'EXTRACT'
lr_data TYPE REF TO data, "Reference Data object
lv_action TYPE char1, "Variable for View action
lv_mark TYPE char1. "Variable for View mark

 

FIELD-SYMBOLS:

<lf_fval> TYPE ANY, "For field value
<ls_str> TYPE ANY, "For data string
<ls_xfrom> TYPE x, "Hexadecimal value of from value
<ls_xto> TYPE x. "Hexadecimal value of to value
* Dynamic creation of data object using the table name
CREATE DATA lr_data TYPE (x_header-viewname).
ASSIGN lr_data->* TO <ls_str>.

CLEAR: lv_index1, lv_index2, lr_data.
* Looping through all the records in the M.View
LOOP AT total.
CLEAR lv_index1.
lv_index1 = sy-tabix.
READ TABLE extract WITH KEY total.
IF sy-subrc EQ 0.
lv_index2 = sy-tabix.
ELSE.
CLEAR lv_index2.
ENDIF.
ASSIGN total TO <ls_xfrom> CASTING.
ASSIGN <ls_str> TO <ls_xto> CASTING.
<ls_xto> = <ls_xfrom>.
MOVE:
<action> TO lv_action,
<mark> TO lv_mark.
CASE <action>.
WHEN neuer_eintrag. " New entry
* If there is a new entry fill created by / date
ASSIGN COMPONENT 'CRNAM' OF STRUCTURE <ls_str> TO <lf_fval>.
IF sy-subrc = 0.
<lf_fval> = sy-uname.
ENDIF.
ASSIGN COMPONENT 'CRDAT' OF STRUCTURE <ls_str> TO <lf_fval>.
IF sy-subrc = 0.
<lf_fval> = sy-datum.
ENDIF.
* If Changed by / date have to be blanked out if e.g. an entry is
 copied
ASSIGN COMPONENT 'AENAM' OF STRUCTURE <ls_str> TO <lf_fval>.
IF sy-subrc = 0.
CLEAR <lf_fval>.
ENDIF.

ASSIGN COMPONENT 'AEDAT' OF STRUCTURE <ls_str> TO <lf_fval>.
IF sy-subrc = 0.
CLEAR <lf_fval>.
ENDIF.

WHEN aendern. "Updated entry
* Update to an existing entry fill in changed by / date
ASSIGN COMPONENT 'AENAM' OF STRUCTURE <ls_str> TO <lf_fval>.
IF sy-subrc = 0.
<lf_fval> = sy-uname.
ENDIF.

ASSIGN COMPONENT 'AEDAT' OF STRUCTURE <ls_str> TO <lf_fval>.
IF sy-subrc = 0.
<lf_fval> = sy-datum.
ENDIF.
ENDCASE.

*(make desired changes to the line total)

* Changes to be incorporated only when update or new entry.
CHECK <action> EQ aendern OR <action> EQ neuer_eintrag.
CLEAR: total,extract.
ASSIGN <ls_str> TO <ls_xfrom> CASTING.
ASSIGN total TO <ls_xto> CASTING.
<ls_xto> = <ls_xfrom>.
MOVE :
lv_action TO <action>,
lv_mark TO <mark>.
CONCATENATE total lv_action lv_mark INTO total.
MODIFY total from total INDEX lv_index1.
* modify extract if this is necessary
CHECK lv_index2 GT 0.
extract = total.
MODIFY extract from extract INDEX lv_index2.
ENDLOOP.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值