Learn how to access and use Salesforce Custom Settings in Apex. Understand the difference between List and Hierarchy Custom Settings
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.
Contact Us TodaySalesforce Custom Settings are essential for managing configuration data in Salesforce. They allow businesses to store both organization-wide and user-specific data, improving access speed and overall system performance.
At Differenz Force, we assist businesses in setting up and managing Salesforce Custom Settings. Our expertise helps optimize your Salesforce platform, reduce query times, and improve overall system performance. Custom Settings can make your Salesforce environment more scalable and efficient.
Salesforce Custom Settings are a special type of custom object in Salesforce that allow you to store configuration data. Unlike regular objects, Custom Settings are designed to provide easy access to information without the need for 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 need to be accessed across Salesforce applications. This data is easily accessible through Apex code or formulas, and can be used to 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. Both serve different purposes depending on the type of data you're 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 when you need to store configuration values that should apply universally, such as global app settings. Data stored in List Custom Settings is accessible by 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 different teams or users within the organization. For example, you may want to assign different settings to the sales team and customer support team, or even specific settings to individual users.
Salesforce Custom Settings are a powerful way to store app-level or user-level configuration data. They can be used to control logic, manage settings, and reduce hardcoded values. You can access Custom Settings in Apex using built-in methods like 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
apex
Course_Settings__c course = Course_Settings__c.getValues('Physics');
apex
// All records as a map
Map
MyCustomSetting__c.getValues();
// Specific keys
List
List
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()); |
i)Migration Restriction: Cannot be migrated between environments using change sets; needs manual recreation or data deployment tools.
ii)Governor Limits: Counts towards Salesforce governor limits, especially in Apex transactions.
iii) No Triggers or Validation Rules: Cannot have triggers, workflows, or validation rules applied.
iv) Limited API Access: Cannot be directly accessed through the Salesforce API.
v) SOQL Alternative Required: Does not support SOQL queries, requiring Apex methods like getAll and getInstance.
vi)Not for Complex Data Structures: Cannot create relationships with standard or custom objects.
vii) Limited Field Types: Does not support certain field types like lookup or formula fields.
i) Use for Configuration Data – Avoid using custom objects for settings to reduce SOQL queries.
ii) Use Hierarchy Custom Settings for User-Specific Data – Store different settings for users or profiles
iii) Keep Data Small – Large datasets are not supported, so only store necessary information.
iv) Use Apex Methods for Fast Access – Use getAll(), getValues(), and getInstance() instead of SOQL.
v) Update Regularly – Review and clean outdated settings to keep data useful.
vi) Avoid Using for Large Data Processing – Not ideal for bulk operations or frequently changing data.
vii) Do Not Use for Relationships – Custom Settings do not support relationships with other objects.
viii) Secure Sensitive Data – Custom Settings do not provide field-level security, so avoid storing confidential information.
ix) Use List Custom Settings for Global Values – Store organization-wide constants efficiently.
x) Consider Custom Metadata for Deployment Needs – If you need to migrate settings, Custom Metadata might be a better option.