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
bretondevbretondev 

Apex : How to know current environment?

Hello

We want to know in which environment we are when running Apex, more precisely we need to know if we are in Production, or Sandbox A, or Sandbox B, etc..

At the moment, we are looking at Org Id to determine the environment, like this :
 
public static Boolean IsProductionOrg() { // Method to check of environement is Production ORG or not
        return UserInfo.getOrganizationId() == '00ecqzecqc1IkdlUAC' ? true : false; 
    }
    
        /**
     * This method retun whether this org is a production or a sandbox
     * @return true/false
     */
    public static Boolean IsPProdOrg() { // Method to check of environement is PPROD Sandbox or not
       return UserInfo.getOrganizationId() == 'cdscddscdsccsdc'  ? true : false; 
    }

We do not like this method because everytime we refresh the Sandbox, the Org IDs of the Sandboxes change and we have to update the code.

I have found this link :
http://salesforceworld4u.blogspot.com/2015/12/how-to-get-sandbox-name-in-apex.html

It says we can look at the suffix of the logged-in user, then we can find out if it is Sandbox or Production.
For example, if logged-in user is john.smith@mycompany.com.pprod , than means environment is pprod Sandbox.

We also do not like this method because we are afraid users can change their username and remove the suffix, so method will not work anymore.

What is the clean way of doing this?


 
Steven NsubugaSteven Nsubuga
If all you want to know is whether you are in sandbox or production the just use this
public static Boolean IsProductionOrg() { // Method to check of environement is Production ORG or not
        Organization org = [select IsSandbox from Organization where Id =:UserInfo.getOrganizationId()];
        return (org.IsSandbox == true) ? false : true;
    }
    
       

 
Raj VakatiRaj Vakati
You cannt get the Enr name direcly ... 

create a label and store the name of env .. 

after that youneed to return the label or setting is the IsSandbox  is true 
 
public static Boolean IsProductionOrg() { // Method to check of environement is Production ORG or not
        Organization org = [select IsSandbox from Organization where Id =:UserInfo.getOrganizationId()];
        return (org.IsSandbox == true) ? false : true;
    }

 
bretondevbretondev
Thanks but your solution is not sufficient because we have multple Sandboxes and I need to know the name of the Sandbox
David Hurtado BandaDavid Hurtado Banda
SELECT InstanceName FROM Organization WHERE Id = :UserInfo.getOrganizationId()
Brian CaughellBrian Caughell
This is an old question, but in case anyone else is looking for a solution, you can now get the sandbox name via the System.DomainParser class:
 
String orgName = System.DomainParser.parse(URL.getOrgDomainUrl()).getSandboxName() == null ? 'production' : System.DomainParser.parse(URL.getOrgDomainUrl()).getSandboxName() ;

getSandboxName returns null if run in production.

 
Paul Linder 1Paul Linder 1
@Brian Caughell, thank you. Your solution gives the exact sandbox name.