Published on: 05 June 2023
Author:
Triggers in the AL Code of Business Central are events or actions that initiate the execution of a specific code in response to a particular change or condition in the system. As a result, they can automate specific processes and make the system more efficient.
Examples of triggers in Business Central include:
The syntax of a trigger in Business Central is as follows:
trigger TriggerName on TableName { //Trigger code goes here // Multiple statements can be included here }TriggerName is the name of the trigger and should be descriptive and unique.
TableName is the name of the table that the trigger is associated with. The trigger will be executed whenever a record in the table is inserted, modified, or deleted.
The code inside the curly braces {} is the logic of the trigger. This code will be executed whenever the trigger is triggered.
The trigger OnModifySalesOrderHeader is associated with the SalesOrderHeader table and will be executed whenever a record in the table is modified. The code checks if the sales order status has been changed to Shipped; if it has, it updates the ShipDate field with the current system date.
Let us look at another scenario in which you want to develop an automated procedure to determine an employee bonus depending on their performance. The following describes how to utilize triggers to complete this task:
First, add a "Performance" field to the Employee table to save an employees performance rating.
Create a new field in the Employee table called "Bonus" to record the calculated bonus amount.
First, to calculate the bonus when an employees performance rating is modified, establish an “OnModify” trigger on the Employee database.
In this case, the trigger uses the performance rating saved in the "Performance" field to determine the bonus. The incentive is 5000 if the performance rating is 9 or higher. The incentive amount is set at 3000.00 if the performance rating is between 7 and 8. Moreover, the bonus is zero if the performance rating is below seven. The determined bonus is then stored in the "Bonus" field.
Every time a workers performance rating changes, this trigger will activate automatically, keeping the bonus amount current.
Using triggers is essential for developing custom logic that can be carried out automatically without human interaction. In the Business Central platform, triggers can be used in a variety of ways, some of which are listed below:
Business Central offers a variety of triggers, including:
These triggers can provide the system with specialized functionality and enable quick responses to specific circumstances or events.
Dynamics 365 Business Central triggers in AL Code is a potent tool for automating system functions. For example, when particular system events occur, you can use triggers to perform specified operations like inserting, editing, deleting, verifying, or looking up entries.