Identify different table and field triggers

Completed

Table triggers will be run when certain events take place in the database. When you create a table with the ttable snippet, four table triggers are automatically generated:

  • OnInsert

  • OnModify

  • OnDelete

  • OnRename

Showing examples of the four table triggers.

Each of the triggers are run before the action is taken. For example, the OnInsert trigger is run before the data is stored in the database. The OnDelete trigger is run before the data is deleted in the database.

This feature gives you the opportunity as a programmer to add extra validations and logic to check before the actual database transaction is completed.

OnModify is triggered when a record in the database is modified. However, if you modify the primary key of a record, then OnRename is triggered.

The following screenshot shows an example of the OnInsert trigger being used in the Customer table.

Screenshot of an example of an OnInsert trigger on the Customer Table.

Typically, for master tables, you'll find some logic to get a new number for your record through the number series management in the OnInsert trigger.

Alongside table triggers, there are triggers at the field level:

  • OnValidate

  • OnLookup

field(1; "No."; Code[20])
{
    Caption = 'No.';
    DataClassification = CustomerContent;

    trigger OnValidate()
    begin

    end;

}
field(4; "Country/Region Code"; Code[10])
{
    Caption = 'Country/Region Code';
    DataClassification = CustomerContent;
    TableRelation = "Country/Region";

    trigger OnLookup()
    begin

    end;
}

The OnValidate trigger is used to run extra validation when a user enters data into a certain field. The OnValidate trigger will be called before the data is stored in the database. It's better to use the OnValidate trigger instead of the OnModify trigger because you can target one specific field. If you get an error in the OnValidate trigger, the OnModify trigger won't be called.

The OnValidate trigger on the primary key of master tables is typically used to check if the user is allowed to manually enter a value for the primary key. Master tables use number series management to generate a new number for a record. On a number series, you can also define that manual numbering is allowed, as shown in the following example with the OnValidate trigger.

Example of an OnValidate trigger on the No. field.

OnLookup is triggered when you perform a lookup to another table. By default, a lookup is created by setting the TableRelation property on the field. This setting will create a drop-down control on the page. Selecting this control will display data from the linked data, more specifically, the fields that are defined in the DropDown field group. If you want to perform other actions or want to perform extra filtering before showing the data, use the OnLookup trigger.

The next example shows the OnLookup trigger being used in the Customer table. When you select a contact that is linked by the TableRelation property, the code will first filter the contact based on their relationship to this specific customer. This code won't allow you to select a contact person that isn't linked to this customer.

OnLookup trigger on the Primary Contact No. field.