+ 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 : http://salesforceworld4u.blogspot.in/2015/12/how-to-get-sandbox-name-in-apex.html
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()); 
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().