Custom Validation in TMG on Enter key
This post describes about how to validate newly created, changed & deleted entry in TMG. We have several events in TMG which can be used according to our requirement but there is not any event for Enter key. We need to write our code in PAI of generated screen.
Requirement: If we create any new record in table ZMTEST, field CARRID should be validated from SFLIGHT table. If CARRID doesn’t exist in SFLIGHT, we need to display error message and data should not be saved.
Table: ZMTEST
Step 1: Create TMG for Table: ZMTEST and Double click on Generated screen number
Step 2: It will navigate you to Screen flow logic, Create a module in PAI. Double click on Module name to create PAI Module. Provide the Include Program name and Save.
Step 3: Write below code in the created Module.
TOTAL: At Runtime it contains all the entries – Newly added & deleted along with the action performed – Insert, Update, Delete or None.
<VIM_TOTAL_STRUC>: will be assigned automatically inside loop and it will have same structure of table.
<ACTION>: it contains value of action performed. N: New, U: Updated, space: No change, D: Deleted and X: Deleted new entry.
VIM_ABORT_SAVING: Flag to abort the save.
Code:
MODULE common_validate INPUT.
* Local declarations
TYPES : BEGIN OF ty_sflight,
carrid TYPE s_carr_id,
END OF ty_sflight.
DATA : lt_zmtest TYPE STANDARD TABLE OF zmtest,
wa_zmtest TYPE zmtest,
lt_sflight TYPE STANDARD TABLE OF ty_sflight.
FIELD-SYMBOLS : <fs_zmtest> TYPE zmtest.
* TOTAL: At Runtime it contains all the entries – Newly added &
* deleted along with the action performed – Insert, Update
* Delete or None
* <VIM_TOTAL_STRUC> will be assigned automatically inside loop
* and it will have same structure of table.
* <ACTION> it containg value of action performed.
* N: New, U: Updated, space: No change, D: Deleted
* and X: Deleted new entry.
* Here we are storing Newly created entries in table LT_ZMTEST
LOOP AT total.
IF <vim_total_struc> IS ASSIGNED AND <action> = ‘N’.
MOVE-CORRESPONDING <vim_total_struc> TO wa_zmtest.
APPEND wa_zmtest TO lt_zmtest.
CLEAR : wa_zmtest.
ENDIF.
ENDLOOP.
* Extracting CARRID from Table SFLIGHT to vlidate newly
* added records
IF lt_zmtest[] IS NOT INITIAL.
SELECT carrid FROM sflight INTO TABLE lt_sflight
FOR ALL ENTRIES IN lt_zmtest
WHERE carrid EQ lt_zmtest–carrid.
IF sy–subrc IS INITIAL.
SORT lt_sflight BY carrid.
ENDIF.
* VIM_ABORT_SAVING: Flag to abort the save
UNASSIGN <fs_zmtest>.
LOOP AT lt_zmtest ASSIGNING <fs_zmtest>.
READ TABLE lt_sflight TRANSPORTING NO FIELDS WITH KEY carrid = <fs_zmtest>–carrid
BINARY SEARCH.
IF sy–subrc IS NOT INITIAL.
vim_abort_saving = abap_true.
MESSAGE e368(00) WITH <fs_zmtest>–carrid ‘does not exist in table Sflight’.
ENDIF.
ENDLOOP.
UNASSIGN <fs_zmtest>.
ENDIF.
ENDMODULE. ” COMMON_VALIDATE INPUT