AL icon indicating copy to clipboard operation
AL copied to clipboard

When using OnInstallPerCompany you might also want add code to CompanyInitialize subcription (info)

Open MarcHansenMicrosoft opened this issue 6 years ago • 1 comments

Title When using OnInstallPerCompany you might also want add code to CompanyInitialize subcription (info).

Description When using OnInstallPerCompany you might also want add code to CompanyInitialize subcription (info).

Reason for the rule OnInstallPerCompany do only get called for existing companies at the point of installing the extension. If you have general extension setup code you should place it in CompanyInitialize event.

Bad code sample

codeunit 50000 "MyEXT - Initialize"
{
    Subtype = Install;

    trigger OnInstallAppPerCompany()
    var
        AppInfo: ModuleInfo;
    begin
        NavApp.GetCurrentModuleInfo(AppInfo);
        if AppInfo.DataVersion() = Version.Create('0.0.0.0') then
            CODEUNIT.Run(CODEUNIT::"MyEXT-Upgrade");

        SetupCompany();
    end;

    local procedure SetupCompany()
    begin
        CreateElectronicProfiles();
        CreateDocumentSendingProfile();
        CreateProfileCode();
    end;
    ...

Good code sample

codeunit 50000 "MyEXT - Initialize"
{
    Subtype = Install;

    trigger OnInstallAppPerCompany()
    var
        AppInfo: ModuleInfo;
    begin
        NavApp.GetCurrentModuleInfo(AppInfo);
        if AppInfo.DataVersion() = Version.Create('0.0.0.0') then
            CODEUNIT.Run(CODEUNIT::"MyEXT-UpgradeCompany");

        SetupCompany();
    end;

    local procedure SetupCompany()
    begin
        CreateElectronicProfiles();
        CreateDocumentSendingProfile();
        CreateProfileCode();
    end;

    [EventSubscriber(ObjectType::Codeunit, Codeunit::"Company-Initialize", 'OnCompanyInitialize', '', false, false)]
    local procedure CompanyInitialize()
    begin
        SetupCompany();        
    end;
    ...

Good and bad practices for fixing the rule Make sure to call the extensions company setup code from OnInstallPerCompany and CompanyInitialize.

Remarks

MarcHansenMicrosoft avatar Jan 29 '20 13:01 MarcHansenMicrosoft

Thank you for reporting this issue. Unfortunately, we do not find it important enough to address at this point.

We’re planning to give the CodeCop engine and its rules an overhaul in a future major release.

mazhelez avatar Jun 24 '22 10:06 mazhelez