How to identify the type of Sandbox in Apex?

I don't think there's a straightforward solution.

As you've said, we have the Organization SObject which we can query to find certain environment information, such as:

SELECT Id, InstanceName, IsSandbox, Name, OrganizationType FROM Organization

This will tell us if we're on a sandbox environment, but it won't tell us which of sandbox it is.

The best solution I can offer is to include sandbox type in the sandbox type when creating them.

So if you have the following environments:

  • Prod: CompanyName
  • Sandbox: CompanyName.UAT
  • Sandbox: CompanyName.QA
  • Sandbox: CompanyName.DEV
  • Sandbox: CompanyName.DEVPRO

You will be able to query Organization.Name to find the current environment type.

It's pretty messy and requires the use of a naming convention in order to work, but I think it's the easiest way for now. Probably worth a post on the Ideas site, as it would be a pretty useful feature.

Tags:

Sandbox

Apex