Dynamics 365 Business Central: How to copy an existing company via AL (Create multiple companies in one go)

Dynamics 365 Business Central

Hi, Readers.
Today I would like to talk about how to copy an existing company via AL (Create multiple companies in one go).

As you might know, Microsoft will add a new company limit in 2023 wave 1 (BC22).

Company limit (per environment)

SettingDescriptionLimit
Max companiesThe maximum number of companies that can be contained in one environment.300

This company limit will take effect in 2023 wave 1 release. When in effect, exceeding the limit will prevent you from doing some environment operations. For information about the consequences of exceeding the limit, go to Operational challenges with many companies per environment.
If you already have more than 300 companies in one environment, distribute them across more environments to avoid problems later.

If you need to test this limit, you have to manually create 300 companies, although not many people may do so……

And when I discussed What happens when the capacity limit is exceeded (Exceeding capacity quota) before, I also received similar questions about how to quickly increase the capacity of the environment. The answer is actually very simple, it is to copy the company. Of course, you must first ensure that there is enough data in the company.

Microsoft has prepared the standard Copy feature for us on the Companies page. In most cases, this feature is sufficient.

Copy: Copy an exsiting Company to a new company

After entering the new company name and confirming the important items, click OK.

Company My Company has been copied successfully.

report 357 “Copy Company”:

Well, let’s get to the point. This time we need to use the Database.CopyCompany(Text, Text) Method. It is also used in the standard process.

Database.CopyCompany(Text, Text) Method: Creates a new company and copies all data from an existing company in the same database.

Syntax:

[Ok := ]  Database.CopyCompany(SourceName: Text, DestinationName: Text)

Parameters:

SourceName
 Type: Text
The name of the company that you want to copy data from.

DestinationName
 Type: Text
The name of the company that you want to create and copy data to. The company name can have a maximum of 30 characters. If the database collation is case-sensitive, you can have one company called COMPANY and another called Company. However, if the database is case-insensitive, you cannot create companies with names that differ only by case.

Remarks:

Links and notes on records are not copied to the new company.

Let me do a simple test.
I created a new action on the Companies page first.

After clicking, a Dialog page will be displayed, allowing the user to enter the name of the destination company and number of copies.

Working on it…

The copy is successful, and the company name is determined by the rule of adding numbers to the destination name.

Test Video: (The copying process is a bit long……)

Very simple, isn’t it. Give it a try!!!😁

Source Code: Github (Please note that the source code is for reference only, you can improve it according to your own needs)

pageextension 50123 ZYCompaniesExt extends Companies
{
    actions
    {
        addfirst(processing)
        {
            action(CopyMultipleCompanies)
            {
                Caption = 'Copy to Multiple Companies';
                ApplicationArea = All;
                Promoted = true;
                PromotedIsBig = true;
                PromotedCategory = Process;
                Image = Copy;

                trigger OnAction()
                var
                    CopyMultiCompaniesPage: Page "Copy to Multiple Companies";
                begin
                    CopyMultiCompaniesPage.SetSourceName(Rec.Name);
                    if CopyMultiCompaniesPage.RunModal() = Action::OK then
                        CopyMultiCompaniesPage.CopytoMultipleCompanies();
                end;
            }
        }
    }
}

page 50100 "Copy to Multiple Companies"
{
    PageType = StandardDialog;
    Caption = 'Copy to Multiple Companies';
    layout
    {
        area(content)
        {
            field(SourceName; SourceName)
            {
                ApplicationArea = All;
                Caption = 'Source Name';
                Editable = false;
            }
            field(DestinationName; DestinationName)
            {
                ApplicationArea = All;
                Caption = 'Destination Name';
            }
            field(NumberOfCopies; NumberOfCopies)
            {
                ApplicationArea = All;
                Caption = 'Number of Copies';
            }
        }
    }
    var
        SourceName: Text[30];
        DestinationName: Text[30];
        NumberOfCopies: Integer;
        PictureURL: Text;

    procedure SetSourceName(NewSourceName: Text[30])
    begin
        SourceName := NewSourceName;
    end;

    procedure CopytoMultipleCompanies()
    var
        i: Integer;
        NewCompanyName: Text[30];
        ProgressWindow: Dialog;
        ProgressMsg: Label 'Creating new company %1.';
        CopySuccessMsg: Label 'Company %1 has been copied successfully.';
    begin
        if (NumberOfCopies <> 0) and (DestinationName <> '') then begin
            for i := 1 to NumberOfCopies do begin
                NewCompanyName := DestinationName + ' ' + Format(i);
                ProgressWindow.Open(StrSubstNo(ProgressMsg, NewCompanyName));
                CopyCompany(SourceName, NewCompanyName);
            end;
            ProgressWindow.Close();
            Message(CopySuccessMsg, SourceName);
        end;
    end;
}

END

Hope this will help.

Thanks for your reading.

ZHU

コメント

Copied title and URL