Custom Setting in Salesforce

                              Custom settings

Custom settings are similar to custom objects in that they let you customize org data. Unlike custom objects which have records based on them, custom settings let you utilize custom data sets across your org, or distinguish particular users or profiles based on custom criteria.

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.
 
Say for ex. 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.
Hierarchical Custom Settings 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 organization 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?

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 #'+ saC.name +'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.


What is the difference between Custom Setting Type "List" and "Hierachy"?
Hierarchy custom settings can be configured at varying user specificity levels under a single name; the platform will look for the most specific configuration first, then fall back / inherit to the least specific:
  • setting per user,
  • setting per profile,
  • setting for whole org,
Hierarchy custom settings are easily retrieved in Visualforce: {!$Setup.Setting__c.Field__c} and will be resolved according to the current user context.
hierarchy custom setting
List custom settings have user-independent values, retrieved under multiple names. For example, you could use several configs to hold the credentials for a web service callout so that you can swap the endpoints:
  • setting for development environment,
  • setting for volume testing environment,
  • setting for production environment,
List custom settings need to be accessed using a controller if you want to avail them on a page, for example return ListSetting__c.getInstance('dev').Field__c;
list custom setting
Helpful link:
http://blog.jeffdouglas.com/2010/01/07/using-list-custom-settings-in-salesforce-com/
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_methods_system_custom_settings.htm  


Accessing Custom Settings

 

You can access custom settings from formula fields, validation rules, Apex, and the SOAP API. Some sample code segments are provided below.
Formula Fields
Formula fields only work for hierarchy custom settings; they can’t be used for list custom settings.
{!$Setup.CustomSettingName__c.CustomFieldName__c} 

 

Samples for List Custom Settings
When you add data to a custom setting, you must name each set of data. Then you can distinguish between the sets of data by the data set name. The following returns a map of custom settings data. The getAll method returns values for all custom fields associated with the list setting.
Map<String_dataset_name, CustomSettingName__c> mcs = CustomSettingName__c.getAll();
The following example uses the getValues method to return all the field values associated with the specified data set. This method can be used with both list and hierarchy custom settings, using different parameters.
CustomSettingName__c mc = CustomSettingName__c.getValues(data_set_name);
Samples for Hierarchy Custom Settings
The following example uses the getOrgDefaults method to return the data set values for the organization level:
CustomSettingName__c mc = CustomSettingName__c.getOrgDefaults();
The following example uses the getInstance method to return the data set values for the specified profile. The getInstance method can also be used with a user ID.
CustomSettingName__c mc = CustomSettingName__c.getInstance(Profile_ID);
SOAP API
Custom settings that have Privacy defined as Public are exposed to the API in the same way custom objects are exposed.
If Privacy is defined as Protected, and the custom setting is contained in a managed package, the custom setting is not accessible using the API in either the developer organization or a subscribing organization.
Use any tool with API access to perform query or profile-permission-setting operations.
You can also access custom settings data through a Standard Object Query Language (SOQL) query, but this method doesn't make use of the application cache. It’s similar to querying a custom object.

 



Comments

Popular posts from this blog

All about workflow rule , limitation and important points with example

Action tags in VisualForce Salesforce