Exercise - Create the Customer Rewards Install Logic codeunit object
Scenario
In this scenario, you want to set the default codeunit for handling events automatically when you're installing the extension. Create the Customer Rewards Install Logic codeunit with the following code.
Create the Customer Rewards Install Logic codeunit object
To create the Customer Rewards Install Logic codeunit object, follow these steps:
Create a new .al file.
To create a new page in your extension, first create a new file. Select the New File button in the side bar of Visual Studio Code.
Make sure that the filename ends with .al. In this example, you might want to use the name CustomerRewardsInstallation.Codeunit.al.
Add the code to the page. You can copy the following code and paste it in the CustomerRewardsInstallation.Codeunit.al file.
codeunit 50100 "Customer Rewards Installation"
{
// Customer Rewards Install Logic
Subtype = Install;
trigger OnInstallAppPerCompany();
var
myAppInfo: ModuleInfo;
begin
NavApp.GetCurrentModuleInfo(myAppInfo); // Get info about the currently executing module
if myAppInfo.DataVersion = Version.Create(0, 0, 0, 0) then // A 'DataVersion' of 0.0.0.0 indicates a 'fresh/new' install
HandleFreshInstall()
else
HandleReinstall(); // If not a fresh install, then we are Re-installing the same version of the extension
end;
local procedure HandleFreshInstall();
begin
// Do work needed the first time this extension is ever installed for this tenant.
// Some possible usages:
// - Service callback/telemetry indicating that extension was installed
// - Initial data setup for use
SetDefaultCustomerRewardsExtMgtCodeunit();
InsertDefaultRewardLevels();
InitializeRewardsForExistingCustomers();
end;
local procedure HandleReinstall();
begin
// Do work needed when reinstalling the same version of this extension back on this tenant.
// Some possible usages:
// - Service callback/telemetry indicating that extension was reinstalled
// - Data 'patchup' work, for example, detecting if new 'base' records have been changed while you have been working 'offline'.
// - Set up 'welcome back' messaging for next user access.
end;
procedure SetDefaultCustomerRewardsExtMgtCodeunit();
var
CustomerRewardsMgtSetup: Record "Customer Rewards Mgt. Setup";
begin
CustomerRewardsMgtSetup.DeleteAll();
CustomerRewardsMgtSetup.Init();
// Default Customer Rewards Ext. Mgt codeunit to use for handling events
CustomerRewardsMgtSetup."Cust. Rew. Ext. Mgt. Cod. ID" := Codeunit::"Customer Rewards Ext Mgt";
CustomerRewardsMgtSetup.Insert();
end;
procedure InsertDefaultRewardLevels()
var
RewardLevels: Record "Reward Level";
begin
Clear(RewardLevels);
if not RewardLevels.IsEmpty then
exit;
InsertRewardLevel('Bronze', 10);
InsertRewardLevel('Silver', 20);
InsertRewardLevel('Gold', 30);
InsertRewardLevel('Platinum', 40);
end;
local procedure InsertRewardLevel(Level: Text[20]; Points: integer)
var
RewardLevel: Record "Reward Level";
begin
Clear(RewardLevel);
RewardLevel.Level := Level;
RewardLevel."Minimum Reward Points" := Points;
RewardLevel.Insert();
end;
local procedure InitializeRewardsForExistingCustomers()
var
Customer: Record Customer;
SalesHeader: Record "Sales Header";
begin
Clear(SalesHeader);
SalesHeader.SetCurrentKey("Sell-to Customer No.");
SalesHeader.SetRange(Status, SalesHeader.Status::Released);
if SalesHeader.FindSet() then
repeat
if not Customer.Get(SalesHeader."Sell-to Customer No.") then
exit;
Customer.RewardPoints += 1; // Add a point for each new sales order
Customer.Modify();
until SalesHeader.Next() = 0;
end;
}