Home > ABAP, Dictionary, SAP > Custom Validation on Table Maintenance Generator (SM30)

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

About these ads
Categories: ABAP, Dictionary, SAP
  1. RG
    April 26, 2011 at 3:06 pm | #1

    Thanks a lot Marc. This was really helpful.

  2. venki
    June 22, 2011 at 10:23 pm | #2

    hi, ihave a work on object on ” default cost objects for absences and wage types’. fields in custom table are bukrs,butxt,subty(in t554s table),atext(in t554s),lgart(in t511),lgtxt in t512t,aufnr in viaufk_afvc,vornr in afvc.
    reqiured fields to save a record in this custom table are company code, attendence type or wage type and order no.
    here activity no is not satnd alone field and it always goes along with an order. routing no of operations in the order(aufpl)is the key field between tables viaufk_afvc and afvc tables to determine operation in an order. every operation /activity is assigned to an order.
    how to provide wage type and attendence tpye fields are mutually exclusive in a custom table so that either one of these can only exist in one record of this table.
    how to generate an error mesage if user tries to save record with both attendence type and wage type.
    how to use all standard error handling messages for this cutom table.
    PURPOSE :
    pupose of this enhancement is to develop a custom table where users can maintain a cost object for an attendace type and wage type by company code. there by user exit CATS0002 (EXIT_SAPLCATS_002) ON CAT2 transaction code can fetch the cost object of the entered attendence type from this custom table. cost ocject can be either a work order or combination of work order and activity no.

    • marcponty
      June 23, 2011 at 12:14 am | #3

      Hi Venki,

      Please correct me if I’m wrong, what you want to do is to prevent the user to input both the wage type and the attendance type in the same record. If that so, then you can apply the step by step above and only need to modify the code in form f_validate_entry.

      In form f_validate_entry, add below validation

      when equals N (new entry) or U (updated entry), raise error message if lwa_row-wage_type is not initial and lwa_row-attendance_type is not initial.

      Hope it helps.

      Regards,
      Ponty

  3. venki
    June 22, 2011 at 10:24 pm | #4

    waiting for ur help

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: