Today I would like to share another mini tip about Business Central, how to easily test your upgrade codeunit.
When developing a new extension version, you can use upgrade codeunits to upgrade data from the previous version and make it compatible with the new one. Once you have the upgrade code in place, you can publish and synchronize the new version, and the run the data upgrade. For more information, see Upgrading Extensions.
You write upgrade logic in an upgrade codeunit, which is a codeunit whose SubType property is set to Upgrade.
SubType Property (Codeunit): Sets the codeunit to method as a normal, test, test runner, or upgrade codeunit.
|Upgrade||An upgrade codeunit includes AL methods for synchronizing changes to a table definition in an application with the business data table in SQL Server and migrating existing data.|
So, when does the upgrade codeunit run? Usually when the version number increases (we won’t discuss upgrade triggers this time)
An upgrade is defined as enabling an extension that has a greater version number, as defined in the app.json file, than the current installed extension version.
Then there will be a small problem here. During development, if we want to test the code in the upgrade codeunit, we need to increase the version number each time……
In fact, there is a very simple way. You can find forceUpgrade setting in launch.json.
Launch JSON file: The
launch.json file contains information about the server that the extension launches on.
|forceUpgrade||No||Always run upgrade codeunits, even if the version number of the extension is the same as an already installed version. This can be useful for troubleshooting upgrade issues.|
Let me test it again. (Publish the extension twice without increasing the version number)
Very nice, give it a try!!!😁
1. How to run code for a specified company from Install Codeunit or Upgrade Codeunit
2. Debugging Upgrade and Install code in Visual Studio Code
3. Developers can write faster upgrade code (New DataTransfer data type)
4. Controlling when upgrade code runs: In most cases, it’s important, that upgrade code isn’t run more than once. There are a couple ways that you can control when upgrade code runs. You can either use extension version data or upgrade tags.
5. Upgrade triggers:
|Trigger||Description||Fails the upgrade on error|
|OnCheckPreconditionsPerCompany() and OnCheckPreconditionsPerDatabase()||Used to check that certain requirements are met before the upgrade can be run.||Yes|
|OnUpgradePerCompany() and OnUpgradePerDatabase()||Used to do the actual upgrade.||Yes|
|OnValidateUpgradePerCompany() and OnValidateUpgradePerDatabase()||Used to check that the upgrade was successful.||Yes|
PerCompany triggers are run once for each company in the database, where each trigger is executed within its own system session for the company.
PerDatabase triggers are run once in the entire upgrade process, in a single system session that doesn’t open any company.
Hope this will help.
Thanks for your reading.