AL icon indicating copy to clipboard operation
AL copied to clipboard

OnInstallPerCompany not working

Open thomasbarbut opened this issue 8 years ago • 8 comments

Hello,

when I deploy my package to Business Central (Cloud) the OnInstallPerCompany is not getting triggered. Any ideas?

codeunit 50100 InstallCodeunit { Subtype = Install;

trigger OnInstallAppPerCompany() var ReportSelection: Record "Report Selections"; begin ReportSelection.RESET; ReportSelection.SetRange(Usage,ReportSelection.USAGE::"S.Invoice"); If ReportSelection.FINDFIRST THEN repeat ReportSelection.DELETE(TRUE); UNTIL ReportSelection.NEXT=0; ReportSelection.SetRange(Usage,ReportSelection.USAGE::"S.Cr.Memo"); If ReportSelection.FINDFIRST THEN repeat ReportSelection.DELETE(TRUE); UNTIL ReportSelection.NEXT=0;

  ReportSelection.Init();
  ReportSelection.VALIDATE(Usage,ReportSelection.Usage::"S.Invoice");
  ReportSelection.VALIDATE(Sequence,'1');
  ReportSelection.VALIDATE("Report ID",50101);
  ReportSelection.INSERT;
  ReportSelection.Init();
  ReportSelection.VALIDATE(Usage,ReportSelection.Usage::"S.Cr.Memo");
  ReportSelection.VALIDATE(Sequence,'1');
  ReportSelection.VALIDATE("Report ID",50102);
  ReportSelection.INSERT;

end; }

best regards, Thomas Barbut

thomasbarbut avatar Sep 16 '18 17:09 thomasbarbut

Are you saying that it doesn't execute 'any more' or that it never did?

The OnInstallAppPerCompany trigger will only execute on a 'new' installation of the extension, or during a reinstallation (I.e. this first time you install, or if you uninstall and reinstall the same version). See: Writing Extension Install Code

The only other case is when you have increased the version number of the extension in the app.json, in which case you are performing an upgrade. In this case, if you want to execute code, you must put it in an upgrade codeunit and utilize the OnUpgradePerCompany or OnUpgradePerDatabase triggers. See: Upgrading Extensions V2

ghost avatar Sep 17 '18 14:09 ghost

Hello,

It is indeed an upgrade. Thanks for the help. This solved my issue!

best regards, Thomas Barbut

thomasbarbut avatar Sep 17 '18 14:09 thomasbarbut

Glad to help.

ghost avatar Sep 17 '18 14:09 ghost

I'm getting the same issue although I haven't changed the version of app. Tried uninstalling and un-publishing the app before publishing again but still it didn't work. Have tried OnInstallPerCompany and onInstallPerDatabase both. what else could be wrong?

`codeunit 70112320 "App Installation" { Subtype = Install;

trigger OnInstallAppPerDatabase();
var
    myAppInfo: ModuleInfo;
begin
    InitStripeData();
    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

        PublishWebServices()
    else
        PublishWebServices();
end;

local procedure PublishWebServices()
var
    WebServiceRec: Record "Web Service Aggregate";
begin
    if not WebServiceRec.Get(WebServiceRec."Object Type"::Codeunit, 'DataExport') then begin
        with WebServiceRec do begin
            Init();
            "Object Type" := "Object Type"::Codeunit;
            "Object ID" := 70112322;
            "Service Name" := 'DataExport';
            Published := true;
        end;
        WebServiceRec.Insert(true);
    end;

    if not WebServiceRec.Get(WebServiceRec."Object Type"::Codeunit, 'ImportData') then begin
        with WebServiceRec do begin
            Init();
            "Object Type" := "Object Type"::Codeunit;
            "Object ID" := 70112323;
            "Service Name" := 'ImportData';
            Published := true;
        end;
        WebServiceRec.Insert(true);
    end;
end;

local procedure InitStripeData()
var
    StripeSetup: Record "StripeSetup";
begin
    StripeSetup.RefreshData();
end;

}`

mohsinJanjua avatar Oct 09 '18 15:10 mohsinJanjua

What about this code doesn't seem to be working/firing? If you have done an uninstall/reinstall then this code should most definitely be firing.

Are you just not seeing entries in the web services table after performing these inserts? Or are you expecting different behavior? Note that since the services are being inserted here in code, they will not be removed when the extension is uninstalled

Web services should actually be included in an xml file inside of the extension, see Exporting data for Extensions

ghost avatar Oct 09 '18 16:10 ghost

The webservices aren't being created in the first place. Same goes for the Stripe data. I have tried to publish/install app with a different name/id after removing the earlier version but still it doesn't work. Regarding the use of Xml files, I will use xml's for webservices but at this point, at least the InitStripeData() function should work because it doesnt have anything to do with the services.

mohsinJanjua avatar Oct 10 '18 10:10 mohsinJanjua

I'm currently getting a similar error message, while executing a get webservice call, during the installation routine. I'm doing this to get my stripe products The error message says that "Publishing app fails due to "callback" functions". When I comment out the installation code everything works fine.

hendrik244 avatar Oct 10 '18 11:10 hendrik244

I'm definitely not able to reproduce this issue as of yet. Could you try a sanity check test and just throw an error inside of the code? Something like the following codeunits would assert the path of execution. You should receive an error indicating where/what was code was fired.

codeunit 70112320 UpgCodeunit
{
    Subtype = Upgrade;

    trigger OnUpgradePerCompany()
    begin
        Error('ERRROR In UpgPerComp');
    end;

    trigger OnUpgradePerDatabase()
    begin
        Error('ERRROR In UpgPerData');
    end;
}

codeunit 70112321 InstallCodeunit
{
    Subtype = Install;

    trigger OnInstallAppPerCompany()
    begin 
        Error('ERRROR In InstPerComp');
    end;

    trigger OnInstallAppPerDatabase()
    begin 
        Error('ERRROR In InstPerData');
    end;
}

ghost avatar Oct 10 '18 15:10 ghost