March 6, 2015

Table maintenance and other features

In the previous post I showed how to make use of some of the features that SAP provides when making tables.

We made a table, we generated a table maintenance screen, we selected events and put come code there to show how events work. This time I would like to show another feature that we could use when we program events.

Ok, let's code;

First of all, let’s add another field and call it 'AGE' with the data element 'INT2'. Check the screen.


2. Activate the table

3. Update the dynpro to include the new field.
4. Go to Utilities / Table Maintenance Generator menu.


5. Go to Environment / Modification / Events menu. Click 'Ok' on the message box.


6. Add a new entry, select option '01' (Before saving the data in the database) and type 'ON_BEFORE_SAVE'

7. Click on the editor button.


8. Select the include previously created or create a new one. I'll use the one that exists.


9. Enter the following code.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
form on_before_save.
  data wa_testtab type ztesttab.

  loop at total. " Check all the records
    if <vim_total_struc> is assigned.
      move-corresponding <vim_total_struc> to wa_testtab. " Get the line to check
    endif.

    if <action> ne 'D' and <action> ne 'X'. " Consider only create and update operations
      if wa_testtab-age not between 15 and 130.
        message 'Age is not valid. Must be between 1 and 130' type 'I' display like 'E'.
        vim_abort_saving = 'X'. " Abort operation
      endif.
    endif.
  endloop.
endform.

The variable 'total' is a table with header line which contains all the data of the table. 

The field symbol '<vim_total_struc>' is a structure that contains the line or tuple we are reading. Remember that at this point we haven't saved but we get the changes we've made to the record. Here we transfer the data from the field symbol to the work area of type 'WA_TESTTAB'.

The field symbol '<action>' indicates the action over the record. It can be 'I' (insert), 'U' (update), 'D' (delete) or 'X' (none). In this example we are going to validate when inserting or updating data.

The variable 'vim_abort_saving' is used to indicate if we cancel the operation. If the age is not between 15 and 130 we'll issue an error and SAP will not save the new entries or changes.

10. Activate the code.


11. Return and save.


12. Create a new entry without age and then try to save. The error 'Age is not valid. Must be between 15 and 130' will arise. No matter what you do, unless you type a valid age the data won't be saved. 



As you can see, you can make use of this feature to add more validations or manage your data in your table. 

Well, that's it for now. Hope it helps.
Please Share it! :)

No comments :

Post a Comment