custom settings in salesforce

Article 1** Article 2** Article 3** Article 4** Article 5** Article 6** Article 7** Article 8** Article 9  ** Article 10

Custom settings are are similar to custom objects. A custom object is created by developer, similarly we have to create our own custom setting.

A custom setting like a custom object will have custom fields as per requirement. Once we have object and its fields, we can create records of that object, similarly we can create records of custom setting too(its just that we don't call it a record, otherwise it very well qualifies to be called a record).
We will call it a list of data for our reference.

If custom settings are similar to custom objects, then why do we need custom custom settings in first place? Let us try to understand advantages of custom settings in salesforce
Ans: Custom setting allows us to store set of data and access it without actually querying it in apex. So you save a select query here !

Lets understand this by an example,

Before we start the example its just worth to mention that there are two types of custom settings List and Hierarchical. As of now lets discuss List type. We will also see the difference between list and hierarchy custom settings in salesforce and how they can be used for meeting our requirements.

To understand the usage consider that we have to store data of states and its capital cities. So a state should have its capital associated with it. For this we can create a custom setting and store data in two fields.

Custom setting default gives one field "Name" so this is our name of the state. So we need one more field to store the capital of the state "Capital".

After our fields are created its time to create the data (States and its capitals) We can create this by pressing "Manage" and then new button.

All the list of data created can be viewed by pressing manage button:

The above example was for List Type custom setting. Now let us see Hierarchical Custom settings.
hierarchy custom settings in salesforce are similar to List type but in this you can assign data depending on user or profile. You can also have default data. 

For example,

You want to create custom setting for Incentive(you will store incentive amount). So your custom setting will have field Incentive amount. Any organisation will distribute its incentive based on profile/User.
A manager will definitely get more incentive than a Trainee.

So depending on the profile/user incentive be given.

In hierarchical custom setting you have a extra field "Location" here you have to specify the user or the profile.

Salesforce logically checks the profile/user settings for the current user and returns the specific data to that profile/User.

Now let us address the following question,

How to access custom setting in apex? At times we may need to manipulate the custom settings data in code logic. lets see how we can update custom setting in apex.

Let us access data from List Type custom setting,
In the above example for List type we had custom setting named "States and Capitals" and its api name "StatesandCapitals__c", it had field "Capital__c".
We had data set for this custom setting, so now let us access it in apex.

1. We will access all the states and their capital
2. We will access capital of a particular state

Our code as below,

Public CustomSettingMethod (){
   //Get all the states and their capitals
    for(StatesandCapitals__c saC: StatesAndCapitalsList ){
          System.debug('State Name is #'+ +'and it s capital is'+ saC.Capital__c);

  //Get capital of a specific state
  StatesandCapitals__c CapitalOfGoa =  StatesandCapitals__c.getValues('Goa');
  System.debug('Capital of goa is -' + CapitalOfGoa.Capital__c );

Debug statements can be used to see the data.

Article 1** Article 2** Article 3** Article 4** Article 5** Article 6** Article 7** Article 8** Article 9 ** Article 10


  1. Very nice post :) Thank you.

  2. Fantastic post..!! Want more info of Hierarchy settings.

  3. very nice explanation with example

  4. Excellent post Yogi, you simplified it

  5. It throw an error unexpected token: 'CustomSettingMethod'.

  6. Much Appriciated... Simplified and ease to understand.

  7. It throw an error variable not defined StatesAndCapitalsList, How to solve this?

    1. Hi Arpit, you have to create a new list of StatesandCapitals__c.
      List StatesAndCapitalsList = new List();
      Since in the above example, list variable has been directly used.

      Please let me know if this helps you..!!

  8. Nice info regarding custom setting in salesforce My sincere thanks for sharing this post Please Continue to share this post
    Salesforce Training in Chennai