How it works...

If you need some logic to be executed when your application is upgraded, then you need to create an upgrade codeunit. An upgrade codeunit is executed whenever an application is installed on a system that has had a previous version of the application installed.

You create an upgrade codeunit by following these steps:

  1. Create a new empty codeunit object.
  2. Assign the SubType = Upgrade attribute to the codeunit.

In the upgrade codeunit, you have access to a number of different triggers where you can place your code. The following triggers are available:

When writing your upgrade logic, you can get information regarding the old version that was installed by using the ModuleInfo data type. With this, you can use the data version to determine the starting point for the upgrade, since, at the time the upgrade logic is executed, the version of the data represents the original version that's being upgraded.

More information can be found about the ModuleInfo data type at https://docs.microsoft.com/en-us/dynamics365/business-central/dev-itpro/developer/methods-auto/moduleinfo/moduleinfo-data-type.