function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Mat SchafferMat Schaffer 

Writing Classes for multiple orgs with/without RevenueForecast

So I'm working on a component which uses quotas (via RevenueForecast object), but I'd like the app to be able to operate without quotas.

 

I have a method in my controller like this:

  public RevenueForecasts[] getRevenueForecasts() { ... }

 

But if I try to deploy the app into an org that doesn't have customizable forecasting enabled I get a compile error because the org doesn't know about RevenueForecast.

 

Is there some way to code to a generic type (maybe sObject?) so that I can just return a null quota if the org doesn't have RevenueForecast objects available?

 

Thanks,

Mat

Best Answer chosen by Admin (Salesforce Developers) 
rungerrunger

 

public List<Sobject> getRevenueForecasts() {
  Map<String, Schema.SObjectType> gd = Schema.getGlobalDescribe(); // storing this off in a static variable is better, 
                                                                   // you're limited in how many times you can call it
  Schema.SObjectType sot = gd.get('RevenueForecast');
  if (sot != null) {
    return Database.query('SELECT ... FROM RevenueForecast ...');
  }
  else {
    return null;
  }
}

 

 

All Answers

rungerrunger

Yes, you can use describes to determine if the sobject type RevenueForecast is available, and dynamic apex to do the query:

 

 

rungerrunger

 

public List<Sobject> getRevenueForecasts() {
  Map<String, Schema.SObjectType> gd = Schema.getGlobalDescribe(); // storing this off in a static variable is better, 
                                                                   // you're limited in how many times you can call it
  Schema.SObjectType sot = gd.get('RevenueForecast');
  if (sot != null) {
    return Database.query('SELECT ... FROM RevenueForecast ...');
  }
  else {
    return null;
  }
}

 

 

This was selected as the best answer
Mat SchafferMat Schaffer

Worked like a charm! Thanks!