Tuesday, 23 December 2014

Create table event

Validation on table maintenance

While creating the tables, sometimes we may need to update the table fields in the background. For example, if we create a table with 10 fields and in those 10 fields there are 2 fields which are User name and the Date, these 2 fields should update automatically when a record Inserted/Updated in the table. It also can be used when we want provide various validations on creating, changing or saving entries in a table.

Event Handling

When we provide event handling on table maintenance, table fields will be updated automatically when a new record inserted into the table or an existed record changes through the table maintenance generator.
To achieve the event handling in table maintenance, we need to create a table maintenance generator and then create events. Process is as follows:

Create a table ZTAB with the following fields:



Create a table maintenance generator for the table:

To create table maintenance generator go Utilities -> Table Maintenance Generator and provide following information:
Authorization Group: &NC&
Function group: Any suitable FC Eg: ZBC420XX
Maintenance type: Two Step
Maint. screen no: Overview screen 8005
                             Single screen    8006
Click on Create.

Create Events for table Maintenance:

To create events on table maintenance go to Environment -> Modification -> Events -> New Entries


List of events available:

Choose maintenance event 05-Creating New Entry and provide a name of the event (This will become perform/subroutine).
Click on Editor Button to provide event handling code.

A popup will be appear, It will ask to create an include program, in which we'll write the event handling code.
Click on continue.


Write the following code in include for this event.
FORM new_entry.
  DATA: v_nchar TYPE i.
  CONSTANTS: c_conlimit TYPE i VALUE 3.
  ztab-changed_by = sy-uname.         " current user
  ztab-create_date = sy-datum.        " current date
  ztab-create_time = sy-uzeit.        " current time
* Validation on Country Field
  v_nchar = STRLEN( ztab-country ).
  IF v_nchar NE c_conlimit.
    MESSAGE i536.
  ENDIF.
ENDFORM.                    "New_Entry

Create Transaction Code:

Create a transaction code to run the table maintenance generator.
Go to Tcode SE93
Transaction code: ZTAB
Short text: Test Table Maintenance Events
Start Object: Transaction with parameters (Parameter Transaction)

Transaction: SM30
Skip first Screen: Check ON
Default Values:
View Name      ZTAB
Update           X


Execute the transaction code:

Execute the transaction code ZTAB and click on New Entries button.


Now provide a valid value in country field and remain blank another fields.

Click Enter (NEW_ENTRY event will trigger) and current User, date and time will be automatically filled in respective fields.

Save the entry and check in table.

One more entry by table maintenance:
Write country code as US in place of USA and press Enter
Information message shown that entry was not saved.

Now provide USA as country code and save entry.

No comments:

Post a Comment