Configuration

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 Force.com 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
</apex:outputPanel>

</apex:page>

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

4 comments:

  1. Good explanation with example.

    ReplyDelete
  2. i want some more integration examples

    ReplyDelete
    Replies
    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!

      Delete

Powered by Blogger.