Print this page

Determine the organization's edition and environment via APEX code

Knowledge Article Number 000181384
Description There might be a use case wherein the user would need to identify in which edition the code is running.
Resolution
Solution:

In order to detect the organization, we need to query the Organization object for field OrganizationType.

Check the detail code below on how this is done:

public class whereAmI{
 
 
    // First method will give us the edition as a string 
 
    public Static String getEdition(){
 
        // Organization object FTW!
        Organization[] org = new Organization[]{};
        org = [select Id, OrganizationType from Organization limit 1];
 
        // Check to make sure we have a result
        if(org.size()==1)
            return org[0].OrganizationType;
        else
            return '[still lost...]';
    }
 
 
    // Next method tells us if we're in a sandbox
 
    public Static Boolean isSandbox(){
 
        String host = URL.getSalesforceBaseUrl().getHost();
        String server = host.substring(0,host.indexOf('.'));
 
        // It's easiest to check for 'my domain' sandboxes first 
        // even though that will be rare
        if(server.contains('--'))
            return true;
 
        // tapp0 is a unique "non-cs" server so we check it now
        if(server == 'tapp0')
            return true;
 
        // If server is 'cs' followed by a number it's a sandbox
        if(server.length()>2){
            if(server.substring(0,2)=='cs'){
                try{
                    Integer.valueOf(server.substring(2,server.length()));
                }
                catch(exception e){
                    //started with cs, but not followed by a number
                    return false;
                }
 
                //cs followed by a number, that's a hit
                return true;
            }
        }
 
        // If we made it here it's a production box
        return false;
    }
}




promote demote