Organization-Specific Variables

January 28, 2009 Appirio

If you are a developer that has done any involved programming in Apex, you have probably thought at one point or another that it would be nice to have a spot to store organization-specific global variables.

This would be useful when your Apex code references:

  • A record id, object id, or field id that is different in sandbox and production
  • A URL that needs to be different in sandbox and production
  • A soql query that includes a bound variable that needs to be different in sandbox and production due to differences in the data

You soon realize that trying to keep this information as constants in an Apex class creates a problem since you can’t make changes to most Apex code in production; so you’d be stuck with the sandbox org values in production.  Sure, you could store all this information as records in a custom object, but that requires one or more SOQL calls to retrieve the information, which you are desparately trying to avoid in order to stay on the Governor’s good side.  In other programming languages like Java, you might store this type of information in environment variables and/or properties files.

One solution for this conundrum that I use regularly is to create a custom object called CONFIG__c that has one picklist called ORG__c and no records.  Add a single picklist value in ORG__c that represents the current org in which the object is deployed.  For example, in my sandbox, I name the one picklist value “sandbox” and when I deploy the object to production, I edit the single picklist value from “sandbox” to “production”.

Next create an Apex class with a method that will inspect the metadata of CONFIG__c.ORG__c to determine what the current org is:

public class Constants {

  public static final String ORG_SANDBOX = 'sandbox';
  public static final String ORG_PRODUCTION = 'production';

  public static final String CURRENT_ORG = getOrg();
  public static String getOrg(){
    Schema.DescribeFieldResult result = CONFIG__c.ORG__c.getDescribe();
    List picklistValues = result.getPicklistValues();
    if (picklistValues == null || picklistValues.size() == 0)
      return '';
    else
      return picklistValues[0].getValue();
  }
}

The variable CURRENT_ORG can then be used to conditionally set all org-dependent constants in your Constants class in the following manner:

  public static final String BUYER_PROFILE_ID = getBuyerProfileId();
  private static String getBuyerProfileId() {
    if (CURRENT_ORG == ORG_SANDBOX)
      return '00eR0000000IAYr';
    else //if (CURRENT_ORG == ORG_PRODUCTION)
      return '00e50000000ilvj';
  } 

  public static final String BASE_SERVER_URL = getBaseServerURL();
  private static String getBaseServerURL() {
    if (CURRENT_ORG == ORG_SANDBOX)
      return 'https://cs1.salesforce.com';
    else //if (CURRENT_ORG == ORG_PRODUCTION)
      return 'https://na3.salesforce.com';
  } 

Note: This solution will use one of your field describe calls, but these are typically not in as high demand as SOQL calls.

Previous Article
Facebook Toolkit
Facebook Toolkit

Kyle RocheWe’re going to be extending the current postings for the Force for Facebook Toolkit with a few sa...

Next Article
We've Updated to the New Blogger…

We’ve just moved this blog to the latest and greatest version of Blogger to take advantage of some of the a...