Click here to Skip to main content
13,599,028 members
Click here to Skip to main content
Add your own
alternative version


17 bookmarked
Posted 10 Mar 2007
Licenced CPOL

Implementing Triggers in 4D

, 10 Mar 2007
Rate this:
Please Sign up or sign in to vote.
The target audience of the article are novice 4D developers. It aims at explaining the usage of Triggers in 4D databases.


4D(4th dimension) is a cross-platform relational database management system that facilitates building excellent database and web applications. This development tool is owned by 4D, Inc and has been there since 1984. 4th dimension supports a wide range of standards, including ODBC, a native HTTP server, FastCGI, XML, and SOAP.

In this article, I will be discussing the concept of triggers in 4D and how to implement them in 4D applications.

By now, you must be wondering if the trigger in 4D is something different or equivalent to SQL triggers. Let me tell you that the definition is quite similar to what we find in the SQL world, however they differ a lot in the way they are implemented.

Triggers in 4D are methods associated with table that are used to control and validate actions that affect records or the table as a whole.

Features specific to 4D triggers are that they execute on the machine where the database engine runs. They return an error code, or 0 if there is no problem. Only one trigger can run at a time.

The triggers tab of the table properties dialog in 4D lists four different events:

  1. On saving new record
  2. On saving an existing record
  3. On deleting a record
  4. On loading a record

These are called database events or record modification events.

Format to write a trigger:

This is a sample of what a trigger looks like:

` [Employee] Trigger 

Case of: 
(Database event=Save Existing Record Event) 
$ErrorCode:= EmployeeSaveExisting 
: (Database event=Save New Record Event) 
$ErrorCode:= EmployeeSaveNew 
: (Database event=Delete Record Event) 
$ErrorCode:= EmployeeDelete 
: (Database event=Load Record Event) 
Else ` A bug. 
End case 

Let me take the opportunity to explain to you about cascading triggers in 4D. Look at the explanation below:

When a trigger performs actions that invoke another table's trigger, it is called a trigger cascade. Trigger cascades are necessary to avoid orphaned records in the system.

For example, consider the following case:

Suppose there are three tables say [table1], [table2] and [table3].

When you delete a [table1] record, its trigger deletes all related [table2] records. The [table2] trigger in turn deletes all related [table3] records. This is how cascading triggers are meant to behave.

Now suppose there is a fourth table, say [table4] which is dependant on a field from the [table3]. The [table3] record is deleted so when we do an update, then the [table4] is going to behave hazardously.

The problem can be easily solved in 4D. In 4D language, we have command (called Trigger level) that provides us the ability to handle the situation. The command lets us know where we stand. The first trigger executed is level 1, the second is level 2, and so on.

If, for example, you wanted to say that [table3] records can only be deleted by the [table2] trigger, then the [table3] trigger would include code like this:

Case of:(Database event=Delete Record Event) 
` Deletion is allowed only from the [table2] trigger. 
I f(Trigger level<2) 
` Return an error code: 
` Get the properties of the invoking trigger (Trigger level - 1). 
` delete record trigger 
I f($invokingEvent#Delete Record Event) | ($TableNum#$invokingTableNum) 
` return an error. 
` Carry on the deletion. 
End if 
` ($invokingEvent#Delete Record Event)| ($TableNum #$invokingTableNum) 
End if 
` (Trigger level<2) 
End case 


This article provides a better insight about using triggers in 4D. The novice users can easily understand and use triggers for different database events.

Manjushree Das


  • 10th March, 2007: Initial post


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Manjushree Das
Web Developer
India India
No Biography provided

You may also be interested in...

Comments and Discussions

-- There are no messages in this forum --
Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web01-2016 | 2.8.180621.3 | Last Updated 10 Mar 2007
Article Copyright 2007 by Manjushree Das
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid