Extension install code is executed when:
- An extension is installed for the first time.
- An uninstalled version is installed again.
This gives you control to write different logics for first time installation of extension and reinstallations of uninstalled extensions.
This is achieved by defining Install Codeunit in your Extension.
First thing first:
- Subtype property of codeunit need to be set to Install
- OnInstallAppPerCompany trigger is triggered when the Extension is Installed first time or subsequent install of same version on Extension.
- DataVersion property one of the important properties which tells you what version of data that you’re dealing with.
- AppVersion, DataVersion, Dependencies, ID, Name, and Publisher. These properties are encapsulated in a ModuleInfo data type. You can access these properties by using the NavApp.GetCurrentModuleInfo and NavApp.GetModuleInfo methods.
- If the DataVersion property equals Version.Create(0,0,0,0), then it’s the first time that the extension is installed because no data exists in the archive.
Sample codeunit can be similar to below:
codeunit <ID> “Name of Codeunit”
{
// 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 Reinstalling the same version of the extension
end;
local procedure HandleFreshInstall();
begin
// Logic to execute on first time this extension is ever installed for this tenant.
// Some possible usages: – Initial data setup for use
end;
local procedure HandleReinstall();
begin
// Logic to execute on reinstalling the same version of this extension back on this tenant.
// Some possible usages: – Data ‘patchup’ work, for example, detecting if new ‘base’
// records have been changed while you have been working ‘offline’.
end;
}
Happy Learning.