When developing extensions in Microsoft Dynamics 365 Business Central, one of the most powerful tools available to AL developers is table triggers.
These triggers allow you to write business logic that runs automatically when data in a table is inserted, modified, deleted, or renamed.
In this blog, we’ll explore what table triggers are, why they matter, and how to use them effectively.
What Are Table Triggers?
Table triggers are predefined methods in AL that are executed when certain events occur on a table.
They help you to:
- Apply business rules and logics
- Validate data
- Perform calculations automatically
- Ensure data consistency across the system
Types of Table Triggers:
There are four main triggers in Business Central tables:
1. OnInsert() - Triggered when a new record is inserted into the table.
Example: I want to ensure that every new record automatically gets today’s date as soon as it is inserted in a table. For this use-case I can use OnInsert() trigger.
2. OnModify() - Triggered when an existing record is modified.
Example: I want to prevent any modification on my records if the record is in blocked state. For this, I can use the OnModify() trigger.
3. OnDelete() - Triggered before a record is deleted from the table.
Example: I want to ensure that important posted transactions are not accidentally deleted. This can be achieved using OnDelete() trigger.
4. OnRename() - Triggered when the primary key of a record is changed.
Example: Used to manage logic when a key value (like Customer No.) is updated. This can be achieved using OnRename() trigger.
Best Practices
- Keep trigger logic lightweight → heavy code inside triggers can slow down performance.
- Use Validation rules in fields (via OnValidate triggers) for field-specific checks, and table triggers for record-level logic.
- Avoid complex business logic directly in triggers → instead, call codeunits to keep code modular.
- Always test with different scenarios (insert via UI, code, or integrations) because triggers fire in all contexts.
Example – Combining Triggers
Let us assume that I want to create a new Table named “Items Custom”.
When I choose any item in the list, automatically the Item's Description gets auto-populated and “Action Performed” is initialized with “Inserted”. This can be achieved by writing code on OnInsert() trigger.
Now, I want to change the “Action Performed” to “Modified” as soon as there is any change in existing record. This can be achieved by OnModify() trigger.
If I want to change the “Item No.” which is my primary key, I can do this using OnRename() trigger.
And lastly, I want to show a message whenever my record gets deleted. This can be achieved by OnDelete() trigger.
Sample Code
Sample Outputs –
1. OnInsert(): New Item Inserted. As soon as Item No. is selected, Item Desc is auto-populated and Action Performed is changed from blank to Inserted.
2. OnModify(): Description Modified. As soon as Item Desc is modified, the Action Performed is changed from Inserted to Modified.
3. OnRename(): Item No. changed. As soon as Item is renamed/ Item No. field is changed (which is a primary key), Action Performed is changed to Renamed.
4. OnDelete(): Record deleted. As soon as any record gets deleted, a message pops-up to let the user know that which item was deleted.
Conclusion:
Table triggers are a foundational part of Business Central AL development. They allow developers to enforce rules, automate data handling, and ensure system integrity.
When used wisely (and in combination with events), table triggers help you build reliable and maintainable extensions that align with real-world business requirements.