The trigger defines procedures that are implicitly and automatically executed when some operations are performed on the table, such as creations, updates, or deletions.
The trigger can be executed either just before the event (insert, delete, update) or just after.
The DSL (Domain Specific Language) is used to define a table trigger. For more details on DSL, see the Reference .
To create a table trigger, use the Data Model Assistant (DMA) .
To edit and modify a script of the table trigger, use the EBX IDE that provides contextual code completion.
When a table trigger is first edited using the DMA , EBX® provides an initial script with the correct definition of the exported procedure.
An initial script of a table trigger has following structure:
<unit usage statement 1> <unit usage statement 2> ... <unit usage statement N> export procedure trigger_signature_name() begin ... <statement 1> <statement 2> ... end
The following script is a an example of before create trigger:
// this trigger calculates automatically the total and the percentage of students marks. export procedure onBeforeCreate() Begin _ebx.record.total := _ebx.record.subj1 + _ebx.record.subj2 + _ebx.record.subj3; _ebx.record.per :=(_ebx.record.total/60)*100; end
The following script is a an example of before modify trigger:
// this trigger updates automatically the last modification date. uses core.datetime as datetime; export procedure onBeforeModify() begin _ebx.record.lastModificationDate := datetime.now(); end
An exported procedure can be called directly by EBX®. Only one exported procedure is allowed per script in a table trigger.
The signatures of table trigger procedures are the following:
onNewContext() : is called when a new context of record is created.
onBeforeCreate() : is called before the creation of a record.
onAfterCreate() : is called after the creation of a record.
onBeforeModify() : is called before the modification of a record.
onAfterModify() : is called after the modification of a record.
onBeforeDelete() : is called before deleting a record.
onAfterDelete() : is called after deleting a record.
onBeforeTransactionCancel() : is called before a cancel.
onBeforeTransactionCommit() : is called before a commit.
Node: The exported procedure signature (name) depends on the trigger’s type and should not be changed.
Predefined variables allow access to the context of a table trigger. The following contextual variables are specific to the table trigger. For more details on common predefined variables, see the Reference .
The predefined variable _ebx.session provides access to information on the current session. This contextual variable is constant and of an immutable complex type.
It has the following fields:
Name | Type | Description |
---|---|---|
userId | string | The identifier of the current user |
userEmail | string | The email of the current user |
locale | string | The locale of the current session |
Example:
// this triggers logs information of the current session. uses core.log as log; export procedure onNewContext() begin var message := ' Session locale : ' | _ebx.session.locale; message |= '\n User id : ' | _ebx.session.userId; message |= '\n User email: ' | _ebx.session.userEmail; log.info(message); end
The predefined variable _ebx.record allows read/write access to the current record to be created, updated or deleted. Its fields are defined by the current EBX® schema.
Example:
// This trigger adds the full name and the modification date of a new created person. uses core.datetime as datetime; export procedure onBeforeCreate() begin _ebx.record.inscriptionDate := datetime.now(); _ebx.record.fullName := _ebx.record.firstName | ' ' | _ebx.record.lastName; end
The predefined variable _ebx.nextRecord allows read-write access to the next record to be updated. It is available only for onBeforeModify triggers. It is defined by the current EBX® schema.
Example:
uses core.datetime as datetime; export procedure onBeforeModify() Begin if (_ebx.nextRecord.phone <> _ebx.record.phone) then begin _ebx.nextRecord.phoneChangeDate := datetime.now(); _ebx.nextRecord.description := 'old phone number is ' | _ebx.record.phone | ' and New Phone number is ' | _ebx.nextRecord.phone ; end end
The predefined variable _ebx.previousRecord allows read-only access to the previous record after an update. It is available only for OnAfterModify triggers. It is defined by the current EBX® schema.
Example:
// This trigger logs all data modifications of the table sales. uses core.log as log; uses core.datetime as datetime; export procedure OnAfterModify( ) begin var message := ' Sales id : ' | _ebx.record.sales_id; message |= '\n Previous amount : ' | _ebx.previousRecord.amount; message |= '\n Current amount : ' | _ebx.record.amount; message |= '\n Updated on : ' | datetime.now( ); log.info(message); end
The predefined variable _ebx.transaction provides a read-only access to the current transaction of the trigger. It is available only in the context of a table trigger. This variable is constant and of an immutable complex type. The transaction is needed to perform some operations on the dataspace (update, modify, delete records, etc). It ensure that the integrity state of data is never compromised when some actions fail.
Example:
uses core.data as d; // The trigger creates a new record in a table. export procedure onAfterCreate() begin var tableA := d.findTable<.tableName>(_ebx.dataset) var recordA:= d.recordOf(_ebx.transaction, tableA); // Update the record. ... d.saveRecord(recordA); end