Custom settings are similar to custom objects and enable application developers to create custom sets of data, as well as create and associate custom data for an organization, profile, or specific user. All custom settings data is exposed in the application cache.
Types Of Custom Settings
There are two types of custom settings:
- List Custom Settings A type of custom setting that allow you to store org-wide static data that is frequently used.This custom setting is equivalent to custom / standard object. We can store values in tabular format.
- Hierarchy Custom Settings A type of custom setting that uses a built-in hierarchical logic that checks the organisation, profile, and user settings for the current user and returns the most specific, or “lowest,” value.
- You can only access hierarchy custom settings in formula field and validation rules.
- Most unique feature about Hierarchy custom setting is that we can have different values at Organization level, profile level and user level.
- Custom setting can be used in formula fields using global variable $Setup. Syntax to use custom setting is {!$Setup.CustomSettingName__c.CustomFieldName__c}
- Highest priority is given to value defined at user level then profile and at last organization level.
- Once created these settings can be used even in formula fields or normally in APEX code to store information
You Can enable list custom setting schema Setting Page
Setup > schema Setting Page > List custom Setting
Create List Custom Setting
Add field Currency to custom settings
Select Manage to add data to the field
Below is the list of data I added in Currency_c Field
Methods of custom setting
- getAll()
Returns a map of the data sets defined for the custom setting. - getInstance(dataSetName)
Returns the custom setting data set record for the specified data set name. This method returns the exact same object as getValues(dataSetName). - getValues(dataSetName)
Returns the custom setting data set record for the specified data set name. This method returns the exact same object as getInstance(dataSetName).
List<Country_Currency__c> cc = Country_Currency__c.getall().values();
system.debug(cc);
Country_Currency__c cc = Country_Currency__c.getInstance(‘India’);
system.debug(cc);
Map<string,Country_Currency__c> mapCurrency = Country_Currency__c.getAll();
// display the currency for India
System.debug(‘Currency: ‘+mapCurrency.get(‘India’).Currency__c);
Country_Currency__c myCS1 = Country_Currency__c.getValues(‘United States’);
String myCCVal = myCS1.Currency__c;
Country_Currency__c myCS2 = Country_Currency__c.getInstance(‘United States’);
String myCCInst = myCS2.Currency__c;
system.debug(myCCVal);
system.debug(myCS2);
system.assertEquals(myCCinst, myCCVal);
Hierarchy Custom Settings
We can create custom fields in custom settings and store data in them
We can create custom fields in custom settings and store data in them
After you define your custom settings and add fields, you need to populate the fields with data.
Reference
https://blog.jeffdouglas.com/2010/01/07/using-list-custom-settings-in-salesforce-com/
https://www.jitendrazaa.com/blog/salesforce/use-hierarchy-custom-settings-to-avoid-hard-coding-in-formula-field-custom-button-process-builder-or-workflow-rules/
Benefits of Using Custom Setting: