3

How to Validate Data on Maintenance View(SM30) Before Save

 1 year ago
source link: https://blogs.sap.com/2023/03/21/how-to-validate-data-on-maintenance-viewsm30-before-save/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client
March 21, 2023 2 minute read

How to Validate Data on Maintenance View(SM30) Before Save

0 2 142

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

Table%20Structure

Table Structure

Open%20Maintenance%20View

Open Maintenance View

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%20Event%20to%20Maintenance%20View

Add Event to Maintenance View

Add event before save and give a form name to be called when event is triggered.

Add%20Event%20and%20Form%20Name

Add Event and Form Name

Code Block

Click open editor and create form that you have given above ( BEFORE_SAVE in that example)

FORM%20Content%20of%20BEFORE_SAVE

FORM Content of BEFORE_SAVE

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.

Try%20to%20save%20data%20with%20invalid%20company%20code

Try to save data with invalid company code

That is all. I hope that is useful for you.

Thanks for reading.

Reference Link

https://blogs.sap.com/2015/10/29/validate-data-in-table-maintenance-generator-event/


Recommend

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK