Introduction
In Microsoft Dynamics 365 Business Central, triggers are the foundation of event-driven development.
Triggers automatically execute AL code when specific system actions occur, allowing developers to:
- Validate data
- Automate workflows
- Control UI behavior
- Process transactions
- Manage imports/exports
- Build integrations
- Enforce business rules
For beginners, learning triggers is essential because they form the core of nearly every customization.
What Are Triggers?
A trigger is a predefined AL code block that runs automatically when a related event occurs.
Simple Examples:
- Creating a record → OnInsert()
- Updating a record → OnModify()
- Opening a page → OnOpenPage()
- Clicking a button → OnAction()
- Running a report → OnPreReport()
- Importing XML data → OnBeforeInsertRecord()
- Running a query → OnBeforeOpen()
Major Trigger Categories in Business Central
1. Table Triggers
Used for managing database record lifecycle.
Main Triggers:
- OnInsert()
- OnModify()
- OnDelete()
- OnRename()
- OnValidate()
Best For:
- Audit fields
- Validation
- Data security
- Record integrity
2. Codeunit Triggers
Used for reusable business process logic.
Main Triggers:
- OnRun()
- OnCheckPreconditions()
Best For:
- Posting systems
- Automation
- Batch jobs
- Processing logic
3. Page Triggers
Used for user interface interactions.
Main Triggers:
- OnOpenPage()
- OnClosePage()
- OnAfterGetRecord()
- OnNewRecord()
- OnValidate()
- OnAction()
Best For:
- UI control
- User validation
- Navigation
- Interactive workflows
4. Report Triggers
Used for reporting and processing datasets.
Main Triggers:
- OnInitReport()
- OnPreReport()
- OnPostReport()
- OnPreDataItem()
- OnAfterGetRecord()
- OnPostDataItem()
Best For:
- Reporting
- Batch exports
- Financial statements
- Summaries
5. XMLport Triggers
Used for structured data import/export.
Main Triggers:
- OnInitXMLport()
- OnPreXMLport()
- OnPostXMLport()
- OnPreXmlItem()
- OnAfterGetRecord()
- OnBeforeInsertRecord()
- OnAfterInsertRecord()
Best For:
- Data migration
- Integrations
- CSV/XML imports
- API data exchange
6. Query Triggers
Used for data retrieval optimization.
Main Trigger:
- OnBeforeOpen()
Best For:
- Dashboards
- APIs
- BI tools
- Secure data filtering
Beginner Trigger Flow Example
Sales Order Lifecycle:
Step 1: User opens page
OnOpenPage()
Step 2: User creates record
OnNewRecord()
Step 3: User enters fields
OnValidate()
Step 4: Record saved
OnInsert()
Step 5: User clicks Post
OnAction()
Step 6: Posting codeunit runs
OnRun()
Step 7: Report generated
OnPreReport() → OnAfterGetRecord()
This demonstrates how multiple triggers work together.
Advanced Trigger Strategies
1. Use xRec for Change Tracking
if Rec.Amount <> xRec.Amount then
LogChange();
Benefits:
- Audit trails
- Approval workflows
- Change validation
2. Prevent Recursive Loops
Problem:
Updating records inside OnModify() repeatedly.
Solution:
Use flags or conditional checks.
3. Modularize Heavy Logic
Wrong:
Large calculations inside page triggers.
Correct:
Move to codeunits.
4. Validate Early
Use:
- TestField()
- Error()
- Setup checks
5. Secure Sensitive Operations
Examples:
- Posting
- Deleting
- Approvals
- Financial processing
Event-Driven Development vs Traditional Triggers
Modern Business Central also supports:
- Integration events
- Business events
- Event subscribers
Why Important?
They improve:
- Scalability
- Extensibility
- Upgrade safety
Recommendation:
Beginners should first master standard triggers before advanced event subscriptions.
Common Trigger Best Practices
DO:
- Keep triggers lightweight
- Validate before processing
- Separate business logic
- Use codeunits for scalability
- Secure sensitive actions
- Test all scenarios
- Use comments
DON’T:
- Overload triggers
- Use unnecessary UI messages
- Skip validation
- Ignore permissions
- Create recursive loops
- Hardcode sensitive logic
Common Beginner Mistakes
| Mistake | Solution |
| Heavy code in triggers | Use procedures/codeunits |
| Missing validation | Use Error() / TestField() |
| Ignoring xRec | Compare old/new values |
| Poor security | Add permission checks |
| Recursive updates | Add control conditions |
Trigger Selection Guide
| Goal | Recommended Trigger |
| Set defaults | OnInsert |
| Validate fields | OnValidate |
| Track updates | OnModify |
| Restrict deletion | OnDelete |
| Page control | OnOpenPage |
| Process actions | OnAction |
| Batch logic | OnRun |
| Reporting | OnPreReport |
| Import/export | XMLport triggers |
| Secure queries | OnBeforeOpen |
Professional Developer Tips
- Design modular architecture
- Use event subscribers when possible
- Keep triggers focused
- Document logic clearly
- Optimize performance
- Build upgrade-safe extensions
- Follow Microsoft AL standards
Conclusion
Triggers are the backbone of Business Central development.
By mastering beginner and advanced trigger concepts, developers can build:
- Secure ERP systems
- Automated workflows
- Reliable data structures
- Scalable extensions
- Professional enterprise solutions
Core Triggers to Master:
- Table triggers
- Codeunit triggers
- Page triggers
- Action triggers
- Report triggers
- XMLport triggers
- Query triggers
Final Thought
Strong trigger knowledge transforms beginner AL developers into professional Business Central solution architects.
Whether you are building:
- Sales systems
- Financial reports
- Data imports
- Approval workflows
- API integrations
- Dashboard solutions
Triggers are essential to your success in Business Central development.