Introduction
In Microsoft Dynamics 365 Business Central, tables are the foundation of business data storage.
Table Triggers are automatic event-driven code blocks that execute whenever records are:
- Inserted
- Modified
- Deleted
- Renamed
These triggers are critical for:
- Data validation
- Audit tracking
- Security enforcement
- Business rule implementation
- Process automation
- For AL developers, mastering table triggers is essential for building reliable ERP systems.
What are Table Triggers?
Table triggers execute automatically when database operations occur.
They ensure:
- Data consistency
- Business logic enforcement
- Controlled record lifecycle
- Secure operations
- Better maintainability
Core Table Triggers
1. OnInsert()
Overview
OnInsert() runs whenever a new record is created.
Syntax:
trigger OnInsert()
begin
"Created By" := UserId();
end;
Example:
trigger OnInsert()
begin
"Created By" := UserId();
"Created Date" := Today();
Status := Status::Open;
end;
Common Uses:
- Number series generation
- Default field values
- Audit fields
- Initial status setup
- Validation controls
2. OnModify()
Overview
OnModify() runs whenever an existing record is updated.
Example:
trigger OnModify()
begin
"Modified By" := UserId();
"Modified Date" := Today();
end;
Using xRec
xRec stores the previous version of the record before modification.
Example:
if Rec.Amount <> xRec.Amount then
Message('Amount changed');
Common Uses:
- Change tracking
- Audit logs
- Field comparisons
- Approval controls
- Status monitoring
3. OnDelete()
Overview
OnDelete() runs before a record is deleted.
Example:
trigger OnDelete()
begin
if Posted then
Error('Posted records cannot be deleted');
end;
Common Uses:
- Prevent deleting posted transactions
- Dependency validation
- Security restrictions
- Child record cleanup
4. OnRename()
Overview
OnRename() runs when a primary key value changes.
Example:
trigger OnRename()
begin
Message('Primary key updated');
end;
Common Uses:
- Synchronizing related records
- Updating references
- Maintaining integrity
- Logging key changes
- Best Practices for Table Triggers
- Keep Business Rules in Tables
- Use table triggers for:
- Validation
- Required fields
- Status restrictions
- Audit controls
Use xRec Carefully
Always compare old vs new values before processing updates.
Prevent Recursive Updates
Avoid repeatedly modifying records inside OnModify().
Avoid Heavy UI Logic
Table triggers should remain backend-focused because they may run in:
- APIs
- Background sessions
- Integrations
Common Business Scenarios
| Goal | Trigger |
| Set default values | OnInsert |
| Audit changes | OnModify |
| Prevent deletion | OnDelete |
| Handle key updates | OnRename |
Beginner Mistakes to Avoid
Mistake 1: Ignoring xRec
Fix: Always compare previous and current values.
Mistake 2: Missing Validation
Fix: Use Error() and TestField().
Mistake 3: Heavy Processing in Triggers
Fix: Move large logic to codeunits.
Mistake 4: Recursive Loops
Fix: Avoid unnecessary modifications inside triggers.
Professional Development Tips
- Keep triggers lightweight
- Focus on data integrity
- Use audit fields
- Modularize logic
- Follow Microsoft best practices
- Test all trigger scenarios
Conclusion
Table triggers are essential for secure and reliable data management in Business Central.
By mastering:
- OnInsert()
- OnModify()
- OnDelete()
- OnRename()
- Developers can create:
- Strong data models
- Secure ERP systems
- Reliable customizations
- Professional AL solutions
Strong table trigger knowledge ensures better Business Central development and data integrity.