Clean up your copied environments

by Jun 8, 2023AL Language

Home 9 Development 9 AL Language 9 Clean up your copied environments

One of the most important things every developer should handle is to clean up the environment when the environment (or a company) is copied. Especially if the environment is managed directly by a client and they can create new copies anytime. Similarly, for copied companies.

How to do this? It’s really easy. Microsoft is already changing a few setup & system tables on environment copy (see Copy an environment – Business Central | Microsoft Learn). And we can use the same publishers.

To manipulate data when the environment is copied, use these two publishers from the codeunit 1886 “Environment Cleanup”. To clean company-specific data, use the ChangeCompany procedure together with the company name that is available as a parameter (see the example below).

codeunit 1886 "Environment Cleanup"
{
    [IntegrationEvent(false, false)]
    internal procedure OnClearCompanyConfig(CompanyName: Text; SourceEnv: Enum "Environment Type"; DestinationEnv: Enum "Environment Type")
    begin end;

    [IntegrationEvent(false, false)]
    internal procedure OnClearDatabaseConfig(SourceEnv: Enum "Environment Type"; DestinationEnv: Enum "Environment Type")
    begin end;
}

I personally using the same cleaning code for a company-copy process.

report 357 "Copy Company"
{
    [IntegrationEvent(false, false)]
    local procedure OnAfterCreatedNewCompanyByCopyCompany(NewCompanyName: Text[30]; Company: Record Company)
    begin end;
}

An example:

codeunit 50000 "TKA Copy Cleanup"
{
    [EventSubscriber(ObjectType::Codeunit, Codeunit::"Environment Cleanup", 'OnClearCompanyConfig', '', false, false)]
    local procedure OnClearCompanyConfigEnvironmentCleanup(CompanyName: Text)
    begin
        CleanOnEnvironmentOrCompanyCopy(CompanyName);
    end;

    [EventSubscriber(ObjectType::Report, Report::"Copy Company", 'OnAfterCreatedNewCompanyByCopyCompany', '', false, false)]
    local procedure OnAfterCreatedNewCompanyByCopyCompanyCopyCompany(NewCompanyName: Text[30])
    begin
        CleanOnEnvironmentOrCompanyCopy(NewCompanyName);
    end;

    local procedure CleanOnEnvironmentOrCompanyCopy(CompanyName: Text)
    var
        SalesReceivablesSetup: Record "Sales & Receivables Setup";
    begin
        SalesReceivablesSetup.ChangeCompany(CompanyName);
        if not SalesReceivablesSetup.Get() then
            exit;
        if SalesReceivablesSetup."TKA My Integration URL" = '' then
            exit;
        SalesReceivablesSetup."TKA My Integration URL" := 'https://api.DEVserver.xxxxxx.com.au';
        SalesReceivablesSetup.Modify(false);
    end;
}

Recent Articles from the category

BC Open Source? How to start?

BC Open Source? How to start?

BC Open Source? How to start? One of the most exciting news introduced last month in Lyon during Directions EMEA 2023 was the changes to the open-source initiative. This means that you can now contribute to the source code of the Base app and the System app, which are...

read more
Validate a FlowField Field. Wait? What?

Validate a FlowField Field. Wait? What?

Validate a FlowField Field. Wait? What? There are not many things in the AL Language that surprised me. However, last week, I found one such thing - I reviewed customizations made by another partner and had to analyze the OOTB code of the Demand Forecast matrix. I run...

read more
Dynamics NAV 2013 & Expired Cronus License

Dynamics NAV 2013 & Expired Cronus License

We found an interesting problem - we were not able to run the development environment for Dynamics NAV 2013. Whenever we tried to run the development client, we got the following error message: "Your program license has expired" and the development client has closed...

read more
Indirect Dependencies and Access Modifiers

Indirect Dependencies and Access Modifiers

Last week, there was a discussion on Yammer on how to get values from the "Sent Email" record when all fields are marked as Internal. I was surprised that many people do not know what can/can't access modifiers (such as local, protected, or internal) be used for. I...

read more
AL Extensions: Replace Document Attachment

AL Extensions: Replace Document Attachment

I have published a new simple, open-source extension that allows replacing existing document attachments in all master entities as well as in open documents. The source code as well as the app file that can be installed in your environment is available on my GitHub...

read more

Sign Up for News

Certifications

Highest certification
Microsoft Data Management and
also in D365 Business Central

Microsoft Certified: Dynamics 365 Business Central Functional Consultant Associate

See other certifications here