Saturday, February 14, 2015

Filled Under: , , ,

Using Hierarchical Custom Setting to your Advantage



Salesforce gives you many a construct to make your life easier and present your users a very customized and relevant UI to work on.
To begin my Salesforce post, I will start with such a one - Hierarchical Custom Settings.
The Salesforce Hierarchical custom settings gives you a way to use the Salesforce profile and user variations to make your UI more relevant and useful to a particular user or type of user.
To demonstrate the power of Hierarchical custom settings, let us take up a very simple and basic example.
I have a custom object called Survey in my org. The object records are visible to all users in the org, however, I want only a specific type of user - Recruiters to be able to delete any irrelevant survey.
Even as a recruiter deletes a survey, I have a set of additional logic running behind the screen to do so.
I override my standard Survey detail page with a Visualforce page to begin with, as I want some additional UI customizarions.
To begin with, I remove the standard delete button from my Survey page layout.

I just add the below to my Visualforce page, to display the basic fields on the Survey Detail Page

<apex:page standardController="Survey__c">
  <apex:detail relatedList="false" />
</apex:page>

Now, as any type of user I will only see the 'Edit' and 'Submit for Approval' buttons on the Survey detail page

Now, we go ahead and create a new Hierarchical Custom Setting to be used to make my Visualforce page display a Delete button only for a Recruiter.

We create a new Checkbox field in the Custom settings

With 'Manage' we set the 'canDeleteSurveyRecords' value to true/checked only for a Recruiter

Now, a very important aspect of Hierarchical Settings.Your next thought may come to the point, that your org or any org may have several Profiles and hundreds of users to work with.
Do we set the fields of a custom settings for all the profiles and users ?!
The Answer is NO.
We use the Default Organization Level Value of a Hierarchical custom setting.


In our case we want it to be false for any one else so we leave the check box unchecked.

Now we change our visualforce page, I have added an additonal text just to print the value of the settings for convenience
<apex:page standardController="Survey__c">
  <apex:detail relatedList="false" />
  <apex:form >
  <apex:pageBlock>
  <apex:pageBlockSection >
      <apex:outputText label="Value on Hierarchy Setting" value="{!$Setup.UserAccessSettings__c.canDeleteSurveyRecords__c}"/>
      <apex:commandButton value="Delete" rendered="{!$Setup.UserAccessSettings__c.canDeleteSurveyRecords__c}"/>
  </apex:pageBlockSection>
  </apex:pageBlock>
  </apex:form>
</apex:page>
This does nothing, just makes the commnadbutton 'Delete' rendered based on the value of the Hierarchical custom setting - canDeleteSurveyRecords field.
Now, we login as System admin the page will look like
However, as a Recruiter, I see

So, with intelligent use of Hierarchical Custom settings, you can customize your visualforce pages, with no additional lines of logic in your page.
Similarly, you can branch out your Apex code and and its flow based on a User or a User profile using the very effective Hierarchical Custom settings.

Hope this helps you to design more intelligent and manageable pages and classes.

In my next post, I will be talking about how to make your email templates display your desired information.

3 comments:

  1. Helpful as always. Every post you write produce a massive value to your readers that is the only reason it is so popular and has great authority. I read this content really awesome.I hope this information may change my business carrier.I can remember these things whenever taking the decision.


    Base SAS training in Chennai

    Datawarehousing training in Chennai

    Informatica training in Chennai

    Hadoop training in Chennai

    ReplyDelete

  2. Thanks for sharing valuable information and it is useful for onlineitguru provides the best salesforce Online Training Bangalore

    ReplyDelete
  3. I have read your blog its very attractive and impressive. I like your blog. salesforce Online course

    ReplyDelete