Environment Info

Spring ‘22 release brought us a new set of classes to access the current Salesforce Environment information with very low effort. Let’s focus on the Domain and Domain Parser classes.

Using these two classes, together with methods provided by the System.URL class you can solve a long time problem:

“How do I get the name of the sandbox I am currently in?”

public without sharing class Environment {
	public static final System.URL ORG_DOMAIN_URL = System.URL.getOrgDomainUrl();
	public static final String SANDBOX_NAME= DomainParser.parse(ORG_DOMAIN_URL).getSandboxName();
	public static Boolean isSandbox() {
		return SANDBOX_NAME != null;
	}
}

From this very simple implementation we now have a way to access, with very low effort, if we are currently running our code in a Sandbox or Production and change, for example, integration endpoints accordingly

Leave a comment