4D v14.3

Trigger event

Home

 
4D v14.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

 
PROPERTIES 

Product: 4D
Theme: Triggers
Number: 369

 
HISTORY 

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

 
SEE ALSO 

In transaction
Trigger level
TRIGGER PROPERTIES
Triggers

 
ARTICLE USAGE

4D Language Reference ( 4D v14 R3)
4D Language Reference ( 4D v14 R2)
4D Language Reference ( 4D v13.5)
4D Language Reference ( 4D v14.3)
4D Language Reference ( 4D v14 R4)

Inherited from : Database event ( 4D v11 SQL Release 6)