Custom Settings in Salesforce | Techila
1257
single,single-post,postid-1257,single-format-standard,ajax_updown_fade,page_not_loaded,,qode_grid_1300,footer_responsive_adv,hide_top_bar_on_mobile_header,qode-content-sidebar-responsive,qode-theme-ver-9.4.2,bridge,wpb-js-composer js-comp-ver-4.12,vc_responsive
 

Custom Settings in Salesforce

settings_big_1x-300x225

Custom Settings in Salesforce

Custom settings was introduced in winter ’10 release and had 2 variations  

1. List and

2. Hierarchy.

These are similar to custom objects, used to store data and are accessible using their own API’s. 

Custom Settings are used to create and manage custom data at the Organisation, 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 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 then 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 can 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

    }

}

Conclusion

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

Courtesy:  http://www.worldofsalesforce.com/2016/05/custom-settings-is-salesforce.html

Author: techila

No Comments

Post A Comment