Child pages
  • Table Events

Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Migrated to Confluence 5.3

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 methodsmethod.

The first three events occur just prior to the change being committed to the database. Moreover, the event handler has the opportunity to veto the event, preventing the change from being committed. This is and ideal location to implement fail-safe data rules.

...

An onRecordXXX event is bound to an entity or a global method, which is invoked when the event occurs. The record that is modified is passed in as an argument and the method can veto the change by returning false or throwing an exception.

...

Note

Throwing an exception in the event method will result in a Servoy Exception with a SAVE FAILED error code, which may be handled in the solution's onError event handler.

 

Example

This is an example of an onRecordDelete handler for an invoices table. The data rule is that posted invoices will never be deleted by the application.

...

An afterRecordXXX event is bound to an entity or a global method, which is invoked when the event occurs.

...

The next three events occur just prior to the operation intended to be done on the foundset. The event handler has the opportunity to prevent the operation to take place. This is an ideal place to set fail-safe data rules.

  • onFoundSetRecordCreate - occurs prior to a new record being created in the foundset
  • onFoundSetFind - occurs prior to the foundset going into find mode
  • onFoundSetSearch - occurs prior to executing a search on the foundset

An onFoundSetXXX event is bound to an entity or a global method, which is invoked when the event occurs. The method can veto the change by returning false or throwing an exception.

Parameters

The onFoundSetSearch method event receives two parameters:

  • clearLastResults - Boolean which tells whether or not to clear previous search
  • reduceSearch -  Boolean which tells to reduce (true) or extend (false) previous search results

...

Example

This is an example of an onFoundSetFind handle handler. The data rule is that the foundset doesn't go into find mode if there are no records in the table.

Code Block
/**
 * Foundset pre-find trigger.
 * When false is returned the foundset will not go into find mode.
 *
 * @returns {Boolean}
 *
 * @properties={typeid:24,uuid:"9FE55B79-E5D1-4B7D-82CD-D0E8CF6B6725"}
 */
function onFoundSetFind() {
    if(record_count == 0) // record_count is an aggregation defined for the table, which counts the records
        return false;
    return true;
}

The last three events occur immediately following the operation executed on the foundset.

  • afterFoundSetRecordCreate - occurs subsequent to the creation of a new record
  • afterFoundSetFind - occurs subsequent to entering find mode
  • afterFoundSetSearch - occurs subsequent to performing the search for a foundset

An afterFoundSetXXX event is bound to an entity or a global method, which is invoked when the event occurs.

Parameters

The afterFoundSetRecordCreate event receives parmeter JSRecord which is the record object that was recently crated.

Example

This is an example of an afterFoundSetRecordCreate handler for book_text table. The data rule is that every new record added to the foundset will have a predefined text on the comment_text column.

Code Block
/**
 * Record after-create trigger.
 *
 * @param {JSRecord<db:/example_data/book_text>} record record that is created
 *
 * @properties={typeid:24,uuid:"CCDA9A02-7E4D-4A82-9815-B030BBDB1ED0"}
 */
function afterFoundSetRecordCreate(record) {
    record.comment_text = "Some predefined comment text.\n"
}