2

I want to get all of the custom settings in a list and then according to the custom setting i want to build a query? Can someone suggest how i can do that.

For eg: i have a custom setting as A1, A2,A3. I want to do something like: List<A1> A1list = A1.getall().values(); I have to repeat the same code for all. How can i optimise this, by using something like sObject.getGlobaldescribe().

Thanks in advance.

3

If your Custom Setting were named Custom_Setting__c, you could do the following:

List<Custom_Setting__c> customSettings = new List<Custom_Setting__c>();
for (Custom_Setting__c customSetting : [SELECT Name FROM Custom_Setting__c])
{
    customSettings.add(customSetting);
}

However, since they are all different Custom Settings you would need to do separate queries, which would be more expensive than

List<Custom_Setting_A__c> listA = Custom_Setting_A__c.getAll().values();
List<Custom_Setting_B__c> listB = Custom_Setting_B__c.getAll().values();
List<Custom_Setting_C__c> listC = Custom_Setting_C__c.getAll().values();

That's about as lightweight as it will get.

  • Thanks for the reply. I want to remove the section "List<Custom_Setting_A__c> listA = Custom_Setting_A__c.getAll().values(); List<Custom_Setting_B__c> listB = Custom_Setting_B__c.getAll().values(); List<Custom_Setting_C__c> listC = Custom_Setting_C__c.getAll().values();" i.e. specifiying the name of the custom setting.getall.values(). That means i want ot make it more generic. Is this not possible. – Sushma Rao Aug 1 '13 at 3:59
  • 1
    No. They're essentially separate objects. It's like if you were trying to combine a query for Opportunities and Contacts, except you can't even use lookups to get around query limitations. – Adrian Larson Aug 1 '13 at 18:18
1

There is a slightly round-about way of getting the list of Custom Settings using the metadata api. In this example, i use the REST api to access it.

You can get you all of the objects in your org. Each type of Custom Setting is saved as a different custom object definition. So, if you iterate through this list, only look at those objects that have the 'customSetting' property = true. Here's an abbreviated version of how I do it in Groovy with the REST API (some sections omitted). You can find documentation about the response in the REST API Guide

def conn = new SfRestConnection(username, password, clientId, clientSecret, SfRestConnection.SfAuthenticationEndpointForProd)
conn.login()

//as an example: https://na1.salesforce.com/services/data/v26.0/sobjects/ 
def results = conn.get('sobjects', null, null)
for(def obj in results.sobjects)
{
    if(obj.customSetting)
        println obj.name
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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