Learn how to access and use Salesforce Custom Settings in Apex. Understand the difference between List and Hierarchy Custom Settings.
Shyam Agarwal Whether you're looking to optimize your Salesforce solution or need custom development, our team is here to help you unlock the full potential of Salesforce.
Salesforce Custom Settings are essential for managing configuration data in Salesforce. They allow businesses to store organization-wide and user-specific data, improving access speed and overall system performance.
At Differenz Force, we help businesses set up and manage Salesforce Custom Settings. Our expertise optimizes your Salesforce platform, reduces query times, and improves overall system performance. Custom Settings make your Salesforce environment more scalable and efficient.
Salesforce Custom Settings are a special type of custom object that allow you to store configuration data. Unlike regular objects, Custom Settings are designed to provide easy access to information without complex queries, making them a valuable tool for optimizing performance.
They are used to store data that is needed frequently, such as global configurations, user-specific settings, or other settings that must be accessed across Salesforce applications. This data is easily accessible through Apex code or formulas and can reduce the number of SOQL queries, ultimately improving the speed and efficiency of your Salesforce system.
Salesforce offers two main types of Custom Settings: List Custom Settings and Hierarchy Custom Settings. Each serves a different purpose depending on the type of data you are storing and the specific needs of your Salesforce environment.
List Custom Settings are used to store data that remains constant across the entire organization. This type is ideal for storing configuration values that apply universally, such as global app settings. Data stored in List Custom Settings is accessible to all users without customization for individual profiles or users.
Hierarchy Custom Settings allow data to be customized based on user profiles or individual users. This means you can set different values for various teams or users within the organization. For example, you might assign different settings to the sales team, the customer support team, or even specific settings to individual users.
Salesforce Custom Settings provide a powerful way to store application-level or user-level configuration data. They help control logic, manage settings, and reduce hardcoded values. You can access Custom Settings in Apex using built-in methods such as getAll(), getValues(), and getInstance().
Salesforce offers two types of custom settings:
List Custom Settings can be retrieved using methods like getAll() and getValues().
apex
Map courses = Course_Settings__c.getAll();
apex
Course_Settings__c course = Course_Settings__c.getValues(‘Physics’);
apex
// All records as a map
Map settingsMap =
MyCustomSetting__c.getValues();
// Specific keys
List keys = new List{‘Key1’, ‘Key2’};
List selectedSettings =
MyCustomSetting__c.getValues(keys);
Replace MyCustomSetting__c with your actual custom setting API name.
Hierarchy settings allow Salesforce to return different values depending on the user, profile, or org-level settings.
apex
User_Settings__c orgDefaults = User_Settings__c.getOrgDefaults();
apex
User_Settings__c profileSettings =
User_Settings__c.getInstance(Profile_ID);
apex
User_Settings__c userSettings = User_Settings__c.getInstance(User_ID);
apex
MyCustomSetting__c currentUserSetting =
MyCustomSetting__c.getInstance(UserInfo.getUserId());
A typical setup for Salesforce DevOps may include:
| Feature | Description |
|---|---|
| Performance | Custom Settings are cached in memory for fast access (no SOQL needed). |
| Privacy & Access | Protected settings in managed packages can’t be accessed by subscriber orgs. |
| Schema Setting Option | Enable “Manage List Custom Settings Type” in Setup if needed. |
| Deployment Caution | Custom Setting data doesn’t deploy with change sets; use data loader or scripts. |
| Use Case | Apex Code |
|---|---|
| All List Records | Map<String, MySetting__c> map = MySetting__c.getAll(); |
| Specific List Record | MySetting__c setting = MySetting__c.getValues('RecordName'); |
| Multiple Specific Keys | List<MySetting__c> list = MySetting__c.getValues(keys); |
| Org-Wide Default (Hierarchy) | MySetting__c default = MySetting__c.getOrgDefaults(); |
| Profile-Level Hierarchy Setting | MySetting__c profileSetting = MySetting__c.getInstance(ProfileId); |
| User-Level Hierarchy Setting | MySetting__c userSetting = MySetting__c.getInstance(UserId); |
| Current User Hierarchy Setting | MySetting__c me = MySetting__c.getInstance(UserInfo.getUserId()); |