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
santhosh konathala 17santhosh konathala 17 

Hi can anybody send How to access custom settings data?

Best Answer chosen by santhosh konathala 17
Amit Chaudhary 8Amit Chaudhary 8
Please check below post for custom setting
1) https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_methods_system_custom_settings.htm
2) http://amitsalesforce.blogspot.com/2014/11/dynamic-field-mapping-using-custom.html

1) Samples for List Custom Settings

When you add data to a custom setting, you must name each set of data. Then you can distinguish between the sets of data by the data set name. The following returns a map of custom settings data. The getAll method returns values for all custom fields associated with the list setting.
Map<String_dataset_name, CustomSettingName__c> mcs = CustomSettingName__c.getAll();
The following example uses the getValues method to return all the field values associated with the specified data set. This method can be used with both list and hierarchy custom settings, using different parameters.
CustomSettingName__c mc = CustomSettingName__c.getValues(data_set_name);
Example 1:-
List<Games__C> mcs = Games__c.getall().values();
boolean textField = null;
if (mcs[0].GameType__c == 'PC') {
  textField = true;
}
system.assertEquals(textField, true);

2) Hierarchy Custom Setting Examples
The following example uses the getOrgDefaults method to return the data set values for the organization level
CustomSettingName__c mc = CustomSettingName__c.getOrgDefaults();
The following example uses the getInstance method to return the data set values for the specified profile. The getInstance method can also be used with a user ID.
CustomSettingName__c mc = CustomSettingName__c.getInstance(Profile_ID);
Example 2:-
GamesSupport__c mhc = GamesSupport__c.getInstance(pid);
string mPhone = mhc.Corporate_number__c;

Example 3:- Dynamic Query and map with Custom setting
public Map<string,string> getAllMapping()
    {
        qry ='Select ';
        try{
             for (MyLeadToLeadMapping__c mappingTableRec : MyLeadToLeadMapping__c.getall().Values())
             {
                if (mappingTableRec.Name != null && mappingTableRec.Lead_Field_API_Name__c != Null )
                {
                    MapMappingTable.put(mappingTableRec.Name , mappingTableRec.Lead_Field_API_Name__c);
                    qry += mappingTableRec.Name + ',';
                }
             }
             qry =' name from account ';
        }
        catch(exception ex)
        {
            ApexPages.Message msg = new ApexPages.Message(ApexPages.Severity.ERROR, ex.getMessage());
            Apexpages.addMessage(msg);
        }
        return MapMappingTable;
    }



Let us know if this will help you


Thanks
Amit Chaudhary

All Answers

Krishna SambarajuKrishna Sambaraju
Please find the documentation on how to access data of custom settings.
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_methods_system_custom_settings.htm

Hope this helps.
SandhyaSandhya (Salesforce Developers) 
Hi Santosh,

Please refer below link which has all the information.

https://help.salesforce.com/apex/HTViewHelpDoc?id=cs_accessing.htm&language=en 

Please let us know if this helps you.

Thanks and Regards
Sandhya
Amit Chaudhary 8Amit Chaudhary 8
Please check below post for custom setting
1) https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_methods_system_custom_settings.htm
2) http://amitsalesforce.blogspot.com/2014/11/dynamic-field-mapping-using-custom.html

1) Samples for List Custom Settings

When you add data to a custom setting, you must name each set of data. Then you can distinguish between the sets of data by the data set name. The following returns a map of custom settings data. The getAll method returns values for all custom fields associated with the list setting.
Map<String_dataset_name, CustomSettingName__c> mcs = CustomSettingName__c.getAll();
The following example uses the getValues method to return all the field values associated with the specified data set. This method can be used with both list and hierarchy custom settings, using different parameters.
CustomSettingName__c mc = CustomSettingName__c.getValues(data_set_name);
Example 1:-
List<Games__C> mcs = Games__c.getall().values();
boolean textField = null;
if (mcs[0].GameType__c == 'PC') {
  textField = true;
}
system.assertEquals(textField, true);

2) Hierarchy Custom Setting Examples
The following example uses the getOrgDefaults method to return the data set values for the organization level
CustomSettingName__c mc = CustomSettingName__c.getOrgDefaults();
The following example uses the getInstance method to return the data set values for the specified profile. The getInstance method can also be used with a user ID.
CustomSettingName__c mc = CustomSettingName__c.getInstance(Profile_ID);
Example 2:-
GamesSupport__c mhc = GamesSupport__c.getInstance(pid);
string mPhone = mhc.Corporate_number__c;

Example 3:- Dynamic Query and map with Custom setting
public Map<string,string> getAllMapping()
    {
        qry ='Select ';
        try{
             for (MyLeadToLeadMapping__c mappingTableRec : MyLeadToLeadMapping__c.getall().Values())
             {
                if (mappingTableRec.Name != null && mappingTableRec.Lead_Field_API_Name__c != Null )
                {
                    MapMappingTable.put(mappingTableRec.Name , mappingTableRec.Lead_Field_API_Name__c);
                    qry += mappingTableRec.Name + ',';
                }
             }
             qry =' name from account ';
        }
        catch(exception ex)
        {
            ApexPages.Message msg = new ApexPages.Message(ApexPages.Severity.ERROR, ex.getMessage());
            Apexpages.addMessage(msg);
        }
        return MapMappingTable;
    }



Let us know if this will help you


Thanks
Amit Chaudhary
This was selected as the best answer
Rakesh51Rakesh51
I would suggest you to use Custom Metadata Types instead of custom Settings 
 
  • Perhaps the biggest advantage Custom Metadata Types have over Custom Settings is that the data themselves are considered to be Metadata. So, essentially, you have Metadata about Metadata. Technically, they are called “Components”, and the reason this is important, is because you can DEPLOY the records!
  • Since the Records themselves are Metadata, they are refreshed to Sandboxes; even those with no data. 
  • Since the Records themselves are Metadata, APEX Tests can see them even with “SeeAllData=False”. No more creating Custom Settings for Test Classes! • Custom Metadata
  • Types are more like a custom object than Custom Settings. This means it takes a little more time to create them, but you have more control such as page layouts and other features. 
  • Starting with Winter ’16, like a custom object might have, you can Track Audit Trail history! This is a must have feature! 
  • Like Custom Settings, Custom Metadata Types do not cost a SOQL when referencing them. Moreover, you can reference a Custom Metadata Type with a standard SOQL instead of using the special Custom Settings Class. For example: Select ID, Field1__c, Field2__c, … From My_CustomMetadataType__mdt
  • Like Custom Settings, all records are Cached. So you don’t need to worry about performance, and indeed, this will provide better performance over a normal table.