Custom Settings in Salesforce - With Example

Custom Settings Salesforce
 Help Tag description of Custom Settings :  "Use custom settings to create and manage custom data at the organization, profile, and user levels. Custom settings data is stored in the application cache. This means you can access it efficiently, without the cost of repeated queries. Custom settings data can be used by formula fields, Visualforce, Apex, and the Web Services API." 

Custom settings we introduced in winter 10 release and has 2 variations  List and hierarchy .They are similar to custom objects,used to store data and are accessible using their own API's. 

The difference between a custom setting and a custom object is that the custom settings are stored in application cache(so they are fast) and are not counted against the SOQL limits.

Custom settings are generally used to avoid hard coding in the Apex codes or to store configurable parameters. Lets take an example where we want to display/hide certain output panel in the Visualforce page based upon the profile of the user. This can be achieved by hardcoding the profile names in the controller and set the logic render the output panel in the VF page . In this scenario if any new profile is added or if any changes is required in the existing profiles , the code has to be modified to accommodate the new requirement. 

Lets see how we can accomplish it using Custom Settings.

Step 1: Create Custom Setting. Click Setup-->Develop --> Custom Settings -->New  and then enter the fields as mentioned in the snippet below 
Custom Settings Salesforce

Click on Save. This creates a custom object in the background.  If you notice the API name is followed by __c same as in the case of a custom object. 

Step 2: Create a custom checkbox field which would be used to indicate whether the output panel is enable for the profile. Click New in the custom fields section and choose checkbox and click next. Give the field label and  name as 'Enabled' and click next followed by Save.  
It should look the same as shown in snapshot below

Custom Settings Salesforce

Step 3: Click on manage and create records as shown in the image. Create multiple records for all the profiles in your org . The Checkbox field indicates whether the output panel in the VFpage is visible.

Custom settings can be accessed using their own instance methods . getinstance() method can be used to get the record value. The records are accessed using the Name field instead of the id .

 DislayPanel__c disp= DislayPanel__c .getInstance('System Administrator');

To get the list of all records of the custom setting use
list<DisplayPanel__c> ListDisp = DisplayPanel__c.getAll().values();

Now create apex page and controller as shown below: 

Visualforce Page:
<apex:page controller="CustomSettingsDemoExt">

<apex:outputPanel rendered="{!Enable}" >
     This panel is displayed when allowed for the profile in Custom settigs


Controller : 
public with sharing class CustomSettingsDemoExt {
public boolean Enable{get;set;}

    public CustomSettingsDemoExt () {
        Id profileId = UserInfo.getProfileId();
        String profileName = [Select Name from Profile where Id =:profileId].name;  // Get the current user profile Name
        Map<string,DisplayPanel__c> DisplayMap= DisplayPanel__c.getAll();  
        Enable= DisplayMap.get(profileName ).Enabled__c; // display the permission for current profile

Now you can easily toggle the display of this output panel from Custom settings for all profiles instead of altering the code.

About Saurabh Dua


  1. Good explanation with example.

  2. Simple and clear explaination

  3. i want some more integration examples

    1. See post with Label Integration, follow the Facebook page to keep track on upcoming post! Soon will find many simple post explaining Integration scenarios!

  4. Someone Sometimes with visits your blog regularly and recommended it in my experience to read as well. The way of writing is excellent and also the content is top-notch. Thanks for that insight you provide the readers! visit here

  5. I have read your article, it is very informative and helpful for me.I admire the valuable information you offer in your articles. Thanks for posting it.. visa de canadá

  6. Pretty good post. I just stumbled upon your blog and wanted to say that I have really enjoyed reading your blog posts. Any way I'll be subscribing to your feed and I hope you post again soon. Big thanks for the useful info. takardar visa ta Indiya

  7. Positive site, where did u come up with the information on this posting?I have read a few of the articles on your website now, and I really like your style. Thanks a million and please keep up the effective work. new zealand eta

  8. Woh I enjoy your content, saved to bookmarks!!! Turkey transit visa for US citizens... Ordinary passport holders are required to have a visa to enter Turkey. You can get an online visa transit Turkey.You are ready to fill out an application form in the Turkey Visa Pre-Application System.


Powered by Blogger.