Utilization of Hierarchy custom settings in formula field, custom button, process builder or workflow rules without hard-coding

Utilization of Hierarchy custom settings in formula field, custom button, process builder or workflow rules without hard-coding

Custom settings

Custom settings are similar to custom objects. Developers can create custom sets of data and associate custom data for an organization, profile, or specific user. All custom settings data is exposed in the application cache, which enables efficient access without the cost of repeated queries to the database. Formula fields, validation rules, flows, Apex, and the SOAP API can then use this data. The most important thing about Custom Setting is, it does not uses our salesforce data storage and resides in the” application cache”.

Types of custom settings

1)List Custom Setting
2)Hierarchy Custom Settings

List Custom Setting:
It provides a reusable set of static data that can be accessed across your organization. If we are using some set of data frequently within our application, putting that data in a list custom setting streamlines access to it. Data in list settings does not vary with profile or user but is available organization-wide(Static data).

Note: To enable the List custom setting we have to first enable them in the schema settings(Setup–>Schema settings).
Example: Storing Zip codes of all the areas with respect to a particular city.

Dataset name
Zip Code Field value
Florence
90001
Watts
90002
South Central
90003

Hierarchy Custom Setting

It uses a built-in hierarchical logic that lets you “personalize” settings for specific profiles or users. The hierarchy logic checks the organization, profile, and user settings for the current user and returns the most specific, or “lowest,” value. Instead of hardcoding the values we can define them for each of the levels and maintain the data accordingly.

As with list custom settings, when you create a new hierarchy custom setting, the platform creates a custom object in the background for you (notice the API Name field). You then add additional fields to the custom setting in the same manner that you do for custom objects (you are limited to checkbox, currency, date, date/time, email, number, percent, phone, text, text area, and URL fields).

Example: We might set up an org-wide custom setting of a 5% discount that everyone is authorized to offer. If we have a high-producing sales people that are in their own profile and are able to offer a 10% discount,also if there is one salesperson in that same profile that has lobbied the VP of Sales to be able to offer a 20% discount then with the hierarchy custom settings one can be able to manage all of the scenarios.

How does it work?
1)User level———>2)profile level——–>3)Organization level.
If the user level value is defined, then it will take the value defined for that particular user else it will take if the profile level value is defined.If the logged in user does not have any value defined at the user level or profile level, therefore Organization level value would be considered.

The following generic syntax can be used to access Hierarchy Custom Settings in formula field, Validation Rule, Workflow, Process builder and VF page.

Syntax: $Setup.CustomSettingName__c.FieldName__c

Steps to create  Hierarchy custom settings:-

Step-1: Navigate to Setup | Custom Settings | New.

Step-2: Click on new and create a custom setting object specifying the label, object name, setting type and visibility.

Step-3: Create a custom field for the object created, specifying the data type and the label for it.

Step-4: Click on “manage” for setting the values based on the levels(user or profile).Specify the values accordingly for the user level and the profile level.

Step-5: Click on the new button to set the organization default value.

How to access these values in Formula field, Workflow, Process builder ?

Using Hierarchy custom setting in formula field: 
Using Hierarchy custom setting in Workflow Action -Field Update:
Process builder:
Using Hierarchy custom setting in Custom Button:
JavaScript  used in custom button: alert("Your Discount is:"+{!$Setup.Discount__c.Percentage_Allowed__c * 100}+"%")

Can we query the fields of the custom setting object?

The answer is “Yes”.
Query syntax: Select id,name,field_custobjField__c from Custom_settingObj__c
Ex: Select id,name,Percentage_Allowed__c from discount__c

How to get the hierarchy custom setting value in apex?

The answer is by using the “Custom settings methods ”.
Custom settings methods are all instance methods, that is, they are called by and operate on a particular instance of a custom setting.
In our Example, we have an object with API name as “Discount__c” and field name as “Percentage_Allowed__c”.

To get the org default value in Apex:
Discount__c d =  Discount__c.getOrgDefaults();
decimal discountValue= d.Percentage_Allowed__c;
system.debug(‘value is’ +discountValue);

To get the Specified profile  value in Apex :
string pid=’0056F000008ujg9QAA’;
Discount__c d =Discount__c.getInstance(pid);
decimal discountValue= d.Percentage_Allowed__c;
system.debug(‘value is ‘+discountValue);

To get the current profile’s value in Apex :
Discount__c d =Discount__c.getInstance(UserInfo.getProfileId());
decimal discountValue= d.Percentage_Allowed__c;
system.debug(‘value is’ +discountValue);

To get the User specified value
in Apex :
string pid=’00e6F000002zYfJQAU’;
Discount__c d =  Discount__c.getInstance(pid);
decimal discountValue= d.Percentage_Allowed__c;
system.debug(‘value is’ +discountValue);

To get the  Current User’s value in Apex :
Discount__c d =  Discount__c.getInstance(UserInfo.getUserId());
getInstance(UserInfo.getProfileId());
decimal discountValue= d.Percentage_Allowed__c;
system.debug(‘value is’ +discountValue);

Leave a Comment

Your email address will not be published. Required fields are marked *

Recent Posts

prover automation tool a guide to salesforce automation
Provar Automation Tool: A Guide to Salesforce Automation
salesforce experience cloud features
Salesforce Experience Cloud Features & Its Benefits
why should you choose salesforce revenue cloud and cpq
Why Should You Choose Salesforce Revenue Cloud & CPQ!
5 most important salesforce marketing cloud features
5 Most Important Salesforce Marketing Cloud Features
absyz your trusted salesforce managed services provider
ABSYZ: Your Trusted Salesforce Managed Services Provider
Scroll to Top