How to do it...

  1. In Visual Studio Code, open the televisionShow folder that you extracted from ch9-start.zip and have been working with in the previous recipes of this chapter.
  2. In Explorer, select src/table/Television Show.al.
  3. Add the ObsoleteState and ObsoleteReason properties to the Status field, as follows:
field(4; Status; Option)
{
OptionCaption = 'Active,Finished';
OptionMembers = Active,Finished;
DataClassification = CustomerContent;
ObsoleteState = Pending;
ObsoleteReason = 'Replaced by new field: Running';
}
The ObsoleteState and ObsoleteReason properties give the developer an indication when a field, table, or key is or will become obsolete, so that they can make the necessary adjustments in their solutions. You can find more information about this at https://docs.microsoft.com/en-us/dynamics365/business-central/dev-itpro/developer/properties/devenv-obsoletestate-property.
  1. Add a new field to the Television Show table object:
field(9; Running; Boolean)
{
DataClassification = CustomerContent;
}
  1. Right-click on the src/codeunit folder and create a new file named App Upgrade.al. Then, create a new codeunit object:
codeunit 50112 "App Upgrade"
{

}
  1. Add the Subtype = Upgrade attribute to the codeunit object:
codeunit 50112 "App Upgrade"
{
Subtype = Upgrade;

}

This will cause the codeunit object to be executed when the application is upgraded from a previous version.

  1. Add the following trigger and logic to the codeunit object:
trigger OnUpgradePerCompany()
var
CurrentAppInfo: ModuleInfo;
begin
if NavApp.GetCurrentModuleInfo(CurrentAppInfo) then begin
Case CurrentAppInfo.DataVersion() of
Version.Create('1.0.0.0'):
PopulateTelevisionShowRunningField();
end;
end;
end;

When the application is installed, if version 1.0.0.0 was previously installed, then the upgrade logic will execute in order to populate the new field.

Don't forget that you won't necessarily be able to guarantee which version your customers are upgrading from, so you will need to maintain multiple upgrade paths within your application.
  1. Add the following logic to update the new Running field. We'll set the new field to true, but only if the Status field is set to Active:
local procedure PopulateTelevisionShowRunningField()
var
TelevisionShow: Record "Television Show";
begin
if TelevisionShow.FindSet() then
repeat
if TelevisionShow.Status =
TelevisionShow.Status::Active then begin
TelevisionShow.Running := true;
TelevisionShow.Modify();
end;
until TelevisionShow.Next() = 0;
end;
  1. In Explorer, select app.json.
  2. Increase the version of the application by setting the "version" property, like this:
"version": "1.1.0.0"

Now, when the application is built, it will have the new version number.

Any time that you release a new build of your application, always make sure to increase the version number. With a bit of effort, you can add this logic into your continuous build pipeline so the version gets updated with every new build.

Keep working through this chapter and you'll see the upgrade code in action!

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.244.250