4D v16.3

Trigger event

Home

 
4D v16.3
Trigger event

Trigger event 


 

Trigger event -> Function result 
Parameter Type   Description
Function result  Longint in 0 Outside any trigger execution cycle 1 Saving a new record 2 Saving an existing record 3 Deleting a record

Called from within a trigger, the Trigger 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 in the Trigger Events theme:

Constant Type Value
On Deleting Record Event Longint 3
On Saving Existing Record Event Longint 2
On Saving New Record Event Longint 1

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 Trigger event command to structure your triggers as follows:

  // Trigger for [anyTable]
 C_LONGINT($0)
 $0:=0 // Assume the database request will be granted
 Case of
    :(Trigger event=On Saving New Record Event)
  // Perform appropriate action for the saving of a newly created record
    :(Trigger event=On Saving Existing Record Event)
  // Perform appropriate actions for the saving of an already existing record
    :(Trigger event=On Deleting Record Event)
  // Perform appropriate actions for the deletion of a record
 End case



See also 

In transaction
Trigger level
TRIGGER PROPERTIES
Triggers

 
PROPERTIES 

Product: 4D
Theme: Triggers
Number: 369

This command can be run in preemptive processes

 
HISTORY 

Modified: 4D v11 SQL Release 2
Renamed: 4D v13

 
ARTICLE USAGE

4D Language Reference ( 4D v16)
4D Language Reference ( 4D v16.1)
4D Language Reference ( 4D v16.2)
4D Language Reference ( 4D v16.3)