Share Your Experience With Others

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