Table maintenance generator is very powerful tool to generate simple data entry screen for tables. For tables which store configuration data i.e. rarely modified it not worth spending time creating module pool program for data entry. Table maintenance generator also gives programmer opportunity to modify standard behaviour by including additional logic. In this blog I will enhance standard table maintenance to automatically record created by and created date/time and also last modified by and last modified date/time.
I have this table which record additional information per plant. Instead of hard-coding this text we decided to create a table and put these values there in case it requires change in future.
Create table with ‘Display/Maintenance Allowed’
I have fields ERNAM, ERDAT, ERZET, AENAM, AEDAT, AEZET apart from fields which will hold actual data.
Create table maintenance from menu option Utilities->Table Maintenance Generator. Make sure to select ‘one step’ in Maintenance type.
With standard table maintenance you will have all field active and ready for input. Since we username and date times field to be filled automatically by program logic we need to hide them from table maintenance screen. To do that, in table maintenance screen double click on screen number.
It will take you to the flow logic of screen. Add module modify_element in PBO of screen.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
PROCESS BEFORE
OUTPUT
.
MODULE
LISTE_INITIALISIEREN.
*--------------------------------------------------------------------*
* Hide user name and date/time column
*--------------------------------------------------------------------*
MODULE
modify_element .
*--------------------------------------------------------------------*
LOOP
AT
EXTRACT
WITH
CONTROL
TCTRL_ZSD_PLANT_CODE CURSOR NEXTLINE.
MODULE
LISTE_SHOW_LISTE.
ENDLOOP
.
*
|
Save and double click on modify_element module to create module in new include.
In module modify_element copy following code. This code is going through the table control structure to hide columns.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
*----------------------------------------------------------------------*
***INCLUDE LZSD_PLANT_CODEO01 .
*----------------------------------------------------------------------*
*&---------------------------------------------------------------------*
*& Module MODIFY_ELEMENT OUTPUT
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
MODULE
modify_element
OUTPUT
.
LOOP
AT
<vim_tctrl>-cols
INTO
vim_tc_cols.
IF
vim_tc_cols-screen-name =
'ZSD_PLANT_CODE-ERNAM'
OR
vim_tc_cols-screen-name =
'ZSD_PLANT_CODE-ERDAT'
OR
vim_tc_cols-screen-name =
'ZSD_PLANT_CODE-ERZET'
OR
vim_tc_cols-screen-name =
'ZSD_PLANT_CODE-AENAM'
OR
vim_tc_cols-screen-name =
'ZSD_PLANT_CODE-AEDAT'
OR
vim_tc_cols-screen-name =
'ZSD_PLANT_CODE-AEZET'
.
vim_tc_cols-invisible = 1 .
MODIFY
<vim_tctrl>-cols
FROM
vim_tc_cols .
ENDIF
.
ENDLOOP
.
ENDMODULE
.
" MODIFY_ELEMENT OUTPUT
|
Save and activate all objects. Make sure the function group where table maintenance generator code is active including screen and our new include.
If you run table maintenance now you should not see these field.
Now it’s time to add code which will fill user name, date and time when records are created or changed.
Table Maintenance Generator offers events that during program processing and we programmer can add our own code to execute at this point. Very similar to user-exits. Events in which we are interested in are 05 which executes when new entry is create and event 01 which triggers before saving the data in database. We are going to add code at these event which will populate our fields.
Open Table Maintenance Generator in change mode and choose menu option Environment->Modification->Events.
Click ok on dialog box which say do not change SAP data. On next screen press ‘New Entries’ and add modules at 05 and 01 events.
Click on the editor button next to form routine, create include if required, and put following code in each of these form routines.
1
2
3
4
5
6
7
8
|
*&---------------------------------------------------------------------*
*& Form ZSDPLANT_NEW_ENTRY
*&---------------------------------------------------------------------*
FORM
zsdplant_new_entry .
zsd_plant_code-ernam = sy-uname .
zsd_plant_code-erdat = sy-datum .
zsd_plant_code-erzet = sy-uzeit .
ENDFORM
.
"zsdplant_new_entry
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
|
*&---------------------------------------------------------------------*
*& Form ZSDPLANT_MODIFY_SAVE
*&---------------------------------------------------------------------*
FORM
zsdplant_modify_save .
FIELD-SYMBOLS
: <fs_field>
TYPE
ANY
.
LOOP
AT
total .
CHECK
<action> EQ aendern.
ASSIGN
COMPONENT
'AENAM'
OF
STRUCTURE
<vim_total_struc>
TO
<fs_field> .
IF
sy-subrc = 0 .
<fs_field> = sy-uname .
ENDIF
.
ASSIGN
COMPONENT
'AEDAT'
OF
STRUCTURE
<vim_total_struc>
TO
<fs_field> .
IF
sy-subrc = 0 .
<fs_field> = sy-datum .
ENDIF
.
ASSIGN
COMPONENT
'AEZET'
OF
STRUCTURE
<vim_total_struc>
TO
<fs_field> .
IF
sy-subrc = 0 .
<fs_field> = sy-uzeit .
ENDIF
.
READ
TABLE
extract
WITH
KEY
<vim_xtotal_key>.
IF
sy-subrc = 0.
extract = total .
MODIFY
extract
INDEX
sy-tabix.
ENDIF
.
MODIFY
total.
ENDLOOP
.
ENDFORM
.
"ZSDPLANT_MODIFY_SAVE
|
Save and Activate all objects. To activate function group you can go to SE80 and activate from there.
I have created two entries in table maintenance and then changed one. This is how table contents look like.