Access Custom Settings with Code
You can access custom settings from Apex, SOAP API, and formulas.
Required Editions
| Available in: Salesforce Classic and Lightning Experience. |
Available in: Group, Professional, Developer, Enterprise, Performance, Unlimited, and Database.com Editions. Packages aren’t available in Database.com. |
Note Formulas include: flows, workflow rules, approval processes, validation rules, formula
fields, and Process Builder processes.
Here are some sample code segments:
- Formula Fields
- Formula fields only work for hierarchy custom settings; they can’t be used for list
custom
settings.
{!$Setup.CustomSettingName__c.CustomFieldName__c} - Apex
- Apex can access both custom setting types.
- Samples for List Custom Settings
- When you add data to a custom setting, name each data set so that you can
distinguish them. The following returns a map of custom settings data. The
getAllmethod 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
getValuesmethod to return all the field values associated with the specified data set. This method can be used with list and hierarchy custom settings, using different parameters.CustomSettingName__c mc = CustomSettingName__c.getValues(data_set_name); - Samples for Hierarchy Custom Settings
- The following example uses the
getOrgDefaultsmethod to return the data set values for the organization level:CustomSettingName__c mc = CustomSettingName__c.getOrgDefaults();The following example uses the
getInstancemethod to return the data set values for the specified profile. ThegetInstancemethod can also be used with a user ID.CustomSettingName__c mc = CustomSettingName__c.getInstance(Profile_ID);
- SOAP API
- Custom settings that have Privacy defined as Public have the same type of exposure to the API as custom objects. When a custom setting is contained in a managed package, and Privacy for a custom setting is Protected, the settings can only be accessed by the Apex code or formulas that are part of the managed package.
Note You can also access custom settings data through a Standard Object Query Language (SOQL)
query, but this method doesn't use the application cache. It’s similar to querying a custom
object.
Did this article solve your issue?
Let us know so we can improve!

