Manage extension installation and data upgrades

Completed

The processing service now has runtime boundaries and extension points. Install and upgrade codeunits keep environment initialization and schema evolution outside those runtime objects.

Initialize an installed extension

An install codeunit uses Subtype = Install. Business Central runs its install triggers on the first installation and on a reinstall. It doesn't run install code when the platform upgrades an extension to a newer version.

The two trigger scopes are distinct:

  • OnInstallAppPerCompany runs once for each company in which the extension installs.
  • OnInstallAppPerDatabase runs once for the database, outside a company-specific initialization path.

Multiple install codeunits run independently, and their order isn't guaranteed. Each codeunit must initialize only the data it owns and must not depend on another install codeunit running first.

codeunit 70507 "Entry Install"
{
    Subtype = Install;

    trigger OnInstallAppPerCompany()
    var
        Module: ModuleInfo;
    begin
        NavApp.GetCurrentModuleInfo(Module);
        if Module.DataVersion = Version.Create(0, 0, 0, 0) then
            InitializeCompanyData();
    end;

    local procedure InitializeCompanyData()
    begin
        // Insert extension-owned defaults only when they don't exist.
    end;
}

ModuleInfo.DataVersion distinguishes a first install from a reinstall that retains extension data. Guard every insert because a company can already contain initialized data. Per-database code must also avoid company-bound table assumptions.

Order an extension upgrade

An upgrade codeunit uses Subtype = Upgrade. Business Central runs upgrade triggers in this order:

  1. OnCheckPreconditionsPerDatabase and OnCheckPreconditionsPerCompany verify whether the upgrade can start.
  2. OnUpgradePerDatabase and OnUpgradePerCompany transform data.
  3. OnValidateUpgradePerDatabase and OnValidateUpgradePerCompany verify the result.

Per-database and per-company triggers run in separate sessions. Don't rely on global variables to pass state between them. Preconditions should report unsupported states before mutation, upgrade triggers should make repeatable changes, and validation should detect incomplete outcomes.

DataTransfer supports set-based transfer between tables during upgrade. It is available only in upgrade code and is useful for larger migration operations, but simple guarded record changes remain clearer for small transformations.

Make migrations repeatable

An upgrade tag records that a specific migration completed. Use a unique, stable name that includes the extension, purpose, and version or date. Check the tag before the change and set it only after the change succeeds.

codeunit 70508 "Entry Upgrade"
{
    Subtype = Upgrade;

    trigger OnUpgradePerCompany()
    begin
        UpgradeEntries();
    end;

    local procedure UpgradeEntries()
    var
        UpgradeTag: Codeunit "Upgrade Tag";
        UpgradeTagValue: Code[250];
    begin
        UpgradeTagValue := 'LBT-0001-ProcessingEntryStatus-20260930';
        if UpgradeTag.HasUpgradeTag(UpgradeTagValue) then
            exit;

        // Apply a repeatable transformation to extension-owned records.

        UpgradeTag.SetUpgradeTag(UpgradeTagValue);
    end;
}

Register applicable tags during a fresh install so new companies don't run migrations for data states that never existed. Also register them when a new company initializes after the extension version that introduced the migration. This practice separates historical migration from current initialization.

Test first install, reinstall with retained data, upgrade from every supported data version, a repeated upgrade attempt, and new-company initialization. The runtime service then receives data in the shape it expects, including when it runs as an unattended scheduled task.

Learn more in Write extension installation code, Upgrade extensions, and Upgrade codeunit method types.