Download Brochure

Boosting Efficiency: Business Central Triggers

views
image-1

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:

  • OnInsert: Triggers when a new record is inserted into a table.
  • OnModify: Triggers when an existing record is modified.
  • OnDelete: Triggers when a record is deleted from a table.
  • OnValidate: Triggers when a field in a record is being validated.
  • OnCalcField: Triggers when a calculated field is being updated.
  • OnLookup: Triggers when a lookup is being performed in the system.
  • OnOpenPage: Triggers when a page is being opened in the system.
  • OnClosePage: Triggers when a page is closed in the system.

Syntax of a trigger

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.

Why and When to Use Triggers?

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:

  1. Data validation: Triggers can be used to check that all needed fields are filled out and that data is entered in the proper format before storing it in the database.
  2. Data integrity: Triggers can uphold data integrity by preventing the deletion of entries referred to by other records and enforcing business standards.
  3. Automated procedures: When a given condition is satisfied, a report or email may be sent, and a field can be updated dependent on the value of another field. These types of processes can be automated using triggers.
  4. User experience: By automatically filling up fields or displaying messages in response to user inputs, triggers can be utilized to enhance the user experience.

What are the types of Triggers in Business Central?

Business Central offers a variety of triggers, including:

  1. Table triggers: Table-specific triggers within the system, such as OnInsert, OnModify, and OnDelete.
  2. Field triggers: OnValidate and OnCalcField are two examples of triggers connected to areas in a database.
  3. Page triggers: The OnOpenPage and OnClosePage events are connected to system pages.
  4. Query triggers: OnLookup, for example, is a query trigger corresponding to a specific system query.
  5. The system triggers: Triggers connected to system-level activities like OnBeforePost and OnAfterPost.
  6. These triggers can provide the system with specialized functionality and enable quick responses to specific circumstances or events.

    Conclusion

    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.

Comments

Avatar

Leave a Comment

Your email address will not be published. Required fields are marked *

Want to Know More?

Are you interested in learning more about our business and what we offer? Feel free to reach out!