Logo

Differenz Force

We make a difference
  • Home   »  
  • Blog   »  
  • Salesforce Custom Settings

Salesforce Custom Settings

Learn how to access and use Salesforce Custom Settings in Apex. Understand the difference between List and Hierarchy Custom Settings.

Salesforce Custom Settings
Table of Contents

Get in Touch with Our Salesforce Experts

Contact Us Today

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.

Meaning of Salesforce Custom Settings

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.

Types of Salesforce Custom Settings

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.

1. List Custom Settings

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.

Use Cases:

  • Store global app configurations.
  • Ideal for shared system settings.
  • No user-specific customization required.

2. Hierarchy Custom Settings

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.

Use Cases:

  • Store user-specific or profile-specific data.
  • Ideal for personalized configuration settings.
  • Supports different settings for each user or profile.

Accessing Salesforce Custom Settings in Apex

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().

1. Types of Salesforce Custom Settings

Salesforce offers two types of custom settings:

List Custom Settings

  • Work like custom objects.
  • Store global/static data shared by all users.
  • Accessible by record name (key).

Hierarchy Custom Settings

  • Allow different values based on:
    • Organization level
    • Profile leve
    • User level
  • Useful for role-based or user-specific preferences.

2. Accessing List Custom Settings

List Custom Settings can be retrieved using methods like getAll() and getValues().

a) Fetching All Records

apex

Map courses = Course_Settings__c.getAll();

  • Retrieves all records stored in the Course_Settings__c setting.
b) Fetching a Specific Record by Name

apex

Course_Settings__c course = Course_Settings__c.getValues(‘Physics’);

  • Retrieves the record where the name is ‘Physics’.
c) Using getValues() for All or Specific Keys

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.

3. Accessing Hierarchy Custom Settings

Hierarchy settings allow Salesforce to return different values depending on the user, profile, or org-level settings.

a) Fetching Organization-Wide Defaults

apex

User_Settings__c orgDefaults = User_Settings__c.getOrgDefaults();

  • Returns the default (global) values for the org.
b) Fetching Settings for a Specific Profile

apex

User_Settings__c profileSettings =
User_Settings__c.getInstance(Profile_ID);

  • Replace Profile_ID with the actual Profile ID.
  • Returns the settings for that profile.
c) Fetching Settings for a Specific User

apex

User_Settings__c userSettings = User_Settings__c.getInstance(User_ID);

  • Replace User_ID with the actual User ID.
  • Returns settings applied to that user.
d) Fetching Current User’s Settings

apex

MyCustomSetting__c currentUserSetting =
MyCustomSetting__c.getInstance(UserInfo.getUserId());

  • Useful for user-specific configurations.

Key Considerations

A typical setup for Salesforce DevOps may include:

FeatureDescription
PerformanceCustom Settings are cached in memory for fast access (no SOQL needed).
Privacy & AccessProtected settings in managed packages can’t be accessed by subscriber orgs.
Schema Setting OptionEnable “Manage List Custom Settings Type” in Setup if needed.
Deployment CautionCustom Setting data doesn’t deploy with change sets; use data loader or scripts.

Code Summary – Quick Reference Table

Use CaseApex Code
All List RecordsMap<String, MySetting__c> map = MySetting__c.getAll();
Specific List RecordMySetting__c setting = MySetting__c.getValues('RecordName');
Multiple Specific KeysList<MySetting__c> list = MySetting__c.getValues(keys);
Org-Wide Default (Hierarchy)MySetting__c default = MySetting__c.getOrgDefaults();
Profile-Level Hierarchy SettingMySetting__c profileSetting = MySetting__c.getInstance(ProfileId);
User-Level Hierarchy SettingMySetting__c userSetting = MySetting__c.getInstance(UserId);
Current User Hierarchy SettingMySetting__c me = MySetting__c.getInstance(UserInfo.getUserId());

Limitations of Salesforce Custom Settings

  • Migration Restriction: Cannot be migrated between environments using change sets; requires manual recreation or data deployment tools.
  • Governor Limits: Counts toward Salesforce governor limits, especially in Apex transactions.
  • No Triggers or Validation Rules: Cannot have triggers, workflows, or validation rules applied.
  • Limited API Access: Cannot be accessed directly through the Salesforce API.
  • SOQL Alternative Required: Does not support SOQL queries, requiring Apex methods such as getAll and getInstance.
  • Not for Complex Data Structures: Cannot create relationships with standard or custom objects.
  • Limited Field Types: Does not support certain field types, such as lookup or formula fields.

Best Practices for Using Salesforce Custom Settings

  • Use for Configuration Data – Avoid using custom objects for settings to reduce SOQL queries.
  • Use Hierarchy Custom Settings for User-Specific Data – Store different settings for users or profiles.
  • Keep Data Small – Large datasets are not supported, so store only necessary information.
  • Use Apex Methods for Fast Access – Use getAll(), getValues(), and getInstance() instead of SOQL.
  • Update Regularly – Review and remove outdated settings to keep data relevant.
  • Avoid Using for Large Data Processing – Not suitable for bulk operations or frequently changing data.
  • Do Not Use for Relationships – Custom Settings do not support relationships with other objects.
  • Secure Sensitive Data – Custom Settings do not provide field-level security, so avoid storing confidential information.
  • Use List Custom Settings for Global Values – Store organization-wide constants efficiently.
  • Consider Custom Metadata for Deployment Needs – If you need to migrate settings, Salesforce Custom Metadata may be a better option.