+ Start a Discussion
Scott.MScott.M 

Get sandbox name from apex

Does anyone know if there's a way to get the current sandbox name in apex short of screen scraping it from the upper right of a standard page? Thanks!

 

Scott

forecast_is_cloudyforecast_is_cloudy

Unfortunately, Sandbox related metadata (like Name) is not currently available in Apex of any API. 

Scott.MScott.M

Thanks for the quick response :)  , the reason I ask is that the subdomain on the Site object in sandboxes doesn't get updated to take into account the sandbox name. For example if the subdomain for a site in production is abc when you query the Subdomain field of the Site record in apex it will return abc. If you setup a sandbox with the name efg. The sandbox name is also a subdomain but there's no way of getting it to regenerate the abc.edf.cs3.force.com url.

 

 

 

 

forecast_is_cloudyforecast_is_cloudy

Interesting use case. Guess you'll need to hardcode the Sandbox sub-domain for now :(

DanO41DanO41
Is is a Sandbox:   Select IsSandbox From Organization
Name of Sandbox:  UserInfo.getUserName().substringAfterLast('.com')
Fabien TaillonFabien Taillon
There is now an official solution that is out with the Spring' 22 release (https://help.salesforce.com/s/articleView?id=release-notes.rn_apex_domain_classes.htm&type=5&release=236).
You can use the new getSandboxName() method:
System.Domain d = System.DomainParser.parse(URL.getOrgDomainUrl());
System.debug(d.getSandboxName());
You can get more information about this new method in the Apex Reference Guide here (https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_class_System_Domain.htm#apex_System_Domain_getSandboxName).
There are also so other new and useful methods like getMyDomainName().