Typically, we want all our extension code to run the same regardless of which Business Central environment we are in. However, there are cases where we want some different behavior depending on specific environmental details.
The first examples that comes to mind is do we want to send remittance notices to clients from a Sandbox Environment? No, we would want to swap that client address with a safe internal address. If someone makes a copy of the Production environment to a Sandbox, we want to change this behavior.
There is where codeunit 457 “Environment Information” is very useful.
- IsProduction: Returns true if the environment type is Production.
- GetEnvironmentName: Retrieves the name of the current environment.
- IsSandbox: Returns true if the environment type is Sandbox.
- IsSaaS: Returns true if the deployment type is SaaS (Software as a Service).
- IsOnPrem: Returns true if the deployment type is OnPremises.
- IsFinancials: Returns true if the application family is Financials.
- IsSaaSInfrastructure: Returns true if the deployment infrastructure is SaaS (returns false in Docker).
- GetApplicationFamily: Retrieves the application family.
- VersionInstalled(AppID: Guid): Returns the major version number when the specified app was installed.
- CanStartSession: Returns true if a new session can be started via Session.StartSession.
- EnableM365Collaboration: Enables M365 Collaboration in the tenant admin center.
- GetLinkedPowerPlatformEnvironmentId: Returns the linked Power Platform environment id.
The IsProduction and its counterpart IsSandbox are useful is gating operations based on the environment type. In this little example we exit an emailing procedure if we happen to be in a sandbox environment. You could also use this to swap out the email with a known good email for testing.
procedure SendEmailsToCustomer(CustomerNo: Code[20])
var
EnvironmentInformation: CodeUnit "Environment Information";
begin
if EnvironmentInformation.IsSandbox() then exit();
//Send email to Customer Here
end;
Other useful functions include the InSaaSInfrastructure check. When testing code in Docker the IsSaaS will return as true, but we aren’t being hosted in a SaaS Infrastructure environment, so it will return false. It also returns false for OnPremises installations.
CanStartSession is useful if you are working on multi-session processing, which we will delve into at a later date.
As you can see, this is a great little code unit which can be useful in helping you manage your code execution.
More information on the Microsoft Learn page: Codeunit “Environment Information” | Microsoft Learn





Leave a comment