Use typical data triggers

Completed

In addition to maintaining data validity through data types and table relationships, Business Central also contains data-related business logic. This logic makes sure that more complex data-related business rules are consistently applied as users insert, change, or delete information in the database. These complex business rules are coded in table triggers as AL code. You can find the same patterns of code in the same types of tables, regardless of the application area that they belong to.

It's important that you understand these principles and make sure that the code that you write doesn't violate these principles.

OnInsert trigger

The OnInsert table trigger runs when a user inserts a new record into a table. The code in the trigger runs before the record is inserted into the table. If the code in the trigger causes a run-time error to occur, then the insert operation is canceled.

The OnInsert trigger has the following purposes in different table types:

  • Master table - Assigns the No. field from the appropriate number series if the user hasn't provided the value manually. It also assigns the default dimensions for the account type, based on the configuration in the Default Dimension table.

  • Subsidiary table - Checks whether all necessary primary key fields are entered for tables that have complex primary keys with three or more fields, or checks if any mutually exclusive or unacceptable primary key combinations are selected.

  • Journal table - Validates the values in the Shortcut Dimension 1 and Shortcut Dimension 2 fields.

  • Document (header) table - Assigns the No. field from the appropriate number series if the user hasn't provided the value manually. It applies certain defaults, such as dates. It checks the filter on the field that is related to the primary master record of the application area. It also assigns the value of the filter to the field if the field is filtered to a single value.

  • Document (line) table - Makes sure that the status can accept new lines if the document supports different statuses.

For these table types, the OnInsert trigger might contain more code. For other table types, the OnInsert trigger might contain code that is specific to the applicable scenario and no general principles are applied.

OnModify trigger

The OnModify table trigger runs when a user changes an existing record in a table. The code in the trigger runs before the record is updated in the table. The application cancels the modifications if an error occurs in the trigger code.

The OnModify trigger has the following purposes in different table types:

  • Master table - Sets the Last Date Modified field to the current system date. (For master tables, the OnRename trigger must complete the same action.)

  • Journal table - Makes sure that a modification doesn't violate business rules for the specific journal type.

  • Document (header) table - Assigns the No. field from the appropriate number series, if the user has not provided the value manually. It applies certain defaults, such as dates. It checks the filter on the field that is related to the primary master record of the application area. It also assigns the value of the filter to the field if the field is filtered to a single value.

  • Document (line) table - Makes sure that the modification doesn't violate business rules for the specific document.

For other table types, the OnModify trigger might contain code that is specific to the applicable scenario and then no general principles are applied.

OnDelete trigger

The OnDelete table trigger runs when a user deletes a record from a table. The code in the trigger runs before the record is physically deleted from the table. The record isn't deleted if an error occurs in the trigger code.

The OnDelete trigger has the following purposes in different table types:

  • Master table - Makes sure that started, but incompleted, transactions (such as orders, jobs, and so on) that are related to the master record don't exist. This type of transaction could leave the system in an inconsistent state or cause issues for transaction processing. The trigger deletes all subsidiary information for the master record, including default dimensions and any open documents and transactions.

  • Supplemental table - Deletes all subsidiary information for the supplemental record.

  • Journal table - Makes sure that the deletion doesn't violate business rules for the specific type of journal.

  • Document (header) table - Makes sure that the deletion doesn't violate any business rules for the specific document and deletes all document lines and subsidiary document information.

  • Document (line) table - Makes sure that the deletion doesn't violate any business rules for the specific document and deletes all subsidiary document line information.

  • Document History (header) table - Makes sure that the conditions under which a posted document can be deleted are met, and then deletes the posted document lines and any subsidiary posted document information.

  • Document History (line) table - Deletes any subsidiary posted document line information. It doesn't check whether the conditions for the deletion are met because users can never delete a document history line directly.

Depending on the scenario, the OnDelete trigger might contain more code and achieve more goals than specified previously.

OnValidate trigger

The OnValidate field trigger runs after the user enters a value in a field. The code in this trigger runs after the application runs a default validation behavior, such as data type validation.

This trigger is frequently defined on the fields that relate to other tables, such as master tables, subsidiary tables, and supplemental tables. When it's defined on such fields, the trigger frequently performs the following important operations:

  • Assigns the default dimensions, if applicable

  • Assigns certain default values to other fields

  • Validates any case-specific complex business rules

For example, for the Resource No. field in the Res. Journal Line table, the OnValidate trigger performs the following tasks:

  • Assigns the default dimensions from the selected resource to the resource journal line

  • Makes sure that the selected resource isn't blocked

  • Assigns several fields from the selected resource to the resource journal line, such as Description, Direct Unit Cost, Resource Group No., and Gen. Prod. Posting Group

  • Makes sure that the time sheet is specified for the resource that requires time sheets, if the resource line isn't created automatically by the system