The Ultimate Guide to Custom Settings in Salesforce

Introduction

custom Settings in Salesforce are a powerful way to store data that is specific to your application. This data can be accessed quickly without the need for database queries. In this guide, we will explore the types of Custom Settings, the methods used to access them, and the differences between these methods. By the end, you’ll have a clear understanding of how to use Custom Settings effectively.


Types of Custom Settings

Salesforce provides two main types of Custom Settings:

1. List Custom Settings

  • Purpose: Stores a collection of data records that apply universally to your organization.
  • Key Features:
    • Works like a custom object.
    • Best for data like lookup tables or configurations that don’t change often.
  • Example Use Case: Mapping country codes to country names.

2. Hierarchy Custom Settings

  • Purpose: Provides data that can be customized at the organization, profile, or user level.
  • Key Features:
    • Built-in hierarchy that prioritizes User > Profile > Organization Default.
    • Ideal for settings that vary by user or profile.
  • Example Use Case: Managing user-specific limits or preferences.

Methods to Access Custom Settings

Salesforce provides several methods to retrieve data from Custom Settings. These methods depend on the type of Custom Setting and the use case.

For List Custom Settings

  • Description: Retrieves a specific record using its unique name or key.
  • Use Case: Use this when you need to access one specific record.
  • Example:
MyCustomSetting__c setting = MyCustomSetting__c.getValues('RecordName');
if (setting != null) {
    System.debug(setting.SomeField__c);
}
  • Description: Retrieves all records in the List Custom Setting as a map of key-value pairs.
  • Use Case: Ideal for bulk operations or when you need to access multiple records at once.
  • Example:
Map<String, MyCustomSetting__c> settingsMap = MyCustomSetting__c.getAll();
MyCustomSetting__c specificSetting = settingsMap.get('RecordName');
if (specificSetting != null) {
    System.debug(specificSetting.SomeField__c);
}

For Hierarchy Custom Settings

  • Description: Retrieves the most specific record for the current user or profile.
  • Use Case: Use this to dynamically access settings based on the user’s context.
  • Example:
MyHierarchySetting__c setting = MyHierarchySetting__c.getInstance();
if (setting != null) {
    System.debug(setting.SomeField__c);
}
  • Description: Fetches the organization-wide default settings.
  • Use Case: Useful when you want to ignore user or profile-specific settings.
  • Example:
MyHierarchySetting__c orgSetting = MyHierarchySetting__c.getOrgDefaults();
if (orgSetting != null) {
    System.debug(orgSetting.SomeField__c);
}

Differences Between Methods

MethodCustom Setting TypeInputScopeFallback
getValues(key)ListRecord Name or KeyFetches one specific recordReturns null if not found
getAll()ListNoneRetrieves all recordsN/A
getInstance()HierarchyNoneResolves hierarchy (User > Profile > Org)Falls back to Org Default if none exist
getOrgDefaults()HierarchyNoneFetches organization-wide defaultsN/A

When to Use Each Method

  • Use “ when you need a specific record from a List Custom Setting.
  • Use “ for bulk processing or when accessing all records in a List Custom Setting.
  • Use “ to dynamically retrieve settings for a specific user or profile in Hierarchy Custom Settings.
  • Use “ to fetch only the organization-wide defaults for a Hierarchy Custom Setting.

Examples

List Custom Setting Example

  • Scenario: You want to store and retrieve tax rates for different states.
  • Code:
TaxRateSetting__c taxRate = TaxRateSetting__c.getValues('California');
if (taxRate != null) {
    System.debug('Tax Rate: ' + taxRate.TaxRate__c);
}

Hierarchy Custom Setting Example

  • Scenario: Define email limits for different profiles.
  • Code:
EmailLimitSetting__c emailLimit = EmailLimitSetting__c.getInstance();
if (emailLimit != null) {
    System.debug('Email Limit: ' + emailLimit.Limit__c);
}

Bonus Tips: getInstance() vs getValue() in Custom Settings

FeaturegetInstance()getValue()
PurposeRetrieves a single record of a Custom Setting.Retrieves the value of a specific field in a Custom Setting record.
Use CaseUsed with Hierarchy Custom Settings to get the entire record.Used to access a specific field value from the Custom Setting.
Data Type ReturnedReturns the full Custom Setting record (e.g., AppConfig__c).Returns the field value (e.g., String, Integer).
Checks User, Profile, OrganizationIf the record is not found at the user level, it checks the profile level, and then the organization level.Only retrieves the field value at the user level. If not found, no fallback check is performed.
Common UseWhen you need to get the entire settings record, with fallbacks to profile or organization data.When you need the value of a specific field within a Custom Setting, with no fallback.
ExampleAppConfig__c config = AppConfig__c.getInstance();String settingValue = (String) config.getValue('SettingValue__c');

Conclusion

Custom Settings are a handy tool for developers to manage configurations in Salesforce efficiently. By understanding their types and methods, you can tailor your solutions to meet both organizational and user-specific needs. Whether you’re working with List Custom Settings or Hierarchy Custom Settings, these methods will help you implement scalable and maintainable solutions.

If you found this guide helpful, share it with others and leave a comment below to let us know how you’re using Custom Settings in your projects!

Leave a Comment