• David Hurtado Banda
  • NEWBIE
  • 0 Points
  • Member since 2017

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 0
    Questions
  • 1
    Replies
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?