Introduction
In Microsoft Dynamics 365 Business Central, pages are the primary user interface objects that allow users to:
- View records
- Edit data
- Enter transactions
- Run actions
- Navigate business processes
Page Triggers are automatic code blocks that execute when page-related events occur.
They help developers:
- Control page behavior
- Validate user actions
- Customize UI experiences
- Load data dynamically
- Enforce business rules
For beginner AL developers, mastering page triggers is essential for building interactive and professional applications.
What are Page Triggers?
Page triggers execute automatically during user interaction with:
- Opening pages
- Closing pages
- Validating fields
- Navigating records
- Clicking actions
Benefits:
- Better user experience
- Dynamic interfaces
- Process control
- Improved validation
- Role-based functionality
Common Page Triggers
1. OnOpenPage()
Overview
Runs when a page is opened.
Syntax:
trigger OnOpenPage()
begin
Message('Page opened');
end;
Example:
trigger OnOpenPage()
begin
if UserId() <> 'ADMIN' then
Editable := false;
end;
Common Uses:
- Setting filters
- Security restrictions
- Role-based UI control
- Initializing variables
- Loading setup data
2. OnClosePage()
Overview
Runs when the page closes.
Example:
trigger OnClosePage()
begin
Message('Page closed successfully');
end;
Common Uses:
- Save confirmations
- Cleanup processes
- Session logging
- Temporary data clearing
3. OnAfterGetRecord()
Overview
Runs after a record is retrieved and displayed.
Example:
trigger OnAfterGetRecord()
begin
Highlight := Rec.Balance > 10000;
end;
Common Uses:
- Dynamic calculations
- Conditional formatting
- Display enhancements
- Custom field updates
4. OnNewRecord()
Overview
Runs when a user creates a new record from the page.
Example:
trigger OnNewRecord(BelowxRec: Boolean)
begin
Rec.Status := Rec.Status::Open;
end;
Common Uses:
- Default values
- Initial statuses
- Auto-population
- User guidance
Field Triggers on Pages
OnValidate()
Overview
Runs when a field value changes and is validated.
Example:
trigger OnValidate()
begin
if Rec.Amount < 0 then
Error('Amount cannot be negative');
end;
Common Uses:
- Data validation
- Business rule enforcement
- Calculations
- Dependency checks
Action Triggers
OnAction()
Overview
Runs when a page action/button is clicked.
Syntax:
action(ProcessOrder)
{
trigger OnAction()
begin
Message('Order processed');
end;
}
Common Uses:
- Posting documents
- Running reports
- Opening pages
- Processing workflows
- Custom automation
Best Practices for Page Triggers
Keep UI Logic in Pages
Use page triggers for:
- Interface behavior
- User restrictions
- Display logic
- Navigation controls
Move Heavy Business Logic to Codeunits
Avoid large processing directly in page triggers.
Use Clear Validations
Always validate:
- User input
- Permissions
- Business conditions
Improve User Experience
Use page triggers to:
- Simplify workflows
- Guide users
- Prevent mistakes
Common Business Scenarios
| Goal | Trigger |
| Restrict editing | OnOpenPage |
| Cleanup on close | OnClosePage |
| Display calculations | OnAfterGetRecord |
| Set defaults | OnNewRecord |
| Validate fields | OnValidate |
| Process actions | OnAction |
Beginner Mistakes to Avoid
Mistake 1: Heavy logic inside pages
Fix: Use codeunits for complex processes.
Mistake 2: Poor validations
Fix: Validate user input properly.
Mistake 3: Overusing messages
Fix: Keep UI clean and professional.
Mistake 4: Ignoring permissions
Fix: Use role-based controls.
Professional Development Tips
- Keep pages responsive
- Use modular code
- Improve user workflows
- Follow Business Central UI standards
- Test all user scenarios
- Separate UI from business logic
Conclusion
Page triggers are essential for creating dynamic, secure, and user-friendly Business Central applications.
By mastering:
- OnOpenPage()
- OnClosePage()
- OnAfterGetRecord()
- OnNewRecord()
- OnValidate()
- OnAction()
Developers can build:
- Interactive pages
- Secure interfaces
- Better user experiences
- Professional ERP customizations
Strong page trigger knowledge is key to successful Business Central UI development.