Where am I ? Salesforce

It’s an age old question: “Where the hell am I?”  I don’t like to stop and ask for directions and neither does my code.  I’m surprised it took me this long to require some way for my code to be able to detect the environment in which it was running.

I needed to answer these two questions:

  1. Is this a sandbox?
  2. What edition is this? (enterprise, developer, etc)

I’ll make the recipe a little more universally helpful.  The idea is that you can use this class to easily help your code answer these questions when it’s running all over the cloud like a banshee.

So obviously you want to copy and paste the code below into a new Apex Class.  Then call any one of the methods from within your own code to get the info you need.  

For example: String myEdition = WhereAmI.getEdition();

                      Boolean isSandbox =  WhereAmI.isSandbox();

Class:

public  class WhereAmI {	

        //Tell me am i in sandbox or production
	public static Boolean isSandbox(){

		Organization org = new Organization();
		org = [ select IsSandbox  from Organization limit 1];
                return org.IsSandbox;
	}	

        //Tell me which edition am i in.
	public static String getEdition(){

		Organization org = new Organization();
		org = [ select OrganizationType  from Organization limit 1];
		return org.OrganizationType;
	}
}

 

Reference :

Organization object fields.

Rating: 
No votes yet