This post will cover the Custom Settings concept in Salesforce. We will also talk about how to get started with Custom Settings and when to use a Custom Setting.
What is Custom Setting?
Custom Settings are like custom objects. They are generally used to create custom sets of data and can be associated with an organization, profile or a specific user. The visibility of the custom setting can be controlled by marking it as public or protected. Custom Settings data set can be used in Formula fields, Validation rules, flows, Apex and SOAP API.
Types of Custom Settings
In Salesforce we have two type of custom settings
1) List Custom Settings
2) Hierarchy Custom Settings
|List Custom Settings||Hierarchy Custom Settings|
|It provides a reusable set of static data which|
can be accessed across your organization.
|It uses a built-in hierarchical logic which lets you personalize settings for a specific user or a profile.|
|The data in List Custom Settings is directly visible to any user in the org.||The data in Hierarchy Custom Settings checks the organization, profile and user settings for the current user and makes the data visible for them accordingly.|
The main advantage of using Custom Settings is that the data is cached, which enables efficient access without the cost of repeated queries to the database. One doesn’t have to use SOQL queries which count against the governor limits.
How to create a Custom Settings?
Search for Custom Settings in your setup, and create new Custom Setting and enter the required information in the fields.
Note: If the setting type is disabled, go to schema settings from setup and enable “Manage list custom settings type”.
Now, create Custom fields under the custom setting definition that we just created.
Now Click on Manage from the custom setting definition to add data.
Now that we have created a List Custom Setting, Let’s see how to access it in your apex.
Accessing List Custom Setting data
We can access the data using the custom settings methods. They are all instance methods, that is, they are called by and operate on a specific instance of a custom setting.
- To fetch the custom fields associated with a list setting, use
getAll()method. It returns a map of data set names and custom setting records
Map<String_data_set_name, CustomSettingApiName> var = CustomSettingApiName.getAll(); Eg: Map<String, Student__c> students = Student__c.getAll();
- To fetch all the field values associated with the specified dataset, use
getValues()method. This method can be used for both list and hierarchy settings.
CustomSettingApiName var = CustomSettingApiName.getValues(dataset_name); Eg: Student__c stud = Student__c.getValues('John');
Accessing Hierarchy Custom Setting data
You can similarly try accessing the Hierarchy Custom Setting data with the following methods.
- To return a custom setting data set record for the current logged in user. Use the
CustomSettingApiName var = CustomSettingApiName.getInstance();
- To return a custom setting data set record for a specific userId/ProfileId, Use the
CustomSettingApiName var = CustomSettingApiName.getInstance(userId/ProfileId);
- To return the custom setting record data set for the organization, Use the
CustomSettingApiName var = CustomSettingApiName.getOrgDefaults();
Note: To make the Custom Setting data visible in your apex test class, use “seeAllData=true”.
Limits on Custom Settings
Here are a few limits that needs to be considered before using Custom Settings
- We can have up-to 300 fields per custom setting.
- Sharing a Custom Setting object or record is not possible.
- Since the Custom Settings are a type of custom object, each custom setting counts against the total number of custom objects available in your org.
- The total amount of cached data allowed for your org is the lesser of these two values:
- 10 MB
- 1 MB multiplied by the number of full featured user licenses in your org.
For example, if your org has three full licenses, you have 3 MB of custom setting storage. If your org has 20 org licenses, you have 10 MB of storage.
this is great, thanks. Having trouble getting my newbie head around this and not much helpful info out there. I inherited an org that used custom settings and trying to make sense of it all. a real-life example of how a hierarchy setting could be used would be extremely helpful. I get the ‘cache’ benefit, but is the complexity/effort worth it vs just using a regular object table? Thanks!