Lab 02 – Implement installation and upgrade code in an extension.
Overview
In this assignment, you will create an installation codeunit and an upgrade codeunit.
The installation codeunit is responsible for populating the SalesPerson/Purchaser table with salespeople. Meanwhile, the upgrade codeunit will take charge of modifying the code field of these newly added salespeople, specifically by substituting underscores with spaces.
High Level Tasks
Complete the following tasks:
-
Create a new app: Lab 02 InstallationUpgradeCode
-
Create an Installation Codeunit
-
Test the installation code
-
Create an Upgrade Codeunit
-
Test the Upgrade code
Detailed Steps
Create a new app: Lab 02 InstallationUpgradeCode
To create a new app: Lab 02 InstallationUpgradeCode, follow these steps:
-
Press the Alt+A, Alt+L shortcut keys to trigger the AL Go! command, and then choose a path to a new empty folder and the version to run.
-
Use Lab 02 InstallationUpgradeCode as the name of the project.
-
In the the launch.json file, make sure the EnvironmentName parameter exactly matches the name of your sandbox.
-
In the Command Palette, select AL: Download Symbols.
-
If there are any errors, then update your launch.json file and download symbols again.
-
Delete the HelloWorld.al file.
-
Create a new folder named: src.
Create an Installation Codeunit
To create an Installation Codeunit, follow these steps:
-
Create a new .al file in the src folder.
-
Name the file Installation.Codeunit.al.
-
Add the following code in the Installation.Codeunit.al file:
al-languageCopy codeunit 50200 Installation { Subtype = Install; trigger OnInstallAppPerDatabase(); var myAppInfo: ModuleInfo; begin NavApp.GetCurrentModuleInfo(myAppInfo); if myAppInfo.DataVersion = Version.Create(0, 0, 0, 0) then HandleFreshInstall() else HandleReinstall(); end; local procedure HandleFreshInstall(); begin CreateSalespeople(); end; local procedure HandleReinstall(); begin end; local procedure CreateSalespeople() var SalespersonPurchaser: Record "Salesperson/Purchaser"; Counter: Integer; SalespersonCode: code[20]; begin for Counter := 1 to 5 do begin clear(SalespersonPurchaser); SalespersonCode := 'SP_' + Format(Counter); if not SalespersonPurchaser.Get(SalespersonCode) then begin SalespersonPurchaser.Code := SalespersonCode; SalespersonPurchaser.Name := 'Salesperson ' + Format(Counter); SalespersonPurchaser.Insert(true); end end; end; }
Test the installation code
To test the installation code, follow these steps:
-
In VScode, use the CTRL+F5 shortcut, or the command Palette to publish your app.
-
Business Central now opens.
-
Open the Salespeople/Purchasers page.
-
The 5 salespeople should be in the list and their code start with SP_.
Create an Upgrade Codeunit
To create an upgrade codeunit, follow these steps:
-
Create a new .al file in the src folder.
-
Name the file Upgrade.Codeunit.al.
-
Add the following code in the Upgrade.Codeunit.al file:
al-languageCopy codeunit 50201 Upgrade { Subtype = Upgrade; trigger OnCheckPreconditionsPerCompany(); var myInfo: ModuleInfo; begin if NavApp.GetCurrentModuleInfo(myInfo) then if myInfo.DataVersion = Version.Create(1, 0, 0, 1) then error('The upgrade is not compatible'); end; trigger OnUpgradePerCompany() begin UpdateSalespeople(); end; local procedure UpdateSalespeople() var SalespersonPurchaser: Record "Salesperson/Purchaser"; Counter: Integer; OldSalespersonCode, NewSalespersonCode : code[20]; begin for Counter := 1 to 5 do begin clear(SalespersonPurchaser); OldSalespersonCode := 'SP_' + Format(Counter); NewSalespersonCode := 'SP ' + Format(Counter); if SalespersonPurchaser.Get(OldSalespersonCode) then SalespersonPurchaser.Rename(NewSalespersonCode); end; end; }
Test the Upgrade code
To test the code in the upgrade codeunit, follow these steps:
-
In VScode, use the CTRL+F5 shortcut, or the command Palette to publish your app.
-
Business Central now opens.
-
Open the Salespeople/Purchasers page.
-
Nothing has changed, because your upgrade code did not execute. In order for upgrade code to run, you need to increment the version number in the app.json file.
-
Open the app.json file.
-
In the app.json file set the version to 1.0.0.1.
-
In VScode, use the CTRL+F5 shortcut, or the command Palette to publish your app.
-
Business Central now opens.
-
Open the Salespeople/Purchasers page.
-
The codes of the recently published salespeople have now changed. The underscore was replaced by a space.
-
Open the app.json file.
-
In the app.json file set the version to 1.0.0.2.
-
In VScode, use the CTRL+F5 shortcut, or the command Palette to publish your app.
-
An error is thrown. This is because of the code in the OnCheckPreconditionsPerCompany() trigger in your upgrade codeunit. When the version of the app you are upgrading is 1.0.0.1, then the error is thrown.