In this blog post, I would like to show you how to validate data on maintenance view screen before save.
Maintenance views are very useful interfaces to create a viewer and editor for database tables. And with events and search helps, we can make them even more useful.
In this blog post, we will create a table, where we can map company codes to special ranges for purchase orders. Table will have only 3 fields, client, company code and range. Before saving data on maintenance view, we will check both company code and range and if they are not valid we will warn user and will not let data to be saved.
Table
I assume you have created a maintenance view. And willing to add event to validate data. If you don’t know how to create maintenance view, please read on that link.
Add event before save and give a form name to be called when event is triggered.
Code Block
Click open editor and create form that you have given above ( BEFORE_SAVE in that example)
Above, from line 9 to 15 are used to collect new records or changed records data to recs internal table. total table contains all records and when we loop on total records, <vim_total_struc> field-symbol is assigned, by checking <action> field-symbol we can learn about state of record, if it is new or updated or being deleted.
Later on line 19, recs table is passed to a class that contains validation logic. That class method returns a status structure. If result is not success. we are setting vim_abort_saving = abap_true on line 21. In that way, saving will not happen.
And we are showing message to inform user on line 22.
Here are the lines, that will be useful for you.
FORM before_save.
"Collect records to be checked
DATA : recs TYPE TABLE OF zmm_t_po_nr_cust,
rec TYPE zmm_t_po_nr_cust.
LOOP AT total.
IF <vim_total_struc> IS ASSIGNED AND ( <action> EQ 'N' OR <action> EQ 'U' ).
CLEAR rec.
MOVE-CORRESPONDING <vim_total_struc> TO rec.
APPEND rec TO recs.
ENDIF.
ENDLOOP.
"Validate changes
IF recs IS NOT INITIAL.
"Place your logic below
DATA(status) = zmm_cl_po_numbering=>validate_new_entries( recs = recs ).
IF status-status NE zutils_cl_defs=>c_stat_success.
vim_abort_saving = abap_true. "To abort saving
MESSAGE status-status_text TYPE 'S' DISPLAY LIKE 'E'.
ENDIF.
ENDIF.
ENDFORM.
Activate your changes and go to sm30 to test.
That is all. I hope that is useful for you.
Thanks for reading.
Reference Links
https://blogs.sap.com/2015/10/29/validate-data-in-table-maintenance-generator-event/
https://blogs.sap.com/2012/03/12/viewmaintain-subset-of-data-in-table-maintenance-generator/