Custom settings: List and Hierarchy custom settings




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. 

Benefits of Using Custom Setting:

  • Custom settings data is exposed in the application cache, which enables efficient access without the cost of repeated queries to the database.
  • Custom settings data can be used by formula fields,validation rules, Visualforce, Apex, workflow rules, approval processes, custom button and the Force.com Web Services API.
  • You can make visibility of custom setting public or protected.
  • They  do not count against SOQL limits when fetched

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


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/

Share this

Related Posts

Previous
Next Post »