Dynamics 365 Business Central: How to run code for a specified company from Install Codeunit or Upgrade Codeunit

Dynamics 365 Business Central

Hi, Readers.
A few days ago, I saw a question about Install Codeunit in the Business Central community. He hopes that the code in Install Codeunit will only run in a specific company and not in other companies.
So today I would like to talk about how to run code for a specified company from Install Codeunit or Upgrade Codeunit.

First let’s look at the SubType Property (Codeunit) of Codeunit: (Syntax: SubType = Normal)

ValueDescription
NormalA normal codeunit. This is the default setting.
TestA test codeunit includes AL methods that test the application.
TestRunnerA test runner codeunit manages the execution of one or more test codeunits.
UpgradeAn upgrade codeunit includes AL methods for synchronizing changes to a table definition in an application with the business data table in SQL Server and migrating existing data.
InstallAn install codeunit includes AL methods for performing operations unconcerned with the extension code itself during the initial installation and the reinstallation of an extension.

You write upgrade logic in an upgrade codeunit, which is a codeunit whose SubType property is set to Upgrade. An upgrade codeunit supports several system triggers on which you can add data upgrade code. These triggers are invoked when you run the data upgrade process on the new extension.

TriggerDescriptionFails the upgrade on error
OnCheckPreconditionsPerCompany() and OnCheckPreconditionsPerDatabase()Used to check that certain requirements are met before the upgrade can be run.Yes
OnUpgradePerCompany() and OnUpgradePerDatabase()Used to do the actual upgrade.Yes
OnValidateUpgradePerCompany() and OnValidateUpgradePerDatabase()Used to check that the upgrade was successful.Yes

You write install logic in an install codeunit. This is a codeunit that has the SubType property set to Install. An install codeunit supports two system triggers on which you can add the install code.

TriggerDescription
OnInstallAppPerCompany()Includes code for company-related operations. Runs once for each company in the database.
OnInstallAppPerDatabase()Includes code for database-related operations. Runs once in the entire install process.

Note:
PerCompany triggers are run once for each company in the database, where each trigger is executed within its own system session for the company.

PerDatabase triggers are run once in the entire upgrade process, in a single system session that doesn’t open any company.

For example: The following code will run once for each company in the database.

If we only want to run the upgrade or install code for a specified company, what should we do?

Actually, it’s not very difficult.

This time we use Database.CompanyName Method: Gets the current company name. And the usage is also very simple.

For example: Just add a if statement before processing

Test Video:

As I understood, the system repeats through the company list when PerCompany trigger executing. So we can conditionally check the company name before executing the code.

But the above code still needs to be improved, the company name is hard coded now, is it possible to get the company name that needs to be used in the default features before installing?

Last month, we discuss SessionSettings Data Type.
For more details: Dynamics 365 Business Central: SessionSettings Data Type (A complex data type)

So we can use SessionSettings.Company Method to get the company name for a specific user from the table User Personalization (2000000073). For example, ‘Admin’.

Okay, let’s improve our code.

codeunit 50120 MyCodeunit
{
    Subtype = Install;

    trigger OnInstallAppPerCompany()
    var
        Cust: Record Customer;
        UserPersonalization: Record "User Personalization";
    begin
        UserPersonalization.Reset();
        UserPersonalization.SetRange("User ID", 'ADMIN');
        if UserPersonalization.FindFirst() then begin
            if CompanyName() = UserPersonalization.Company then
                if Cust.Get(10000) then begin
                    Cust.Name := 'Modified By Install Codeunit';
                    Cust.Modify();
                end;
        end;
    end;
}

END

Hope this will help.

Thanks for your reading.

ZHU

コメント

タイトルとURLをコピーしました