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:
For example, I want to add validation on CURR field, which should not be empty, and must have value either ‘USD’ or ‘IDR’.
1. Create Action on Appropriate Event in Table Maintenance Generator
I assume that the table maintenance generator is already created. Now, we have to define where the validation will take place. Go to Environment –> Modification –> Events
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 pick the event ’01’ – Before saving data in the database.
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.
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'. 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