- When working with Microsoft Dynamics 365 Business Central, upgrading your solution across versions is a critical task—especially when you’ve customized the application. One of the key components to a successful upgrade is the Upgrade Codeunit.
- In this blog post, we’ll dive into what an Upgrade Codeunit is, when to use it, and how to implement one efficiently.
What is an Upgrade Codeunit?
- An Upgrade Codeunit is a special type of AL codeunit that allows you to manage data transformation and schema changes during the upgrade of an extension or base application in Business Central.
- At the time of codeunit creation we have to set subtype as Upgrade.
- These codeunits are automatically executed when you run your program.
It helps you :
- Move or Transfer Data from one Schema to another.
- Initialize data in newly added tables/fields.
- Clean up obsolete data.
This especially useful when :
- You’re making structural changes to your data.
- You’re removed or renamed fields.
- You need to migrate data from old field to new field.
There are Two main Upgrade Triggers :
- onCheckPreconditionsPerCompany() / onCheckPreconditionsPerDatabase()
- Validaes if preconditions are met before starting the upgrade.
- OnUpgradePerCompany() / OnUpgradePerDatabase()
- Contains the actual Logix to modify or migrate data.
Here’s a basic structure for an upgrade codeunit :

- Here in this code, we have created one codeunit “Upgrade Weight and Volume” of subtype upgrade.
- We have used “OnUpgradePerCompany()” trigger for this. It runs once per company when an extension is upgraded and is used to handle data transformation, migration and initialization Logic.
- This trigger is useful when migrates the data from old table to new table.
- In this code we are transferring data from old field(“RRCG CI Weight”) to new field(“Net Weight”) and lastly we have modify the current table.
- Lastly we have to increase one version in app.json file in Business central AL code. And we have to publish as PTE.
Conclusion:
- Upgrade Codeunits are essential for maintaining data integrity and ensuring smooth transitions when evolving your Business Central Solutions. Whether you’re adding new features, refactoring your data model, or deprecating legacy tables, a well-crafted Upgrade Codeunit will save you hours of debugging and data recovery.
- Choosing the right trigger ensures your data upgrades are efficient, accurate, and scalable across multiple companies.



