Skip to main content

Check for Salesforce.com Environment (isSandbox)


Check for Sandbox or Production in Apex

  1. Create a Custom Setting called Environment Variables
  2. Add Record in customer settings named: salesforce.com orgid with the orgid as the value__c
  3. Then Assert the value with the current user.
  4. isSandbox will return true for sandbox, false for production

 //Grab Environment Variables
    private static map<string,string> Settings{ 
        get{
            if(settings==null){
                map<string,Environment_Variable__c> s = Environment_Variable__c.getAll();
                settings = new map<string,string>();
                    for(string x: s.keySet()){
                    //iterate to make lowercase
                        settings.put(x.tolowercase(),s.get(x).Value__c);
                    }
            } 
            return settings;
         } 
         set;
    }
    //Check Custom Settings for match on orgid
    private static boolean isSandbox{
        get{
            if(isSandbox==null){
                isSandbox = true;
                if(Settings.containskey('salesforce orgid') && Settings.get('salesforce.com orgid')==userinfo.getOrganizationId()){
                    isSandbox = false;
                }    
            }
            system.debug('Environment = Sandbox?: '+ isSandbox);
            return isSandbox;
        }
        set;
    }


Comments

Popular posts from this blog

Override Name in the Salesforce Cloud Console

Service Cloud Console HyperLink Opens new window outside of the console, Opens new tab inside the console. HYPERLINK("javascript:if(typeof(srcUp)=='function') {srcUp('/" & Id & "?isdtp=vw');}"+ " else {window.location.href='/" & Id & "'}", User__r.FirstName + ' ' + User__r.LastName, "_self" ) Checks if the srcUp exists and if it does, you're in the console.