Introduction
In Microsoft Dynamics 365 Business Central, reports are used to:
- Print documents
- Export business data
- Generate summaries
- Process large datasets
- Create invoices
- Produce financial statements
Reports do much more than display information — they also process data through Report Triggers.
These triggers control:
- Data filtering
- Validation
- Initialization
- Record processing
- Formatting
- Post-processing
For beginner AL developers, understanding report triggers is essential for building professional reports and processing tools.
What are Report Triggers?
Report triggers are predefined AL code blocks that execute during different stages of report processing.
Benefits:
- Dynamic filtering
- Business rule validation
- Data transformation
- Custom calculations
- Controlled processing
Main Report Triggers
1. OnInitReport()
Overview
Runs when the report starts before any data processing begins.
Syntax:
trigger OnInitReport()
begin
Message('Report started');
end;
Common Uses:
- Variable initialization
- Setup loading
- Security checks
- Default values
Example:
trigger OnInitReport()
begin
CompanyName := CompanyInfo.Name;
end;
2. OnPreReport()
Overview
Runs after initialization but before data items are processed.
Example:
trigger OnPreReport()
begin
if StartDate = 0D then
Error('Start date is required');
end;
Common Uses:
- User input validation
- Filter setup
- Parameter verification
- Data preparation
3. OnPostReport()
Overview
Runs after the report finishes processing.
Example:
trigger OnPostReport()
begin
Message('Report completed successfully');
end;
Common Uses:
- Cleanup tasks
- Logging
- Notifications
- Export confirmations
Data Item Triggers
Each DataItem in a report can also have its own triggers.
4. OnPreDataItem()
Overview
Runs before processing records in a specific data item.
Example:
trigger OnPreDataItem()
begin
SetRange(Status, Status::Open);
end;
Common Uses:
- Applying filters
- Limiting records
- Conditional processing
5. OnAfterGetRecord()
Overview
Runs for each record retrieved.
Example:
trigger OnAfterGetRecord()
begin
TotalAmount += Amount;
end;
Common Uses:
- Calculations
- Derived fields
- Conditional formatting
- Totals
- Business logic
6. OnPostDataItem()
Overview
Runs after all records in the data item are processed.
Example:
trigger OnPostDataItem()
begin
Message('Customer records processed');
end;
Common Uses:
- Summaries
- Totals
- Cleanup
- Final calculations
Common Report Trigger Flow
Typical Sequence:
- OnInitReport()
- OnPreReport()
- OnPreDataItem()
- OnAfterGetRecord()
- OnPostDataItem()
- OnPostReport()
Real-World Example: Customer Balance Report
report 50100 "Customer Balance Report"
{
trigger OnInitReport()
begin
TotalBalance := 0;
end;
trigger OnPreReport()
begin
if ReportDate = 0D then
Error('Report date required');
end;
dataitem(Customer; Customer)
{
trigger OnAfterGetRecord()
begin
TotalBalance += Customer.Balance;
end;
}
trigger OnPostReport()
begin
Message('Total Balance: %1', TotalBalance);
end;
}
Best Practices for Report Triggers
1. Validate Early
Use:
- OnPreReport()
- Error()
- TestField()
2. Keep Processing Efficient
Avoid unnecessary logic inside OnAfterGetRecord().
3. Use Variables for Totals
Store:
- Sums
- Counts
- Calculations
4. Filter Data Properly
Use:
- SetRange()
- SetFilter()
5. Separate Complex Logic
Move heavy processing to codeunits if necessary.
Common Business Scenarios
| Goal | Trigger |
| Initialize variables | OnInitReport |
| Validate filters | OnPreReport |
| Filter records | OnPreDataItem |
| Process records | OnAfterGetRecord |
| Summarize data | OnPostDataItem |
| Cleanup/export | OnPostReport |
Beginner Mistakes to Avoid
Mistake 1: Missing validations
Fix: Validate inputs before processing.
Mistake 2: Heavy calculations inside loops
Fix: Optimize OnAfterGetRecord().
Mistake 3: Poor filtering
Fix: Apply filters early.
Mistake 4: No cleanup
Fix: Use OnPostReport() effectively.
Professional Development Tips
- Optimize report performance
- Use modular design
- Validate user inputs
- Keep reports scalable
- Follow AL standards
- Test with large datasets
Conclusion
Report triggers are essential for professional reporting and data processing in Business Central.
By mastering:
- OnInitReport()
- OnPreReport()
- OnPreDataItem()
- OnAfterGetRecord()
- OnPostDataItem()
- OnPostReport()
Developers can create:
- Professional reports
- Processing reports
- Financial summaries
- Automated exports
- Scalable ERP solutions
Strong report trigger knowledge helps developers build efficient, enterprise-grade reporting systems.
Final Thought
Whether you are building:
- Sales reports
- Purchase reports
- Financial statements
- Audit reports
- Processing reports
Report triggers are a critical skill for every Business Central developer.