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

Get in Touch with Our Salesforce Experts

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 Today

Salesforce 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.

Meaning of Salesforce Custom Settings

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.

Types of Salesforce Custom Settings

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.

1. List Custom Settings

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.

Use Cases:

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

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

Use Cases:

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

Accessing Salesforce Custom Settings in Apex

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

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:

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.

Code Summary – Quick Reference Table

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

Limitations of Salesforce Custom Settings

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.

Best Practices for Using Salesforce Custom Settings

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.

Frequently Asked Questions