Custom settings are similar to custom objects because they allow you to customize data in your organization. However, there are a few differences. Unlike custom objects, which have records tied to them, custom settings let you use customized data across your entire organization. Custom settings also help you identify specific users or profiles based on specific criteria you define.
The great thing about custom settings is that the data they contain is stored in the application cache. This means you can access the data quickly and efficiently without having to make repeated requests to the database. You can use this data in different parts of your organization, such as formulas, validation rules, workflows, Apex code, and the SOAP API.
Table of Contents
ToggleTypes of custom settings in salesforce
In Salesforce, there are two types of custom settings: hierarchy custom settings and list custom settings.
Hierarchy Custom Settings:
Hierarchy custom settings allow you to define different values for different levels in your organization’s hierarchy, such as at the organization, profile, or user level. These settings are useful when you need to customize data based on different groups or individuals.
Example: Let’s say you have a custom setting called “Discount Settings.” Within this custom setting, you can define different discount percentages for various profiles or users. For instance, you might set a 10% discount for “Managers” and a 15% discount for “Sales Representatives.” When these users perform certain actions, you can refer to the discount value defined in the custom setting to calculate the appropriate discount.
List Custom Settings:
List custom settings allow you to define a set of values that can be accessed throughout your organization. These settings are helpful when you want to store and retrieve multiple values without the need for hierarchical structure.
Example: Suppose you have a custom setting called “Holiday Dates.” Within this custom setting, you can create a list of specific dates that represent holidays. For instance, you might include dates like “December 25th” and “January 1st” as holidays. You can then refer to this custom setting to check if a given date falls within the list of holidays. This helps you determine if certain processes should be skipped or special actions should be taken on those days.
How to enable List Custom Settings in Apex ?
Starting from the Salesforce Spring ’18 release, the option to create List Custom Settings is disabled by default. To enable the creation of List Custom Settings, you need to activate the “Manage List Custom Settings Type” feature.
Here’s how you can enable it:
- Go to the Salesforce Setup menu.
- In the sidebar, navigate to “Data” or use the Quick Find search and type “Schema Settings”.
- Click on “Schema Settings” to access the configuration.
- Inside the Schema Settings, locate the “Manage List Custom Settings Type” option.
- Enable the “Manage List Custom Settings Type” feature.
- Save your changes.
By enabling this feature, you’ll be able to create List Custom Settings and utilize their functionality within your Salesforce org
How to access Custom settings in Apex ?
Custom settings data is indeed stored in the application cache, which allows for fast and efficient access without the need for repetitive database queries. However, it’s important to note that when querying custom settings data using Standard Object Query Language (SOQL), the application cache is not utilized. Instead, querying custom settings data via SOQL is similar to querying a custom object, and it may involve the typical database query costs.
To take advantage of the caching benefits and optimize the retrieval of custom settings data, it is recommended to use methods specifically designed for accessing custom settings data, like getInstance(), getValues(), and others, are specifically designed to efficiently retrieve custom settings data from the application cache.
Methods available in List Custom settings:
- getAll(): This method retrieves a map containing all the data sets defined for the custom setting. It provides access to the data associated with each data set.
- getInstance(dataSetName): By specifying a data set name, this method returns the specific record of the custom setting data set. It is equivalent to using the getValues(dataSetName) method as both methods provide the same object.
- getValues(dataSetName): When given a data set name, this method retrieves the record of the custom setting data set. It functions in the same way as the getInstance(dataSetName) method, returning the exact same object.
Both getInstance(dataSetName) and getValues(dataSetName) methods are used to retrieve the data set record based on the provided data set name within the custom setting.
List Custom Setting Example in Apex
Let’s assume we have a List custom setting named “DiscountSettings” with the following data sets defined:
Data set name: “Standard”, Discount percentage: 10%
Data set name: “Premium”, Discount percentage: 15%
Data set name: “VIP”, Discount percentage: 20%
// Retrieve all data sets defined for the custom setting
Map<String, DiscountSettings__c> allDiscountSettings = DiscountSettings__c.getAll();
// The key is the data set name, and the value is the custom setting record
DiscountSettings__c standardDiscountSettings = DiscountSettings__c.getInstance('Standard');
// Retrieve a specific data set using getInstance(dataSetName)
DiscountSettings__c premiumDiscountSettings = DiscountSettings__c.getInstance('Premium');
// Retrieve a specific data set using getValues(dataSetName)
DiscountSettings__c vipDiscountSettings = DiscountSettings__c.getValues('VIP');
// Accessing the discount percentage from the custom setting record
Decimal premiumDiscountPercentage = premiumDiscountSettings.Discount_Percentage__c;
Decimal vipDiscountPercentage = vipDiscountSettings.Discount_Percentage__c;
Decimal standardDiscountPercentage = standardDiscountSettings.Discount_Percentage__c;
System.debug('premiumDiscountPercentage '+premiumDiscountPercentage);
System.debug('vipDiscountPercentage '+vipDiscountPercentage);
System.debug('standardDiscountPercentage '+standardDiscountPercentage);
Methods available in Hierarchy Custom settings:
- getInstance():This method retrieves the custom setting data set record for the current user. The fields returned in the record are merged based on the lowest level fields defined in the hierarchy.
- getInstance(userId):By specifying a user ID, this method returns the custom setting data set record for the specified user. It retrieves the record with the lowest level of customization and includes the corresponding fields. Use this method when you specifically want to retrieve data for the custom setting at the user level.
- getInstance(profileId):This method retrieves the custom setting data set record for the specified profile ID. It returns the record with the lowest level of customization and includes the associated fields. Use this method when you want to explicitly retrieve data for the custom setting at the profile level.
- getOrgDefaults():The getOrgDefaults() method returns the custom setting data set record for the organization. It provides the record with the default values defined for the custom setting.
- getValues(userId):By specifying a user ID, this method retrieves the custom setting data set record for the specified user. It returns the record associated with that user.
- getValues(profileId):This method retrieves the custom setting data set record for the specified profile ID. It returns the record associated with the given profile.
These methods allow you to retrieve specific custom setting data set records based on the current user, specified user ID, profile ID, or organization. Each method returns the record with the lowest level of customization and includes the relevant fields based on the provided criteria.
Hierarchy Custom Setting Example in Apex
Let’s assume we have a hierarchy custom setting named “DiscountSettings” with the following data sets and fields:
- Organization-level data set:
- Discount Percentage: 5%
- Profile-level data set:
- Profile: “Sales”
- Discount Percentage: 10%
- User-level data set:
- User: “John Doe”
- Discount Percentage: 15%
Now, let’s see how we can use these methods in Apex:
// Retrieve the custom setting record for the current user
DiscountHierarchySettings__c currentUserDiscountSettings = DiscountHierarchySettings__c.getInstance();
// Retrieve the custom setting record for a specific user ID
Id userId = '005Dn000003E3L0IAK'; // Replace with an actual user ID
DiscountHierarchySettings__c specificUserDiscountSettings = DiscountHierarchySettings__c.getInstance(userId);
// Retrieve the custom setting record for a specific profile ID
Id profileId = '00eDn000002FvrrIAC'; // Replace with an actual profile ID
DiscountHierarchySettings__c specificProfileDiscountSettings = DiscountHierarchySettings__c.getInstance(profileId);
// Retrieve the custom setting record for the organization
DiscountHierarchySettings__c orgDefaultDiscountSettings = DiscountHierarchySettings__c.getOrgDefaults();
System.debug('specificUserDiscountSettings '+specificUserDiscountSettings.Discount_Percentage__c);
System.debug('orgDefaultDiscountSettings '+orgDefaultDiscountSettings.Discount_Percentage__c);
System.debug('specificProfileDiscountSettings '+specificProfileDiscountSettings.Discount_Percentage__c);
Custom Settings vs Custom Metadata types
Types | Custom Settings | Custom Metadata |
Type | Custom Settings, similar to custom objects, offer the flexibility to create either list-based or hierarchy-based configurations. | Custom metadata does not support the hierarchical organization of data based on user profiles or specific users. |
Deployment | The deployment of custom settings data cannot be accomplished using packages or the Metadata API/Change Sets. | Custom metadata types data can be conveniently packaged and deployed. They can be included in deployment packages and deployed using Change Sets or the Metadata API. |
Relationship Field | Custom settings do not support the usage of relationship fields. | You can create a relationship that links a custom metadata type to another metadata type |
Suffix | Custom settings has a suffix of “__C” just like custom object | Custom metadata uses a suffix of “__mdt” |
Test Class | Custom setting data is not accessible or visible within test classes. | Custom metadata types are accessible in test classes without the need for the “SeeAllData” annotation.. |
DML in Apex | In Apex, you can perform Create, Update, and Delete (CUD) operations on custom settings. | In Apex, you cannot directly perform Create, Update, and Delete (CUD) operations on custom metadata types. Custom metadata records are read-only in Apex. To modify custom metadata records, you need to update them via the Metadata API or other appropriate methods. |
SOQL | You can access custom setting data using instance methods, which allows you to retrieve the data without needing to use SOQL queries to the database | With custom metadata types, you have the flexibility to issue an unlimited number of SOQL queries for each Apex transaction to access the data. However, you can also utilize methods available for accessing and manipulating custom metadata records, providing an alternative approach to retrieving the data. |
Advantages of custom settings in salesforce:
- Centralized Configuration: Custom settings provide a centralized location to store and manage configurable data for your Salesforce application. It allows you to define and modify settings without modifying code, making it easier to customize the behavior of your application.
- Flexibility and Customizability: Custom settings offer flexibility by allowing you to define custom fields and data structures based on your specific needs. You can create custom settings at the organization, profile, or user level, enabling different configurations for different scenarios.
- Data Sharing: Custom settings data can be shared across the organization or specific user profiles, allowing you to provide consistent settings and configurations to different users or groups.
- Efficient Access: Custom settings data is stored in the application cache, enabling efficient and fast access without the need for repeated queries to the database. This can improve performance and reduce the impact on system resources.
- Avoid Hard-coding: Custom settings allow you to avoid hard-coding values in your code. Instead, you can retrieve the values from custom settings, making it easier to maintain and modify settings as needed without making changes to your codebase.
- Testability: Custom settings can be easily accessed and modified during testing, making it simpler to validate and adjust the behavior of your application during the testing phase.
- Integration Support: Custom settings can be accessed and utilized in Apex code, formulas, validation rules, workflows, and other areas of Salesforce, providing seamless integration capabilities.
Disadvantages of Custom Settings in Salesforce
- Limited Data Types: Custom settings support a limited set of data types, such as text, number, and checkbox. If you require complex data types or relationships, you may find custom settings inadequate for your needs.
- No Data Validation: Custom settings do not provide built-in data validation mechanisms. It’s your responsibility to ensure the accuracy and validity of the data stored in custom settings. Without proper validation, incorrect or inconsistent data may cause issues in your application.
- Limited Data Volume: Custom settings have data volume limits, which may restrict the amount of data you can store. If you have large amounts of data or if your data grows rapidly, you may encounter limitations and need to consider alternative storage options.
Salesforce Developer Training for Beginners