Wednesday 22 March 2023

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

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

SAP ABAP Certification, SAP ABAP Guides, SAP ABAP Career, SAP ABAP Skills, SAP ABAP Jobs, SAP ABAP Learning, SAP ABAP Guides

Table Structure

SAP ABAP Certification, SAP ABAP Guides, SAP ABAP Career, SAP ABAP Skills, SAP ABAP Jobs, SAP ABAP Learning, SAP ABAP Guides

Open Maintenance View

I assume you have created a maintenance view. And willing to add event to validate data.

SAP ABAP Certification, SAP ABAP Guides, SAP ABAP Career, SAP ABAP Skills, SAP ABAP Jobs, SAP ABAP Learning, SAP ABAP Guides

Add Event to Maintenance View

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

SAP ABAP Certification, SAP ABAP Guides, SAP ABAP Career, SAP ABAP Skills, SAP ABAP Jobs, SAP ABAP Learning, SAP ABAP Guides

Add Event and Form Name

Code Block

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

SAP ABAP Certification, SAP ABAP Guides, SAP ABAP Career, SAP ABAP Skills, SAP ABAP Jobs, SAP ABAP Learning, SAP ABAP Guides

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.

SAP ABAP Certification, SAP ABAP Guides, SAP ABAP Career, SAP ABAP Skills, SAP ABAP Jobs, SAP ABAP Learning, SAP ABAP Guides

Try to save data with invalid company code

No comments:

Post a Comment