Custom Settings in Salesforce
What is Custom Setting in Salesforce:
Custom settings are the same as Salesforce custom objects and enable developers to create records of data as well as create and associate data for an org, profile, or specific user. The custom setting data can then be used in apex code without firing a SOQL query.
There are two types of custom settings:
- List Custom Settings: These provide a set of records, each separated by a unique ‘Name’ value.
- Hierarchy Custom Settings: These provide a record which is returned on the basis of the logged in user and the profile. This is used when we don’t want multiple records but instead if we want to use a hierarchy structure based on user/profile.
Creating a Custom Setting
In order to create a custom setting, go to Setup->Search for ‘Custom Settings’
Then, open the page and click on the ‘New’ button which takes us to the Custom Setting creation screen.
We can enter the custom setting name, the type and the visibility of the custom setting to be saved. We can also create custom fields on custom settings, just like we can do for the custom objects.
Let’s say we create a hierarchy custom setting with the name ‘My First Hierarchy’ :
Click on the Save to create this custom setting. This will take to the custom setting screen, let’s create a custom text field on this custom setting called ‘Notes’.
Enter the field name as ‘Notes’ and enter the length as ‘255’ (the max possible length).
Click the Next button and click Save to create this custom field. Now we should be able to see the new ‘Notes’ field in the custom fields section.
Now, if we wanna create records for this custom we need to use the Manage button on the top.
Now, we see two sections :
- Default Organization Level Values
- Profile/User Level Values
If you click ‘New’ on the Default Organization Level Value, we can create a default record which will be returned to the user if they don’t have a record for their profile or user.
Click Save to save this default record with the ‘Notes’ value as ‘Default Org Value’.
If we click New in the Profile/User level section, we can select if we want to save the value with a Profile or a User value.
User Value :
Note: The hierarchy logic checks the organization, profile and user settings for the current logged in user and returns the most specific, or “lowest,” value. In the hierarchy custom setting, value for an organization are overridden by profile value and then Profile value is overridden by user value.
Now, if we want to create a List type custom setting, we can follow the same steps. Let’s create a list custom setting with the name : ‘My First List Setting’
This will create a list type custom setting for us, and just like the hierarchy custom setting, we can use the Manage button to create records for this custom setting.
When we create records for a list custom setting, we will see that there is standard Name field which is required and unique, which is used to fetch this record in code.
Accessing Custom Settings in Code
List Custom Settings
- If we want to fetch custom setting records for list custom settings in apex, we can use the getAll() method.
- If we want to get the list custom setting record for a specific ‘Name’, which is unique for each list custom setting record, we can do that by using the getValues() method.
Map recs = CustomSettingName__c.getAll();
This does not count as an SOQL query and returns all the list custom setting records for a particular custom setting.
CustomSettingName__c rec = CustomSettingName__c.getValues(namevalue);
Hierarchy Custom Settings
- In hierarchy custom settings, if we want to retrieve the org level default values, then we can use this syntax :
- If we want to fetch the value of a record for a particular profile or user value, we can do that as follows :
CustomSettingName__c rec = CustomSettingName__c.getOrgDefaults();
CustomSettingName__c rec = CustomSettingName__c.getInstance(Profile_ID or User_Id);