{"id":2396,"date":"2026-10-20T09:00:00","date_gmt":"2026-10-20T03:30:00","guid":{"rendered":"https:\/\/swapnilzambare.com\/blog\/hierarchy-custom-settings-apex\/"},"modified":"2026-11-15T11:30:06","modified_gmt":"2026-11-15T06:00:06","slug":"hierarchy-custom-settings-apex","status":"publish","type":"post","link":"https:\/\/swapnilzambare.com\/blog\/hierarchy-custom-settings-apex\/","title":{"rendered":"Hierarchy Custom Settings in Apex Explained with Examples"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Hierarchy Custom Settings let you store a value once for the whole org, then override it for a profile or even a single user. So you can turn a feature off for everyone, leave it on for the admin team, and read the right answer in Apex with one line and no query.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">This post builds on <a href=\"https:\/\/swapnilzambare.com\/blog\/custom-metadata-types-vs-custom-settings\/\">Custom Metadata Types vs Custom Settings<\/a> and <a href=\"https:\/\/swapnilzambare.com\/blog\/apex-classes-and-objects-constructors\/\">Apex classes<\/a>. We will control fee rules at Bright Path Tutors.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">What are Hierarchy Custom Settings?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Hierarchy Custom Settings are a custom setting type with three levels: organization, profile and user. Salesforce merges them for you, so the most specific value wins. They live in cache, which is why reading them costs no SOQL.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Think of a school dress code. The school sets one rule, a department may change part of it, and one student may have a medical exemption. You always follow the most specific rule that applies to you.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Why Hierarchy Custom Settings Matter<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Hard-coded numbers age badly. A discount cap, a batch size or a debug switch written into Apex needs a deployment every time it changes.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Hierarchy Custom Settings move those values into Setup. Also, because they do not count against SOQL limits, you can read them inside loops and triggers without worry.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">How Hierarchy Custom Settings Work<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">With Hierarchy Custom Settings, Salesforce looks for the most specific record and falls back if it does not find one.<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-large\"><a href=\"https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/hierarchy-custom-settings-apex-diagram-1.png\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"640\" src=\"https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/hierarchy-custom-settings-apex-diagram-1-1024x640.png\" alt=\"Hierarchy Custom Settings diagram showing user, profile and org fallback\" class=\"wp-image-2394\" srcset=\"https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/hierarchy-custom-settings-apex-diagram-1-1024x640.png 1024w, https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/hierarchy-custom-settings-apex-diagram-1-300x188.png 300w, https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/hierarchy-custom-settings-apex-diagram-1-768x480.png 768w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/a><figcaption class=\"wp-element-caption\">How the user, profile and org levels are merged.<\/figcaption><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">Creating the Setting<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">An admin creates it in Setup under Custom Settings, choosing Hierarchy as the type. Each field becomes a value you can override.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>Field<\/th><th>Type<\/th><th>Purpose<\/th><\/tr><\/thead><tbody><tr><td>Max_Discount__c<\/td><td>Percent<\/td><td>Highest discount this user may give<\/td><\/tr><tr><td>Enable_Auto_Email__c<\/td><td>Checkbox<\/td><td>Turns reminder emails on or off<\/td><\/tr><tr><td>Batch_Size__c<\/td><td>Number<\/td><td>Records per batch run<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">Reading the Merged Value<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ Merged for the running user\nFee_Setting__c s = Fee_Setting__c.getInstance(UserInfo.getUserId());\nSystem.debug('Max discount: ' + s.Max_Discount__c);\nSystem.debug('Auto email: ' + s.Enable_Auto_Email__c);<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">With no argument, <code>getInstance()<\/code> also returns the merged value for the current user, which is the common case.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Fee_Setting__c s = Fee_Setting__c.getInstance();<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Reading a Specific Level<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Sometimes you want the org value even when the user has an override.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Fee_Setting__c org = Fee_Setting__c.getOrgDefaults();\nFee_Setting__c forProfile = Fee_Setting__c.getInstance(profileId);\nFee_Setting__c forUser = Fee_Setting__c.getInstance(userId);<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The debug for a user whose profile allows more than the org default looks like this.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Org default: 10\nProfile value: 20\nMerged for user: 20<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Writing a Setting from Apex<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Custom settings are records, so normal DML works.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Fee_Setting__c s = Fee_Setting__c.getOrgDefaults();\nif (s.Id == null) {\n    s = new Fee_Setting__c(SetupOwnerId = UserInfo.getOrganizationId());\n}\ns.Max_Discount__c = 15;\nupsert s;<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><code>SetupOwnerId<\/code> is the field that decides the level. It holds the org Id, a profile Id or a user Id.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Hierarchy Custom Settings Methods<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">This table lists the methods you get for free.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>Method<\/th><th>Returns<\/th><\/tr><\/thead><tbody><tr><td>getInstance()<\/td><td>Merged value for the running user<\/td><\/tr><tr><td>getInstance(userId)<\/td><td>Merged value for that user<\/td><\/tr><tr><td>getInstance(profileId)<\/td><td>Merged value for that profile<\/td><\/tr><tr><td>getOrgDefaults()<\/td><td>The organization level record only<\/td><\/tr><tr><td>getValues(name)<\/td><td>Used by list settings, not hierarchy<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Hierarchy vs List Custom Settings<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Custom settings come in two types. This table compares them.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>Feature<\/th><th>Hierarchy<\/th><th>List<\/th><\/tr><\/thead><tbody><tr><td>Levels<\/td><td>Org, profile, user<\/td><td>One flat list<\/td><\/tr><tr><td>Overrides<\/td><td>Yes<\/td><td>No<\/td><\/tr><tr><td>Lookup by<\/td><td>getInstance<\/td><td>getValues with a name<\/td><\/tr><tr><td>Best for<\/td><td>Switches and limits<\/td><td>Reference data<\/td><\/tr><tr><td>Counts against SOQL<\/td><td>No<\/td><td>No<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<figure class=\"wp-block-image aligncenter size-large\"><a href=\"https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/hierarchy-custom-settings-apex-diagram-2.png\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"640\" src=\"https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/hierarchy-custom-settings-apex-diagram-2-1024x640.png\" alt=\"Hierarchy Custom Settings compared with list custom settings\" class=\"wp-image-2395\" srcset=\"https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/hierarchy-custom-settings-apex-diagram-2-1024x640.png 1024w, https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/hierarchy-custom-settings-apex-diagram-2-300x188.png 300w, https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/hierarchy-custom-settings-apex-diagram-2-768x480.png 768w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/a><figcaption class=\"wp-element-caption\">Hierarchy compared with list custom settings.<\/figcaption><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">In short, use hierarchy when the answer depends on who is running the code. Use list settings for shared reference data.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">A Real Hierarchy Custom Settings Example<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Bright Path lets counsellors give a discount, but the cap differs. Normal staff may give 10%, team leads 20%, and one senior counsellor is allowed 25%.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Step 1: Set the Values<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">An admin enters three records in Setup.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>Level<\/th><th>Who<\/th><th>Max Discount<\/th><\/tr><\/thead><tbody><tr><td>Organization<\/td><td>Everyone<\/td><td>10<\/td><\/tr><tr><td>Profile<\/td><td>Team Lead<\/td><td>20<\/td><\/tr><tr><td>User<\/td><td>Ravi Kumar<\/td><td>25<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">Step 2: Enforce It in Apex<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>public with sharing class DiscountGuard {\n    public static Boolean isAllowed(Decimal requested) {\n        Fee_Setting__c s = Fee_Setting__c.getInstance();\n        Decimal cap = (s != null &amp;&amp; s.Max_Discount__c != null)\n                      ? s.Max_Discount__c : 0;\n        return requested &lt;= cap;\n    }\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The null check matters. If no record exists at any level, <code>getInstance<\/code> returns an empty object rather than null, and the fields are null.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Step 3: Use It in a Trigger<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>for (Enrolment__c e : Trigger.new) {\n    if (e.Discount_Percent__c != null &amp;&amp; !DiscountGuard.isAllowed(e.Discount_Percent__c)) {\n        e.addError('Discount above your allowed limit.');\n    }\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Because the read costs no SOQL, calling it inside the loop is safe. Even so, reading it once before the loop is tidier.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Step 4: Check the Result<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>System.debug(DiscountGuard.isAllowed(8));\nSystem.debug(DiscountGuard.isAllowed(22));<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Running as Ravi, who is capped at 25, both come back true.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>true\ntrue<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Running as a normal counsellor, the second one is false.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Common Mistakes with Hierarchy Custom Settings<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Assuming getInstance returns null when nothing is set, instead of an empty record.<\/li>\n\n<li>Forgetting SetupOwnerId when creating a record in Apex.<\/li>\n\n<li>Storing secrets such as API keys, which are visible to anyone who can read the setting.<\/li>\n\n<li>Using a custom setting where Custom Metadata would deploy more cleanly.<\/li>\n\n<li>Creating a user level record for every user, which becomes hard to maintain.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Best Practices for Hierarchy Custom Settings<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Always set an organization default so there is a safe fallback.<\/li>\n\n<li>Keep overrides rare, usually at profile level rather than user level.<\/li>\n\n<li>Null check every field before you use it.<\/li>\n\n<li>Read the setting once per transaction and pass it around.<\/li>\n\n<li>Use Custom Metadata when the value should move between orgs with your code.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Practice Task<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Create a hierarchy setting with a checkbox called Enable_Debug__c. Set it off at org level and on for your own user. Then write a method that returns its value. Hint: use getInstance with no arguments.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Most Asked Hierarchy Custom Settings Interview Questions<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">These Hierarchy Custom Settings questions test whether you know how configuration should be stored.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Basic Hierarchy Custom Settings Interview Questions<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q1: What are hierarchy custom settings?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Hierarchy Custom Settings store values at org, profile and user level, and Salesforce returns the most specific match.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q2: What are the two types of custom setting?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Hierarchy Custom Settings and list settings. Hierarchy supports overrides, while list settings are a flat set of named records.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q3: Which method returns the merged value?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">getInstance, either with no argument for the running user or with a user or profile Id.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q4: What does getOrgDefaults return?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Only the organization level record, ignoring any profile or user override.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q5: Do custom settings count against SOQL limits?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">No. They are held in cache, so reading them is free.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q6: What field decides the level of a record?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">SetupOwnerId, which holds an org, profile or user Id.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q7: Can you update a custom setting in Apex?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Yes. They are records, so insert, update and upsert all work.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q8: What does getInstance return when nothing is set?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">An empty record with null fields, not null itself.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Intermediate Hierarchy Custom Settings Interview Questions<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q9: What is the order of precedence?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">User beats profile, and profile beats the organization default.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q10: Why read the setting once per transaction?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Although reads are cheap, repeated calls still cost CPU time. One read and a local variable is cleaner.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q11: Can hierarchy settings be deployed with data?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The definition deploys, but the records do not. So you recreate values per org or load them with a script.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q12: How is Custom Metadata different?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Custom Metadata records deploy with your code and can be referenced in formulas and validation rules. However, it has no user level override.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q13: Should you store API keys in a custom setting?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">No. Use Named Credentials or protected Custom Metadata, since custom settings are readable by many users.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q14: Can a formula field reference a hierarchy setting?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Yes. Formulas and validation rules can reference custom setting fields, which is a common reason to choose them.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q15: What happens when a user has no profile override?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The merged value falls back to the organization default automatically.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q16: Are custom settings available in a Flow?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Yes. Flow can reference custom setting values directly in formulas and decisions.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Advanced Hierarchy Custom Settings Interview Questions<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q17: Scenario: a feature must be off in production but on for testers. How?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Set the checkbox off at org level, then add user level records for the testers. No deployment is needed to change it later.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q18: What are the storage limits?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Custom settings share a cached data limit per org, often a few megabytes. So keep them small and avoid large lists.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q19: Scenario: a setting is missing in a new sandbox. What breaks?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Any code that assumes a value. Therefore always null check and provide a code level default.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q20: When would you choose Custom Metadata instead?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">When values must travel with the deployment, be packaged, or be referenced across orgs without manual setup.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q21: How do you test code that reads a custom setting?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Insert the setting record in the test with the right SetupOwnerId, so the test does not depend on org data.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q22: Why is a user level record per user a bad pattern?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Because maintenance grows with headcount. Profile level overrides scale far better.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q23: Can custom settings be protected in a package?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Yes. A protected setting is visible only to code in the package, which suits internal switches.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q24: How do hierarchy settings behave for guest users?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">They fall back to the guest user profile or the org default, so set a safe org value.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q25: Are Hierarchy Custom Settings cached per user?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The cache is per org, but the merged result depends on who is running. So two users can read different values from the same setting.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q26: Can a trigger write to Hierarchy Custom Settings?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Yes, since they are records and normal DML applies. However, writing on every save adds DML, so do it sparingly.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q27: Do Hierarchy Custom Settings respect field level security?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Setup access controls who can edit them, and code reading them runs in system context. Therefore do not rely on them to hide sensitive values.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q28: Scenario: a batch size must differ per environment. Which tool?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Hierarchy Custom Settings work, since the value stays per org. Alternatively Custom Metadata with an environment field also fits.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q29: How do you migrate Hierarchy Custom Settings data to production?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Export the records and load them with Data Loader, or create them in a post-deployment script, because the data does not deploy.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Official Resources<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/developer.salesforce.com\/docs\/atlas.en-us.apexcode.meta\/apexcode\/apex_methods_system_custom_settings.htm\" target=\"_blank\" rel=\"noopener noreferrer\">Apex Developer Guide: Custom Settings<\/a><\/li>\n\n<li><a href=\"https:\/\/help.salesforce.com\/s\/articleView?id=sf.cs_about.htm&amp;type=5\" target=\"_blank\" rel=\"noopener noreferrer\">Salesforce Help: Custom Settings<\/a><\/li>\n\n<li><a href=\"https:\/\/help.salesforce.com\/s\/articleView?id=sf.custommetadatatypes_overview.htm&amp;type=5\" target=\"_blank\" rel=\"noopener noreferrer\">Salesforce Help: Custom Metadata Types<\/a><\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Quick Recap<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Hierarchy Custom Settings merge org, profile and user values.<\/li>\n\n<li>getInstance returns the most specific match for the running user.<\/li>\n\n<li>Reads cost no SOQL, so they are safe inside loops.<\/li>\n\n<li>getInstance never returns null, so null check the fields.<\/li>\n\n<li>Use Custom Metadata when values must deploy with your code.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">What&#8217;s Next?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Next, we protect data in code with <a href=\"https:\/\/swapnilzambare.com\/blog\/apex-crypto-encoding\/\">Apex Crypto and Encoding<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hierarchy Custom Settings let you store a value once for the whole org, then override it for a profile or even a single user.&hellip;<\/p>\n","protected":false},"author":1,"featured_media":2393,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_swapz_level":"","_swapz_series_part":0,"_swapz_hide_toc":false,"_swapz_hide_featured":false,"_swapz_description":"","footnotes":""},"categories":[8,96,244],"tags":[98,34,78,26],"swapz_series":[],"class_list":["post-2396","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-salesforce","category-salesforce-developer","category-security-design-patterns","tag-apex","tag-certification","tag-custom-metadata","tag-intermediate"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v28.9 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Hierarchy Custom Settings in Apex Explained<\/title>\n<meta name=\"description\" content=\"Learn Hierarchy Custom Settings in Apex: getInstance, getOrgDefaults, user and profile overrides, and the most asked developer interview questions.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/swapnilzambare.com\/blog\/hierarchy-custom-settings-apex\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Hierarchy Custom Settings in Apex Explained\" \/>\n<meta property=\"og:description\" content=\"Learn Hierarchy Custom Settings in Apex: getInstance, getOrgDefaults, user and profile overrides, and the most asked developer interview questions.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/swapnilzambare.com\/blog\/hierarchy-custom-settings-apex\/\" \/>\n<meta property=\"og:site_name\" content=\"Swapnil Zambare - Blog\" \/>\n<meta property=\"article:published_time\" content=\"2026-10-20T03:30:00+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2026-11-15T06:00:06+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/hierarchy-custom-settings-apex-featured.png\" \/>\n\t<meta property=\"og:image:width\" content=\"2400\" \/>\n\t<meta property=\"og:image:height\" content=\"1260\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"Swapnil Zambare\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Swapnil Zambare\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"9 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/hierarchy-custom-settings-apex\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/hierarchy-custom-settings-apex\\\/\"},\"author\":{\"name\":\"Swapnil Zambare\",\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/#\\\/schema\\\/person\\\/10b09224c8bcc562716e86789427592a\"},\"headline\":\"Hierarchy Custom Settings in Apex Explained with Examples\",\"datePublished\":\"2026-10-20T03:30:00+00:00\",\"dateModified\":\"2026-11-15T06:00:06+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/hierarchy-custom-settings-apex\\\/\"},\"wordCount\":1678,\"publisher\":{\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/#organization\"},\"image\":{\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/hierarchy-custom-settings-apex\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/wp-content\\\/uploads\\\/2026\\\/11\\\/hierarchy-custom-settings-apex-featured.png\",\"keywords\":[\"Apex\",\"Certification\",\"Custom Metadata\",\"Intermediate\"],\"articleSection\":[\"Salesforce\",\"Salesforce Developer\",\"Security and Design Patterns\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/hierarchy-custom-settings-apex\\\/\",\"url\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/hierarchy-custom-settings-apex\\\/\",\"name\":\"Hierarchy Custom Settings in Apex Explained\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/hierarchy-custom-settings-apex\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/hierarchy-custom-settings-apex\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/wp-content\\\/uploads\\\/2026\\\/11\\\/hierarchy-custom-settings-apex-featured.png\",\"datePublished\":\"2026-10-20T03:30:00+00:00\",\"dateModified\":\"2026-11-15T06:00:06+00:00\",\"description\":\"Learn Hierarchy Custom Settings in Apex: getInstance, getOrgDefaults, user and profile overrides, and the most asked developer interview questions.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/hierarchy-custom-settings-apex\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/hierarchy-custom-settings-apex\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/hierarchy-custom-settings-apex\\\/#primaryimage\",\"url\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/wp-content\\\/uploads\\\/2026\\\/11\\\/hierarchy-custom-settings-apex-featured.png\",\"contentUrl\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/wp-content\\\/uploads\\\/2026\\\/11\\\/hierarchy-custom-settings-apex-featured.png\",\"width\":2400,\"height\":1260,\"caption\":\"Hierarchy Custom Settings featured image: code editor card with getInstance call\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/hierarchy-custom-settings-apex\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Hierarchy Custom Settings in Apex Explained with Examples\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/\",\"name\":\"Swapnil Zambare - Blog\",\"description\":\"Salesforce &amp; Tech Guides\",\"publisher\":{\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/#organization\",\"name\":\"Swapnil Zambare - Blog\",\"url\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/wp-content\\\/uploads\\\/2026\\\/09\\\/site-icon-512.png\",\"contentUrl\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/wp-content\\\/uploads\\\/2026\\\/09\\\/site-icon-512.png\",\"width\":512,\"height\":512,\"caption\":\"Swapnil Zambare - Blog\"},\"image\":{\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/#\\\/schema\\\/logo\\\/image\\\/\"},\"sameAs\":[\"https:\\\/\\\/www.linkedin.com\\\/in\\\/theSwapnilZambare\"]},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/#\\\/schema\\\/person\\\/10b09224c8bcc562716e86789427592a\",\"name\":\"Swapnil Zambare\",\"sameAs\":[\"https:\\\/\\\/swapnilzambare.com\\\/blog\"],\"url\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/author\\\/swapnil-zambare\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Hierarchy Custom Settings in Apex Explained","description":"Learn Hierarchy Custom Settings in Apex: getInstance, getOrgDefaults, user and profile overrides, and the most asked developer interview questions.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/swapnilzambare.com\/blog\/hierarchy-custom-settings-apex\/","og_locale":"en_US","og_type":"article","og_title":"Hierarchy Custom Settings in Apex Explained","og_description":"Learn Hierarchy Custom Settings in Apex: getInstance, getOrgDefaults, user and profile overrides, and the most asked developer interview questions.","og_url":"https:\/\/swapnilzambare.com\/blog\/hierarchy-custom-settings-apex\/","og_site_name":"Swapnil Zambare - Blog","article_published_time":"2026-10-20T03:30:00+00:00","article_modified_time":"2026-11-15T06:00:06+00:00","og_image":[{"width":2400,"height":1260,"url":"https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/hierarchy-custom-settings-apex-featured.png","type":"image\/png"}],"author":"Swapnil Zambare","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Swapnil Zambare","Est. reading time":"9 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/swapnilzambare.com\/blog\/hierarchy-custom-settings-apex\/#article","isPartOf":{"@id":"https:\/\/swapnilzambare.com\/blog\/hierarchy-custom-settings-apex\/"},"author":{"name":"Swapnil Zambare","@id":"https:\/\/swapnilzambare.com\/blog\/#\/schema\/person\/10b09224c8bcc562716e86789427592a"},"headline":"Hierarchy Custom Settings in Apex Explained with Examples","datePublished":"2026-10-20T03:30:00+00:00","dateModified":"2026-11-15T06:00:06+00:00","mainEntityOfPage":{"@id":"https:\/\/swapnilzambare.com\/blog\/hierarchy-custom-settings-apex\/"},"wordCount":1678,"publisher":{"@id":"https:\/\/swapnilzambare.com\/blog\/#organization"},"image":{"@id":"https:\/\/swapnilzambare.com\/blog\/hierarchy-custom-settings-apex\/#primaryimage"},"thumbnailUrl":"https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/hierarchy-custom-settings-apex-featured.png","keywords":["Apex","Certification","Custom Metadata","Intermediate"],"articleSection":["Salesforce","Salesforce Developer","Security and Design Patterns"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/swapnilzambare.com\/blog\/hierarchy-custom-settings-apex\/","url":"https:\/\/swapnilzambare.com\/blog\/hierarchy-custom-settings-apex\/","name":"Hierarchy Custom Settings in Apex Explained","isPartOf":{"@id":"https:\/\/swapnilzambare.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/swapnilzambare.com\/blog\/hierarchy-custom-settings-apex\/#primaryimage"},"image":{"@id":"https:\/\/swapnilzambare.com\/blog\/hierarchy-custom-settings-apex\/#primaryimage"},"thumbnailUrl":"https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/hierarchy-custom-settings-apex-featured.png","datePublished":"2026-10-20T03:30:00+00:00","dateModified":"2026-11-15T06:00:06+00:00","description":"Learn Hierarchy Custom Settings in Apex: getInstance, getOrgDefaults, user and profile overrides, and the most asked developer interview questions.","breadcrumb":{"@id":"https:\/\/swapnilzambare.com\/blog\/hierarchy-custom-settings-apex\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/swapnilzambare.com\/blog\/hierarchy-custom-settings-apex\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/swapnilzambare.com\/blog\/hierarchy-custom-settings-apex\/#primaryimage","url":"https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/hierarchy-custom-settings-apex-featured.png","contentUrl":"https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/hierarchy-custom-settings-apex-featured.png","width":2400,"height":1260,"caption":"Hierarchy Custom Settings featured image: code editor card with getInstance call"},{"@type":"BreadcrumbList","@id":"https:\/\/swapnilzambare.com\/blog\/hierarchy-custom-settings-apex\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/swapnilzambare.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Hierarchy Custom Settings in Apex Explained with Examples"}]},{"@type":"WebSite","@id":"https:\/\/swapnilzambare.com\/blog\/#website","url":"https:\/\/swapnilzambare.com\/blog\/","name":"Swapnil Zambare - Blog","description":"Salesforce &amp; Tech Guides","publisher":{"@id":"https:\/\/swapnilzambare.com\/blog\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/swapnilzambare.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/swapnilzambare.com\/blog\/#organization","name":"Swapnil Zambare - Blog","url":"https:\/\/swapnilzambare.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/swapnilzambare.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/09\/site-icon-512.png","contentUrl":"https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/09\/site-icon-512.png","width":512,"height":512,"caption":"Swapnil Zambare - Blog"},"image":{"@id":"https:\/\/swapnilzambare.com\/blog\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.linkedin.com\/in\/theSwapnilZambare"]},{"@type":"Person","@id":"https:\/\/swapnilzambare.com\/blog\/#\/schema\/person\/10b09224c8bcc562716e86789427592a","name":"Swapnil Zambare","sameAs":["https:\/\/swapnilzambare.com\/blog"],"url":"https:\/\/swapnilzambare.com\/blog\/author\/swapnil-zambare\/"}]}},"_links":{"self":[{"href":"https:\/\/swapnilzambare.com\/blog\/wp-json\/wp\/v2\/posts\/2396","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/swapnilzambare.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/swapnilzambare.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/swapnilzambare.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/swapnilzambare.com\/blog\/wp-json\/wp\/v2\/comments?post=2396"}],"version-history":[{"count":1,"href":"https:\/\/swapnilzambare.com\/blog\/wp-json\/wp\/v2\/posts\/2396\/revisions"}],"predecessor-version":[{"id":3054,"href":"https:\/\/swapnilzambare.com\/blog\/wp-json\/wp\/v2\/posts\/2396\/revisions\/3054"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/swapnilzambare.com\/blog\/wp-json\/wp\/v2\/media\/2393"}],"wp:attachment":[{"href":"https:\/\/swapnilzambare.com\/blog\/wp-json\/wp\/v2\/media?parent=2396"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/swapnilzambare.com\/blog\/wp-json\/wp\/v2\/categories?post=2396"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/swapnilzambare.com\/blog\/wp-json\/wp\/v2\/tags?post=2396"},{"taxonomy":"swapz_series","embeddable":true,"href":"https:\/\/swapnilzambare.com\/blog\/wp-json\/wp\/v2\/swapz_series?post=2396"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}