Introduction
In Microsoft Dynamics 365 Business Central, XMLports are specialized AL objects used for importing and exporting structured data.
They are commonly used for:
- Data migration
- Excel/CSV/XML imports
- Data exports
- API integrations
- Third-party system communication
- Bulk record processing
XMLports allow developers to automate data exchange between Business Central and external systems.
To control this process, AL provides XMLport Triggers.
These triggers help manage:
- Initialization
- Validation
- Record processing
- Data transformation
- Cleanup
- Error handling
For beginner AL developers, understanding XMLport triggers is essential for building reliable integrations and data processing tools.
What are XMLport Triggers?
XMLport triggers are automatic code blocks that run during data import/export processing.
Benefits:
- Controlled data exchange
- Input validation
- Business rule enforcement
- Automated transformation
- Error prevention
Main XMLport Triggers
1. OnInitXMLport()
Overview
Runs when the XMLport starts.
Syntax:
trigger OnInitXMLport()
begin
Message('XMLport started');
end;
Common Uses:
- Initialize variables
- Load setup
- Set default values
- Security validation
Example:
trigger OnInitXMLport()
begin
ImportedCount := 0;
end;
2. OnPreXMLport()
Overview
Runs before data processing begins.
Example:
trigger OnPreXMLport()
begin
if FileName = '' then
Error('File is required');
end;
Common Uses:
- File validation
- Parameter checks
- Import/export setup
- Permission checks
3. OnPostXMLport()
Overview
Runs after processing completes.
Example:
trigger OnPostXMLport()
begin
Message('%1 records processed', ImportedCount);
end;
Common Uses:
- Logging
- Notifications
- Cleanup
- Summary reporting
Table Element Triggers
When XMLports process table records, additional triggers are available.
4. OnPreXmlItem()
Overview
Runs before processing each XML/table element.
Example:
trigger OnPreXmlItem()
begin
SetRange(Blocked, false);
end;
Common Uses:
- Filtering records
- Preparing data
- Conditional import/export
5. OnAfterGetRecord()
Overview
Runs after reading each imported/exported record.
Example:
trigger OnAfterGetRecord()
begin
ImportedCount += 1;
end;
Common Uses:
- Data validation
- Field transformations
- Record counting
- Custom processing
- Business logic
6. OnBeforeInsertRecord()
Overview
Runs before inserting imported records into Business Central.
Example:
trigger OnBeforeInsertRecord()
begin
if Name = '' then
Error('Customer name is required');
end;
Common Uses:
- Mandatory field validation
- Duplicate prevention
- Data corrections
- Business rule checks
7. OnAfterInsertRecord()
Overview
Runs after record insertion.
Example:
trigger OnAfterInsertRecord()
begin
LogImport(Rec.”No.”);
end;
Common Uses:
- Logging
- Notifications
- Linked record creation
- Audit trails
Common XMLport Trigger Flow
Typical Import Sequence:
- OnInitXMLport()
- OnPreXMLport()
- OnPreXmlItem()
- OnAfterGetRecord()
- OnBeforeInsertRecord()
- OnAfterInsertRecord()
- OnPostXMLport()
Real-World Example: Customer Import XMLport
xmlport 50100 "Customer Import"
{
trigger OnInitXMLport()
begin
ImportedCount := 0;
end;
trigger OnPreXMLport()
begin
if ImportFile = '' then
Error('Import file required');
end;
tableelement(Customer; Customer)
{
trigger OnBeforeInsertRecord()
begin
if Customer.Name = '' then
Error('Customer name missing');
end;
trigger OnAfterInsertRecord()
begin
ImportedCount += 1;
end;
}
trigger OnPostXMLport()
begin
Message('%1 customers imported successfully', ImportedCount);
end;
}
Best Practices for XMLport Triggers
1. Validate Before Importing
Always check:
- Required fields
- Duplicate data
- Permissions
- File format
2. Use Filters for Performance
Apply:
- SetRange()
- SetFilter()
3. Keep Processing Efficient
Avoid heavy logic for each record when possible.
4. Log Results Properly
Track:
- Successes
- Failures
- Errors
- Record counts
5. Separate Complex Logic
Use codeunits for:
- Advanced transformations
- API integration
- Large processing rules
Common Business Scenarios
| Goal | Trigger |
| Initialize process | OnInitXMLport |
| Validate file/setup | OnPreXMLport |
| Filter records | OnPreXmlItem |
| Process records | OnAfterGetRecord |
| Validate before insert | OnBeforeInsertRecord |
| Audit after insert | OnAfterInsertRecord |
| Cleanup/reporting | OnPostXMLport |
Beginner Mistakes to Avoid
Mistake 1: Missing validations
Fix: Validate before insert.
Mistake 2: Poor duplicate handling
Fix: Check existing records.
Mistake 3: Heavy processing loops
Fix: Optimize record processing.
Mistake 4: No logging
Fix: Track import/export outcomes.
Professional Development Tips
- Optimize imports for speed
- Validate thoroughly
- Modularize transformations
- Use secure integrations
- Test with large datasets
- Follow AL design standards
Conclusion
XMLport triggers are essential for structured data exchange in Business Central.
By mastering:
- OnInitXMLport()
- OnPreXMLport()
- OnPreXmlItem()
- OnAfterGetRecord()
- OnBeforeInsertRecord()
- OnAfterInsertRecord()
- OnPostXMLport()
Developers can build:
- Reliable imports
- Secure exports
- Data migration tools
- Integration systems
- Enterprise-grade automation
Strong XMLport trigger knowledge is critical for successful Business Central data integration.
Final Thought
Whether you are building:
- Customer imports
- Vendor exports
- API connectors
- Data migrations
- Financial integrations
XMLport triggers are a must-know skill for every AL developer.