function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Sekar RajSekar Raj 

How to get Sandbox Name from Apex?

How to get Sandbox Name from Apex?
Frédéric TrébuchetFrédéric Trébuchet
Frédéric TrébuchetFrédéric Trébuchet

If these answers helped you solve your problem, please, mark the question as Solved and kindly select the best one ;)

Use this : system.debug(''+UserInfo.getUserName().substringAfterLast('.com'));

for more info :
Fabien TaillonFabien Taillon
There is now an official solution that is out with the Spring' 22 release (
You can use the new getSandboxName() method:
System.Domain d = System.DomainParser.parse(URL.getOrgDomainUrl()); 
You can get more information about this new method in the Apex Reference Guide here (
There are also so other new and useful methods like getMyDomainName().