The problem
Sometimes you get into situations in which you need to change certain things within the BC database, or some other third party service, before allowing the user to use the application
Maybe you want to migrate some records from table A to table B, or turn on a new setting on all existing records within a table.
On most platforms, something like that would be called an "upgrade script", or a "seeder", and a system like this is definitely possible using business central's "upgrade tags"
Upgrade tags
In Business central, you can add upgrade tags to your application which keeps track of all of the previous versions. That way you can compare the last upgrade tag to the current version, and do certain upgrade functions based on that.
This is all handled within your install codeunit (a codeunit with SubType=Install), and upgrade codeunit (a codeunit with SubType=Upgrade).
These upgrade tags do have a specific format standardized by Microsoft, however you can technically use any format. Microsoft recommends using [CompanyPrefix]-[ID]-[Description]-[YYYYMMDD]
. For example: ABC-1234-MyExtensionUpgrade-20201206
.
Install codeunits
In the install codeunit, we need to set the upgrade tags that you decided to have until now, for example:
Whats happening here isn't that complicated. All we are doing is checking if an upgrade tag exists, and if not: make it exist.
That way, when we freshly install the extension, we can be sure that all upgrade tags have been initialized, and therefore no upgrade function will be run (since we only want to run them when you upgrade from version A to version B, not when you do a fresh install).
Upgrade codeunit
Now in the upgrade codeunit, we can check what upgrade tags we are missing, in which case we can activate some upgrade function, and add the upgrade function when we are done.
This is done like the following:
Debugging upgrade codeunit
You might have some troubles debugging the upgrade script since its only triggered once you upgrade from one version to another. However this is easily fixed since you can force upgrade business central to upgrade from version X to the same version X. This wont actually do anything except trigger the upgrade codeunit (which we want)
To do this, you need to go to your launch.json and set forceUpgrade to true:
{
"configurations": [
{
"forceUpgrade": true
}
]
}
Conclusion
Upgrade Tags in business central is an excellent tool to handle seeders/upgrade scripts. And I highly recommend this system when tackling this problem.
Make use of these build-in Microsoft AL functionalities as much as you want, and have fun coding!
Top comments (0)