D365 Business Central : Sandbox Cleanup Event

When a sandbox is created as a copy of a production environment, BC will do the following things automatically:

  • The job queue is stopped
  • Any base application integration settings are cleared
  • Outbound HTTP calls from extensions are blocked by default and must be approved for each extension.

This is done to prevent any unwanted email / result coming from your sandbox.

What happen if you want to do the same thing with your app ?

Microsoft offers Sandbox Cleanup codeunit that you can subscribe to. It has the following events:

  • OnClearConfiguration: subscribe to this event to clean up data when copying a company to a sandbox environment.
  • OnClearCompanyConfiguration: subscribe to this event to clean up company-specific data when copying to a sandbox environment.
  • OnClearDatabaseConfiguration; subscribe to this event to clean up environment-specific data when copying to a sandbox environment.

Here is an example on how to change the company indicator text when copying to sandbox.

[EventSubscriber(ObjectType::Codeunit, Codeunit::"Sandbox Cleanup", 'OnClearCompanyConfiguration', '', false, false)]
    local procedure OnClearCompanyConfiguration(CompanyName: Text)
    var
        CompanyInfo: Record "Company Information";
    begin
        CompanyInfo.ChangeCompany(CompanyName);
        If CompanyInfo.Get() then begin
            CompanyInfo.Validate("Custom System Indicator Text", 'TEST');
            CompanyInfo.Modify();
        end;
    end;

Another way is to use the Environment Triggers codeunit which has following two events:

  • OnAfterCopyEnvironmentToSandbox
  • OnAfterCopyEnvironmentToSandboxPerCompany
[EventSubscriber(ObjectType::Codeunit, Codeunit::"Environment Triggers", 'OnAfterCopyEnvironmentToSandboxPerCompany', '', false, false)]
    local procedure OnAfterCopyEnvironmentToSandboxPerCompany()
    var
        CompanyInfo: Record "Company Information";
    begin
        If CompanyInfo.Get() then begin
            CompanyInfo.Validate("Custom System Indicator Text", 'TEST');
            CompanyInfo.Modify();
        end;
    end;

Be careful when using the events, because if there is an error, the sandbox creation will be canceled and you will not get any error message.

thatnavguy

Experienced NZ-based NAV Developer and Consultant with 15+ years of experience leading multiple IT projects, performing business analyst, developing, implementing, and upgrading Dynamics NAV and Business Central. Passionate to deliver solution that focuses on user-friendly interface while keeping high standard of compliance with the needs.

You may also like...

1 Response

  1. 6 November 2021

    […] Sandbox Cleanup Event […]

Leave a Reply

Your email address will not be published. Required fields are marked *