Salesforce Hierarchy Custom Settings : Fair Use

 

Hello Trailblazers,

 

Today I am going to discuss about one functionality of Salesforce that everyone knows but still very few use it in correct way. We have been using Custom Settings for storing static values for global use and avoid hard coding, but I believe most of us use List type Custom Settings only. I am going to explain Hierarchy Custom setting and it’s actual/appropriate use in this blog.

So first let us quickly understand what custom settings are – Custom Settings are just like Custom Objects in Salesforce which enable us to create custom sets of data that can be accessed in formula fields, validation rules, apex, flows & SOAP API without consuming an extra query for retrieval. There are two types of custom settings: List & Hierarchy.


What is Hierarchy Custom Setting?

Hierarchy Custom Settings does the same job done by List Custom Settings but with an additional power of managing data as per built-in hierarchical logic that let’s you decide what data to return for specific profile or user. We can store data for Organization level, Profile level and User level using Hierarchy custom settings and as per our requirements we can retrieve these settings.

 Note : In the hierarchy, settings for an organization are overridden by profile settings, which, in turn, are overridden by user settings.

 So, If we have created one field in hierarchy custom settings then we can have different value for a specific user, different value for specific profile and different value for rest of the entities of Org.


How to create Hierarchy Custom Setting? 

  • Go to Setup and type custom setting in Quick find box. Open Custom Setting page.
  • Click on New and enter Label. Select setting type as Hierarchy. Click on Save.

  • Now we will create on field. Click on New button next to Custom fields. Create new field with below details.
    • Type : Currency
    • Label : Threshold Amount
    • Keep rest of the fields as it is.

How to create data for Hierarchy Custom Setting ?

Now let’s create data for different hierarchy. Click on manage for Hierarchy Setting definition page. You will land on below screen.


Here in section 1, you can define values for organization level by clicking on New button.

Location defines level for which this value will be applicable, since we are creating setting for Org level it is shown as Blank here. Upon save it will display your Organization Name.

In section 2, we can create data for specific user or profile. We have to select Location as Profile or User here.


Finally your screen will look like below :

How to use Hierarchy Custom Setting ? 

To access custom setting in formula fields & validation rules we use below syntax : {!$Setup.Setting__c.Field__c}

This will return value resolving current user context.

We can use  below methods along with custom setting name to get values in apex (Hierarchy_Demo__c.METHOD_NAME), and then access using field API Name.


getInstance() – This will return data set record as per current User. So if current logged in User is Manish Khade then it will return data set record with Threshold Amount value as 30,000 and for other users it will return set value with Threshold Amount value as 20,000. Here also lowest level will be considered as per hierarchy, which mean if setting is maintained for current user then it’ll return those values, if not then it will check if data is there for user’s profile and if not then it will return Organization level data.
Example :   Hierarchy_Demo__c hd =  Hierarchy_Demo__c. getInstance ();
                    System.debug(hd.Threshold_Amount__c); 

getOrgDefaults() – This will return custom setting data set record for the organization. This will not follow any hierarchy logic, it will return Organization level values.
Example : Hierarchy_Demo__c hd =  Hierarchy_Demo__c. getOrgDefaults ();
                  System.debug(hd.Threshold_Amount__c);


getInstance(userId) - This will return data set record as per User Id mentioned in parameter. If you specify user id for which setting is not maintained, then Organization level value set will be returned.
Example : Hierarchy_Demo__c hd =  Hierarchy_Demo__c. getInstance (‘USER_ID’);
                  System.debug(hd.Threshold_Amount__c);


getValues(userId) – This will return data set record as per User Id mentioned in parameter. If setting for specified user id is not maintained, then null will be returned.
Example : Hierarchy_Demo__c hd =  Hierarchy_Demo__c. getValues (‘USER_ID’);
                  System.debug(hd.Threshold_Amount__c);


getInstance(profileId) - This will return record set as per Profile Id mentioned in parameter. If you specify profile id for which setting is not maintained, then Organization level value set will be returned.
Example : Hierarchy_Demo__c hd =  Hierarchy_Demo__c. getInstance (‘PROFILE_ID);
                  System.debug(hd.Threshold_Amount__c);


getValues(profileId) – This will return data set record as per Profile Id mentioned in parameter. If setting for specified Profile id is not maintained, then null will be returned.
Example : Hierarchy_Demo__c hd =  Hierarchy_Demo__c. getValues (‘PROFILE_ID);
                  System.debug(hd.Threshold_Amount__c);

I hope this will help many of us to understand correct use of Hierarchy Custom Settings. 

Reference Links : 

Apex Developer Guide - Custom Settings

Comments

Popular posts from this blog

Salesforce Flow - Create multiple records from user input

Scan barcodes from Salesforce Mobile App

Zip or Unzip Files in Salesforce