Take the 2-minute tour ×
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It's 100% free, no registration required.

My app uses different types of custom settings. Let's call them SettingA__c and SettingC_c.

I want a method that handles both types of settings polymorphically.

private void handle(SObject setting) {
   String name = (String) setting.get('Name');

   if(setting.getValues(name) != null) {
      doSomethingWith(setting);
   }
}

This is not possible as getValues() is undefined on SObject and I could not find a common base object for Custom Settings in the docs.

share|improve this question

1 Answer 1

up vote 4 down vote accepted

This question and answer Dynamic invocation (or emulation) of the hierarchy custom setting getInstance() method are related. The solution there is to use SOQL to access the custom setting (impacting the SOQL governor limit) and then you can use the normal dynamic SOQL methods on the result.

It sounds like you are using list custom settings not hierarchical ones so your query can be simpler and pretty much like querying a normal SObject.

share|improve this answer
2  
Do keep in mind that soql on custom settings does not leverage cache, which could be part of why you're using custom settings. –  Samuel De Rycke Mar 19 at 8:44
1  
You also lose the intelligent hierarchical defaulting for hierarchical settings by doing this, but there's no other way of doing it dynamically. –  ca_peterson Mar 19 at 16:43

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.