Events / Triggers

Overview

Servoy provides an event model at the data layer, giving developers the opportunity to implement validation and execute business logic just before and after data changes are committed to the database. There are twelve Table Events, each of which may be bound to an entity or a global method.

Get Started

To work with your database columns, you should first connect to your database and open the table in the Table Editor, where you will manage your events. In the screenshot below we are viewing the events/triggers on the customers table within the example_data database.

Attaching an event handler

Double click on an event handler and choose whether or not it should be created in the entity or scope. In this case we are selecting the onRecordInsert event. In this case we chose to create the event handler within an entity scope. Each table can have a single entity scope associated with it.

Removing an event handler

Removing an event handler is simple, we can simply double click the method and select the none option. If we are no longer using the method it should also be removed from the entity/scope where it was first declared.

Why use on triggers

A typical use case for using an event trigger is to validate data just before an insert/update to the database. Often times we want to check that the record has fulfilled all necessary requirements prior to writing.

For example if we are creating a new record for the first time perhaps certain fields in the record must not be blank otherwise they cause a non-null exception when writing to the database. To prevent this we could auto-fill a default/custom value for null values at this time.

Another typical use case may be to check for duplicate data. If a dupe record was found we can simply stop the write operation by returning false. The logic to check for duplication could be written within this method.

Why use after triggers

These events are fired after the insertion/update of a record. In some cases we want to capture this just after to make use of the newly inserted or updated record.

For example the database may have a native sequence that increments a PK field for a newly created record outside of Servoy's control. The afterRecordInsert event would fire after the write is complete. At this time we may wish to use this record for other business logic or to continue a data transaction.

Last updated