SAP ABAP  Table Maintenance Generator (TMG) Events 1 to 5 – Complete Usage with Real-Time Examples
Share

[[{“value”:”

            Hi , 

In this blog, I’ll explain how to use SAP Table Maintenance Generator (TMG) Events 1 to 5 with simple, real-time examples.
These TMG events help developers add custom logic during table maintenance in transaction SM30 — such as data checks, logging, or automatic updates.
By understanding when each event is triggered, you can easily enhance the standard TMG behavior without changing SAP’s standard code.
This post will take you through all five events step by step — explaining what each one does, when it runs, and how to use it in real projects.

  1. Event 01 – Before saving the data in the database

  2. Event 02 – After saving the data in the database

  3. Event 03 – Before deleting the displayed data

  4. Event 04 – After deleting the displayed data

  5. Event 05 – Creating a new entry

Let’s start with how to create a Table Maintenance Generator (TMG) and the complete steps.

AI-assisted tools were used to refine, structure, and enhance the clarity of the content presented in this blog.
Step 1 :
I have created a database table With some Fields .
image.png
 Step 2 :
I have created a database table Log Entries  For Event 04 .

image.png
Step 3 :
Let’s create a Table Maintenance Generator (TMG).
image.png
Give The Function Group And Active the Function Group In  SE80 T Code

image.png
Step 4 :
Now, create or modify events: go to Modification → Events and select the event names.
image.png
Step 5 :
Let’s start with Event 1, its use case, and an example code.
Event 1 – Before Saving the Data in the Database.
Use Case / Real-Time Example:

  • Triggered before saving data in the database (Insert/Update).

  • Automatically fills audit fields: CHANGEDBY = current user, CHANGEDON = current date.

  • Ensures screen data (extract) and internal table (total) are updated before saving.

Real-time example: In an employee table, when a record is updated, Event 1 automatically logs who changed it and when.
Example Code :

 

*———————————————————————-*
***INCLUDE LZTAB_TMG_EVENTSF01.
*———————————————————————-*
Form save.
FIELD-SYMBOLS: <fs_field> TYPE any.
LOOP AT total.
CHECK <action> EQ aendern.
ASSIGN COMPONENT ‘CHANGEDBY’ OF STRUCTURE <vim_total_struc> TO <fs_field> .
IF sy-subrc = 0 .
<fs_field> = sy-uname .
ENDIF.
ASSIGN COMPONENT ‘CHANGEDON’ OF STRUCTURE <vim_total_struc> TO <fs_field> .
IF sy-subrc = 0 .
<fs_field> = sy-datum .
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.

When a user modifies a record, the system automatically updates the CHANGEDBY and CHANGEDON fields with the current user and date.
Example Output:
image.png

 

Step 6 :
Let’s start with Event 2, its use case, and an example code.
Event 2 –After Creating / Saving a Record.

Use Case / Real-Time Example:

  • Event 2 is triggered after a record is saved to the database.

  • It is commonly used for post-processing, such as:

    1. Sending email notifications after new data is created.

    2. Triggering workflow or alerts for users.

    3. Logging audit or additional details after data insertion.

Real-Time Example:

  • In your ZTAB_TMG_EVENTS table, after a new employee record is created:

    • An email is automatically sent to notify stakeholders.

The email contains a formatted HTML table with all employee details (EMPID, EMPNAME, CREATEDON, CREATEDBY, STATUS).
Example Code :

*———————————————————————-*
***INCLUDE LZTAB_TMG_EVENTSF03.
*———————————————————————-*
form Email.
DATA: lo_send_request TYPE REF TO cl_bcs,
lo_document TYPE REF TO cl_document_bcs,
lo_recipient TYPE REF TO if_recipient_bcs,
lo_bcs_exception TYPE REF TO cx_bcs,
lt_html_lines TYPE soli_tab,
lv_html TYPE string,
lv_subj TYPE so_obj_des,
lv_sent TYPE abap_bool
lv_email TYPE adr6-smtp_addr VALUE ‘test@.com’.

DATA: lt_events TYPE TABLE OF ztab_tmg_events,
ls_event TYPE ztab_tmg_events.

*———————————————————————*
* 1️⃣ Read data from table
*———————————————————————*
SELECT * FROM ztab_tmg_events INTO TABLE _events.

IF lt_events IS INITIAL.
MESSAGE ‘No data found in ZTAB_TMG_EVENTS’ TYPE ‘I’.
EXIT.
ENDIF.

*———————————————————————*
* 2️⃣ Build HTML Header
*———————————————————————*
CLEAR lt_html_lines.
DATA: BODY TYPE STRING.
BODY = ‘Hi, Please find the table data After Creating’.

CONCATENATE
‘<html><body>’
‘<p>’BODY'</p>’
‘<h3>Employee Data</h3>’
‘<table border=”1″ cellpadding=”10″ cellspacing=”1″ >’
‘<thead>’
‘<tr style=”background-color:#FFFF00;”>’
‘<th>EMPID</th>’
‘<th>EMPNAME</th>’
‘<th>CREATEDON</th>’
‘<th>CREATEDBY</th>’
‘<th>STATUS</th>’
‘</tr>’
‘</thead>’
‘<tbody>’
INTO lv_html.

APPEND lv_html TO lt_html_lines.
CLEAR lv_html.

” Loop through data rows and add table rows dynamically
LOOP AT lt_events INTO ls_event.
CONCATENATE
‘<tr>’
‘<td align=”center”>’ ls_event-empid ‘</td>’
‘<td>’ ls_event-empname ‘</td>’
‘<td>’ ls_event-createdon ‘</td>’
‘<td>’ ls_event-createdby ‘</td>’
‘<td>’ ls_event-status ‘</td>’
‘</tr>’
INTO lv_html.
APPEND lv_html TO lt_html_lines.
CLEAR lv_html.
ENDLOOP.

” Close table and HTML tags
APPEND ‘</tbody></table></body></html>’ TO lt_html_lines.

*———————————————————————*
* 4️⃣ Send Email using CL_BCS
*———————————————————————*
TRY.
lo_send_request = cl_bcs=>create_persistent( ).
lv_subj = |ZTAB_TMG_EVENTS Report – { sy-datum DATE = USER }|.

lo_document = cl_document_bcs=>create_document(
i_type = ‘HTM’
i_text = lt_html_lines
i_subject = lv_subj ).

lo_send_request->set_document( lo_document ).

lo_recipient = cl_cam_address_bcs=>create_internet_address( lv_email ).
lo_send_request->add_recipient( lo_recipient ).

lo_send_request->set_send_immediately( ‘X’ ).

lv_sent = lo_send_request->send( ).

IF lv_sent = abap_true.
COMMIT WORK AND WAIT.
MESSAGE |Email sent successfully to { lv_email }| TYPE ‘S’.
ELSE.
MESSAGE ‘Email sending failed’ TYPE ‘E’.
ENDIF.

CATCH cx_bcs INTO lo_bcs_exception.
MESSAGE lo_bcs_exception->get_text( ) TYPE ‘E’.
ENDTRY.
ENDFORM.

Example Output:
image.png
Step 6 :

Event 3 – Before Deleting a Record

Use Case / Real-Time Example:

  • Event 3 is triggered before a record is deleted from the table.

  • It is commonly used to:

    1. Validate records before deletion.

    2. Prevent deletion of critical or active records.

    3. Log information about records that are attempted to be deleted.

Real-Time Example:

  • In your ZCPY_TMG_EVENTS table, before deleting a record:

    • The system checks if the record’s STATUS is 'A' (Active).

    • If the record is active, deletion is blocked, and an error message is shown:

      “You cannot delete a record which is active.”

Only records that are not active can be deleted safely.

Example Code :

*———————————————————————*
* Event 01: Before deleting the data displayed
*———————————————————————*
FORM del.

DATA: lt_zcpy_tmg_events TYPE TABLE OF zcpy_tmg_events,
ls_zcpy_tmg_events TYPE zcpy_tmg_events.

“Collect records marked for deletion from TOTAL
LOOP AT total.
IF <mark> IS NOT INITIAL. “Correct system field for deletion mark
MOVE-CORRESPONDING <vim_total_struc> TO ls_zcpy_tmg_events.
APPEND ls_zcpy_tmg_events TO lt_zcpy_tmg_events.
CLEAR ls_zcpy_tmg_events.
ENDIF.
ENDLOOP.

“Validate before deletion
IF lt_zcpy_tmg_events IS NOT INITIAL.
LOOP AT lt_zcpy_tmg_events INTO DATA(ls_imp).
IF ls_imp-status = ‘A’. “Assume ‘A’ = Active
MESSAGE ‘You cannot delete a record which is active.’ TYPE ‘E’.
ENDIF.
ENDLOOP.
ENDIF.

ENDFORM.

Example Output :
image.png

Step 7 :

Event 4 – After Deleting the Displayed Data

Use Case / Real-Time Example:

  • Event 4 is triggered after records are deleted from the table.

  • It is mainly used to:

    1. Log deleted records for audit purposes.

    2. Perform any post-deletion processing like notifications or updates in related tables.

Real-Time Example:

  • In your ZTAB_TMG_EVENTS table:

    • When a record is deleted, the system stores information about the deleted record in a custom log table ZCPY_TMG_LOG.

    • The log includes details like EMPID (key field), action performed (DELETE), user name, date, and time.

    • This helps track who deleted what and when, which is critical for compliance and auditing.
      Example Code:

*———————————————————————*
* Event 04: After Deleting the Display Data
*———————————————————————*
FORM after_delete_data.

FIELD-SYMBOLS:
<wa_data> TYPE ztab_tmg_events,
<ls_total> TYPE any. ” Row of TOTAL table

DATA: ls_deleted TYPE ztab_tmg_events, ” Your TMG table structure
ls_log TYPE zcpy_tmg_log, ” Your custom log table
lv_id TYPE zcpy_tmg_log-id. ” Log ID

LOOP AT total ASSIGNING <ls_total>.

” Get the internal action flag for this record
ASSIGN COMPONENT ‘ACTION’ OF STRUCTURE <ls_total> TO <action>.

” Check if this record was deleted
IF <action> = ‘D’.

” Cast generic <ls_total> to actual structure
ASSIGN <ls_total> TO <wa_data> CASTING.

” Now data is properly available
ls_deleted = <wa_data>.
” Get next log ID (better: use number range in real system)
SELECT MAX( id )
INTO _id
FROM zcpy_tmg_log.

IF sy-subrc <> 0 OR lv_id IS INITIAL.
lv_id = 1.
ELSE.
lv_id = lv_id + 1.
ENDIF.

” Fill log entry
ls_log-id = lv_id.
ls_log-tabname = ‘ZTAB_TMG_EVENTS’. ” Table name
ls_log-keyfield = ls_deleted-empid. ” Replace with your actual key field
ls_log-action = ‘DELETE’.
ls_log-uuser = sy-uname.
ls_log-ddate = sy-datum.
ls_log-ttime = sy-uzeit.
” Insert into log table
INSERT zcpy_tmg_log FROM ls_log.
CLEAR: ls_deleted, ls_log.
ENDIF.
ENDLOOP.
ENDFORM.

Example Output :
image.png

Step 8 :

Event 5 – Before Creating a New Record

Use Case / Real-Time Example:

  • Event 5 is triggered before a new record is saved in the table.

  • It is mainly used to:

    1. Automatically assign key values (like Employee ID) to new records.

    2. Populate system fields like Created On and Created By.

  • This ensures consistency and reduces manual errors when creating new entries.

Real-Time Example:

  • In your ZTAB_TMG_EVENTS table:

    • When a user adds a new employee record, the system automatically generates a new EMPID based on the maximum existing ID.

    • The fields CREATEDON and CREATEDBY are filled with the current date and the username of the person creating the record.

    • This helps track who created the record and when, ensuring proper audit tracking.
      Example Code :

*———————————————————————*
***INCLUDE LZTAB_TMG_EVENTSF02.
*———————————————————————*
FORM CREATE.
DATA: LV_ID TYPE ZTAB_TMG_EVENTS-EMPID.

” Get the maximum EMPID from the table
SELECT MAX( EMPID )
INTO _ID
FROM ZTAB_TMG_EVENTS.

” Assign next EMPID
IF LV_ID IS INITIAL.
ZTAB_TMG_EVENTS-EMPID = 1.
ELSE.
ZTAB_TMG_EVENTS-EMPID = LV_ID + 1.
ENDIF.

” Populate system fields
ZTAB_TMG_EVENTS-CREATEDON = SY-DATUM.
ZTAB_TMG_EVENTS-CREATEDBY = SY-UNAME.

ENDFORM.

Thank You…..

 

“}]] 

  Read More Technology Blog Posts by Members articles 

#abap

By ali

Leave a Reply