-1

I have 10 custom settings in my org . I need to fetch all and need to use in apex class

  • 3
    Have you read the relevant documentation and examples? What specific issue are you encountering? – David Reed Mar 27 '18 at 11:43
  • Hi David, Thanks for update, I have checked doc and found only single custom settings to fetch . Iam looking for list of custom settings need to be displayed in a vf page – Bhanu Prakash Reddy Mar 27 '18 at 12:08
  • 1
    Please clarify the question: Do you need all values for a given custom setting, or do you need all the custom settings AND their values? What is your use-case for not knowing the names of all the custom settings you need to iterate over in an org? Or are you saying that you know the names of the ten custom settings and you want to fetch all 10 by name and use them in an Apex class? – DavidSchach Mar 27 '18 at 16:21
3

There is no descent way to do it, but can be achieved using a describe call.

Describe call has method that tells is if that SobjectType is a normal Sobject or a Custom Settings. We can take advantage of that to querry all custom settings value.

Algorithm goes like:

-

  1. Do Global Describe to get Whole Org's Metadata
  2. Iterate on individual Object and check if its a CustomSetting or not
  3. If Custom Setting, Using describe call on that object and get all fields of that object
  4. Create Dynamic SOQL and query the custom settings record back.

Below code snippet will help you.

Map<String, Schema.SObjectType> gd = Schema.getGlobalDescribe();

for(String objectName :gd.keySet()){
      Schema.SObjectType result=gd.get(objectName);
      if(result.getDescribe().isCustomSetting()){
              System.debug(objectName);
             String query = 'SELECT';
              // Grab the fields from the describe method and append them to the queryString one by one.
             Map<String, Schema.SObjectField> objectFields =result.getDescribe().fields.getMap();
             for(String s : objectFields.keySet()) {
                    query += ' ' + s + ',';
               }

               if (query.subString(query.Length()-1,query.Length()) == ','){
                   query = query.subString(0,query.Length()-1);
               }
               // Add FROM statement
               query += ' FROM ' + objectName;
               //System.debug(query ); //Show Querry if you want
               System.debug(Database.query(query ));//Query and Display Sobject or show on VP


      }
}
-1

You can obtain all records of a List custom setting by doing

Map<String, CustomSetting__c> settings = CustomSetting__c.getAll();

This provides a map of the setting's Name to its value. You can get a flat list by doing

CustomSetting__c.getAll().values()

where CustomSetting__c is the name of your custom setting object. This applies for List-type custom settings; for Hierarchy custom settings, which don't have a required Name field, you would simply run a SOQL query against the object:

SELECT FIELDS_DESIRED FROM CustomSetting__c
  • @BhanuPrakashReddy, several folks have asked for clarification on your question. Could you take a moment to edit it to make clear what you need? Then, if the question has been solved, please accept one of the answers. – David Reed Mar 28 '18 at 10:55
  • Thanks for updates guys, I have worked all those . I have store all custom settings in Custom meta data and worked fetching those custom settings on Visual Force Page – Bhanu Prakash Reddy Apr 3 '18 at 7:26

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.