Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

Monday, July 5, 2021 10:18 AM

http://saptechnical.com/Tutorials/ABAP/TableMaintenance/events
.htm
Custom Validation on Table Maintenance
Generator (SM30)

To add a validation to prevent invalid data input while using table maintenance
transaction (SM30) is a common situation. However, it may be tricky at first if we
don’t know the trick behind. So, below is the step to add the custom validation.

In this example I will use table with definition as below:


• <action> is a flag that will have values as ‘N’ if a new
entry is made or as ‘U’ if existing entry is modified.
• <vim_total_struc> is a structure that holds the current
looped value on TOTAL table.
• it_total_zdata, internal table should be of type table z-
table. For example, I want to add validation on CURR field, which should not be empty,
and must have value either ‘USD’ or ‘IDR’.
LOOP AT total.
IF <action> EQ ‘N’ OR <action> EQ ‘U’. 1. Create Action on Appropriate Event in Table Maintenance Generator
APPEND <vim_total_struc> TO it_total_zdata. I assume that the table maintenance generator is already created. Now, we have
ENDIF. to define where the validation will take place. Go to Environment –>
ENDLOOP. Modification –> Events

IF it_total_zdata[] IS NOT INITIAL.


* Perform validation
LOOP AT it_total_zdata.
IF it_total_zdata-name NE ‘TESTNAME’.
MESSAGE ‘Name is not TESTNAME’ TYPE
‘S’ DISPLAY LIKE ‘E’.
vim_abort_saving = c_abrt_save.
sy–subrc = 4.
EXIT.
ENDIF.
ENDLOOP.
ENDIF.
There, we could register subroutine call to an event. For list of event, just press F4
on the left most column, which will bring up a list of event code and its event
description. For our case, we want to do validation before data is saved, so we will
From <https://blogs.sap.com/2015/10/29/validate-data-in-table-maintenance- pick the event ’01’ – Before saving data in the database.
generator-event/>
After that, we have to define a name of subroutine, for example
‘F_VALIDATE_ENTRY’. Click on the ‘editor’ button, and a popup asking where to
put the new subroutine will come. Create new include.

REPORT THIS AD

2. Add the validation code

In the new include, add below code:

form f_validate_entry.
DATA lwa_row TYPE ztesttable.
LOOP AT total.
clear lwa_row.
if <vim_total_struc> is ASSIGNED.
MOVE-CORRESPONDING <vim_total_struc> to lwa_row.
endif.
if <action> NE 'D' and <action> is NOT INITIAL and <action> NE 'X'.
if lwa_row-curr NE 'USD' and lwa_row-curr NE 'IDR'.
MESSAGE 'Currency must be USD or IDR' TYPE 'S' DISPLAY LIKE 'E'.

events in maintance view Page 1


MESSAGE 'Currency must be USD or IDR' TYPE 'S' DISPLAY LIKE 'E'.
vim_abort_saving = 'X'.
exit.
endif.
endif.
ENDLOOP.
ENDFORM.

There are some global variable used in this code, which make it tricky if we don’t
know what it contains. Variable ‘total’ will contain all entries in the table: new
entries, newly deleted entries, and existing entries, along with the action
performed on that entry (Insert/Update/Delete/None). However, this variable
come with only one field struct, so we have to do string offset, which is not really
elegant way. Fortunately, in each loop of ‘total’, it will automatically assign itself to
<vim_total_struc> which have the same struct with the table. From there, we
could validate the entry. There’s also <action> variable, which also automatically
assigned during loop, that show the action performed for that entries (value list as
below) The variable ‘vim_abort_saving’ will be flag to abort the save.

List of value for action

N = New entry

U = Updated entry

D = Deleted entry

X = Deleted new entry

blank = No change on entry

From <https://musicodez.wordpress.com/2010/09/13/custom-validation-on-table-maintenance-
generator-sm30/>

Using Event 5 in Table


Maintenance Generator For Data
Validation
Requirement:
Validasi new entry pada saat maintain table ZDT0150 sebagai berikut:

Resolution:
Buat event 05 pada table maintenance generator dengan subroutine F_VALIDATE. Cara membuat event sama seperti
yang pernah disampaikan pada postingan ini di step no.4.
Structure table ZDT0150:

events in maintance view Page 2


Structure table ZDT0150:

Source code:
1 *&---------------------------------------------------------------------*
2 *& Form f_validate
3 *&---------------------------------------------------------------------*
4 * text
5 *----------------------------------------------------------------------*
6 FORM f_validate.
7 DATA: ld_pertext(7),
8 ld_dattext(10).
9 CALL FUNCTION 'CONVERSION_EXIT_PERI6_OUTPUT'
10 EXPORTING
11 input = zdt0150-spmon
12 IMPORTING
13 output = ld_pertext
14 EXCEPTIONS
15 OTHERS = 1.
16 IF zdt0150-spmon <> zdt0150-datab(6).
17 WRITE zdt0150-datab TO ld_dattext DD/MM/YYYY.
18 MESSAGE e006(aq) WITH 'Date' ld_dattext 'is not in period' ld_pertext.
19 ENDIF.
20 IF zdt0150-spmon <> zdt0150-datbi(6).
21 WRITE zdt0150-datbi TO ld_dattext DD/MM/YYYY.
22 MESSAGE e006(aq) WITH 'Date' ld_dattext 'is not in period' ld_pertext.
23 ENDIF.
24 ENDFORM. "f_validate

From <https://belajarabap.wordpress.com/2019/03/08/using-event-5-in-table-maintenance-generator-for-data-validation/>

events in maintance view Page 3

You might also like