Check for Sandbox or Production in Apex
- Create a Custom Setting called Environment Variables
- Add Record in customer settings named: salesforce.com orgid with the orgid as the value__c
- Then Assert the value with the current user.
- 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
Post a Comment