version 11.2 (Modified)
Database event Longint
Parameter | Type | Description | ||||
This command does not require any parameters | ||||||
Function result | Longint | 0 Outside any trigger execution cycle | ||||
1 Saving a new record | ||||||
2 Saving an existing record | ||||||
3 Deleting a record |
Description
Called from within a trigger, the Database event command returns a numeric value that denotes the type of the database event, in other words, the reason why the trigger has been invoked.
The following predefined constants are provided:
Constant | Type | Value |
On Saving New Record Event | Long Integer | 1 |
On Saving Existing Record Event | Long Integer | 2 |
On Deleting Record Event | Long Integer | 3 |
Within a trigger, if you perform database operations on multiple records, you may encounter conditions (usually locked records) that will make the trigger unable to perform correctly. An example of this situation is updating multiple records in a [Products] table when a record is being added to an [Invoices] table. At this point, you must stop attempting database operations, and return a database error so the invoking process will know that its database request cannot be performed. Then the invoking process must be able to cancel, during the transaction, the incomplete database operations performed by the trigger. When this type of situation occurs, you need to know from within the trigger if you are in transaction even before attempting anything. To do so, use the command In transaction.
When cascading trigger calls, 4D has no limit other than the available memory. To optimize trigger execution, you may want to write the code of your triggers depending not only on the database event, but also on the level of the call when triggers are cascaded. For example, during a deletion database event for the [Invoices] table, you may want to skip the update of the [Customers] Gross Sales field if the deletion of the [Invoices] record is part of the deletion of all the invoices related to a [Customers] record being deleted. To do so, use the commands Trigger level and TRIGGER PROPERTIES.
Example
You use the command Database event to structure your triggers as follows:
` Trigger for [anyTable] C_LONGINT($0) $0:=0 ` Assume the database request will be granted Case of : (Database event=On Saving New Record Event) ` Perform appropriates action for the saving of a newly created record : (Database event=On Saving Existing Record Event) ` Perform appropriates actions for the saving of an already existing record : (Database event=On Deleting Record Event) ` Perform appropriates actions for the deletion of a record End case
See Also
In transaction, Trigger level, TRIGGER PROPERTIES, Triggers.