{"id":1764,"date":"2026-10-04T09:00:00","date_gmt":"2026-10-04T03:30:00","guid":{"rendered":"https:\/\/swapnilzambare.com\/blog\/custom-metadata-in-apex\/"},"modified":"2026-11-15T11:31:19","modified_gmt":"2026-11-15T06:01:19","slug":"custom-metadata-in-apex","status":"publish","type":"post","link":"https:\/\/swapnilzambare.com\/blog\/custom-metadata-in-apex\/","title":{"rendered":"Custom Metadata in Apex: Build Config-Driven Code"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Custom Metadata in Apex lets your code read rules and settings that admins manage in Setup, so business changes do not need a code deployment. Instead of hard-coding a 10% discount or an API path, the code reads it from a custom metadata type record. Because these records are metadata, they also move between orgs with your deployments.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In this post, you will read custom metadata with getAll, getInstance and SOQL, build a config-driven discount rule, and test it. It builds on the admin guide to <a href=\"https:\/\/swapnilzambare.com\/blog\/custom-metadata-types-vs-custom-settings\/\">custom metadata types vs custom settings<\/a>, <a href=\"https:\/\/swapnilzambare.com\/blog\/apex-design-patterns\/\">Apex Design Patterns<\/a> and <a href=\"https:\/\/swapnilzambare.com\/blog\/apex-governor-limits-explained\/\">Apex governor limits<\/a>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">What is Custom Metadata in Apex?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">A custom metadata type is like a custom object, but its records are metadata, and its API name ends in __mdt. Custom Metadata in Apex means reading those records from code, using methods such as Discount_Rule__mdt.getAll() or a SOQL query. Admins can change the values in Setup, while developers keep the logic in Apex.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Think of a restaurant menu board. The kitchen&#8217;s recipes stay the same, but the manager can change prices on the board any day. Custom metadata is that board, and Apex is the kitchen that reads it before cooking.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Why Custom Metadata in Apex Matters<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Hard-coded values cause deployments for every small change. Here is why Custom Metadata in Apex matters at Bright Path Tutors.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>First, discount percentages change every term.<\/li>\n\n<li>Also, each course level has a different maximum class size.<\/li>\n\n<li>Next, the LearnHub API path differs between test and live systems.<\/li>\n\n<li>Feature switches let the team turn new logic on or off safely.<\/li>\n\n<li>Finally, the records deploy with the code, so sandboxes match production.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">So with Custom Metadata in Apex, admins own the values, and developers own the logic.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">How Custom Metadata in Apex Works<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Using Custom Metadata in Apex follows a few steps. First, an admin or developer creates a custom metadata type with fields, such as Level__c and Percent__c. Next, they add records in Setup or in source files. Then Apex reads them with getAll(), getInstance() or SOQL. After that, the code applies the values, such as a discount rate. Finally, the records deploy with metadata, so every org gets the same settings.<\/p>\n\n\n<figure class=\"wp-block-image aligncenter size-large\"><a href=\"https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/custom-metadata-in-apex-diagram-1.png\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"640\" src=\"https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/custom-metadata-in-apex-diagram-1-1024x640.png\" alt=\"Custom Metadata in Apex diagram showing create type, add records, read, apply and deploy\" class=\"wp-image-1762\" srcset=\"https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/custom-metadata-in-apex-diagram-1-1024x640.png 1024w, https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/custom-metadata-in-apex-diagram-1-300x188.png 300w, https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/custom-metadata-in-apex-diagram-1-768x480.png 768w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/a><figcaption class=\"wp-element-caption\">How custom metadata drives Apex logic.<\/figcaption><\/figure>\n\n\n<h3 class=\"wp-block-heading\">getAll()<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">getAll() returns a map of all records, keyed by DeveloperName. It needs no SOQL, so it does not use your query limit.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">getInstance()<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">getInstance(&#8216;Beginner&#8217;) returns one record by DeveloperName or Id. It is the quickest way to read one setting.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">SOQL on __mdt<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">You can query __mdt types with normal SOQL. Use it when you need filters, relationships or full long text values.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Long Text Fields<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">getAll() and getInstance() return only the first 255 characters of long text area fields. So query with SOQL when you need the full text.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Changing Records from Apex<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Apex cannot insert or update __mdt records with normal DML. Instead, the Metadata API classes deploy changes asynchronously.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Ways to Read Custom Metadata<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The table below compares the three ways to read records in Apex.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>Method<\/th><th>Returns<\/th><th>Best for<\/th><\/tr><\/thead><tbody><tr><td>getAll()<\/td><td>Map of all records<\/td><td>Small rule tables<\/td><\/tr><tr><td>getInstance(name)<\/td><td>One record<\/td><td>A single setting<\/td><\/tr><tr><td>SOQL query<\/td><td>Filtered list<\/td><td>Filters and full long text<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Custom Metadata Types vs Custom Settings<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The table below compares custom metadata types with list custom settings for configuration.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>Feature<\/th><th>Custom metadata types<\/th><th>Custom settings<\/th><\/tr><\/thead><tbody><tr><td>Records deploy with metadata<\/td><td>Yes<\/td><td>No, data must be loaded<\/td><\/tr><tr><td>Relationship fields<\/td><td>Yes, metadata relationships<\/td><td>No<\/td><\/tr><tr><td>Change with DML in Apex<\/td><td>No<\/td><td>Yes<\/td><\/tr><tr><td>Packageable records<\/td><td>Yes<\/td><td>No<\/td><\/tr><tr><td>Best for<\/td><td>App configuration<\/td><td>Per-user or runtime values<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">When to use which is clear. Use custom metadata for configuration that should deploy. Use custom settings only for values that code must change at runtime.<\/p>\n\n\n<figure class=\"wp-block-image aligncenter size-large\"><a href=\"https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/custom-metadata-in-apex-diagram-2.png\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"640\" src=\"https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/custom-metadata-in-apex-diagram-2-1024x640.png\" alt=\"Custom Metadata in Apex diagram comparing custom metadata types and custom settings\" class=\"wp-image-1763\" srcset=\"https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/custom-metadata-in-apex-diagram-2-1024x640.png 1024w, https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/custom-metadata-in-apex-diagram-2-300x188.png 300w, https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/custom-metadata-in-apex-diagram-2-768x480.png 768w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/a><figcaption class=\"wp-element-caption\">Custom metadata types vs custom settings.<\/figcaption><\/figure>\n\n\n<h2 class=\"wp-block-heading\">A Real Custom Metadata in Apex Example<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Let&#8217;s build Custom Metadata in Apex at Bright Path Tutors. Discounts per course level live in Discount_Rule__mdt, with fields Level__c, Percent__c and Active__c.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Step 1: The Records in Setup<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The table below shows the records an admin creates in Setup &gt; Custom Metadata Types &gt; Discount Rule &gt; Manage Records.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>Label<\/th><th>Level__c<\/th><th>Percent__c<\/th><th>Active__c<\/th><\/tr><\/thead><tbody><tr><td>Beginner<\/td><td>Beginner<\/td><td>10<\/td><td>true<\/td><\/tr><tr><td>Intermediate<\/td><td>Intermediate<\/td><td>5<\/td><td>true<\/td><\/tr><tr><td>Advanced<\/td><td>Advanced<\/td><td>0<\/td><td>false<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">Step 2: Read the Rules in Apex<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>public with sharing class DiscountRules {\n    private static Map&lt;String, Decimal&gt; percentByLevel;\n\n    public static Decimal percentFor(String level) {\n        if (percentByLevel == null) {\n            percentByLevel = new Map&lt;String, Decimal&gt;();\n            for (Discount_Rule__mdt r : Discount_Rule__mdt.getAll().values()) {\n                if (r.Active__c) {\n                    percentByLevel.put(r.Level__c, r.Percent__c);\n                }\n            }\n        }\n        Decimal p = percentByLevel.get(level);\n        return p == null ? 0 : p;\n    }\n\n    public static Decimal discountedFee(Course__c c) {\n        return (c.Fee__c * (100 - percentFor(c.Level__c)) \/ 100).setScale(2);\n    }\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">getAll() loads every rule once and caches the active ones in a static map. Then discountedFee works out the price, and unknown levels get no discount.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Step 3: Test the Logic<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>@IsTest\nprivate class DiscountRulesTest {\n    @IsTest\n    static void beginnerGetsConfiguredDiscount() {\n        Course__c c = new Course__c(Name = 'Apex Basics', Fee__c = 5000, Level__c = 'Beginner');\n        Decimal expected = (5000 * (100 - DiscountRules.percentFor('Beginner')) \/ 100).setScale(2);\n        Assert.areEqual(expected, DiscountRules.discountedFee(c));\n    }\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Custom metadata records are visible in tests without SeeAllData. Also, the test reads the configured percent, so it keeps passing when admins change the value.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Here is the test result.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>DiscountRulesTest.beginnerGetsConfiguredDiscount  Pass  (12 ms)<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Custom Metadata in Apex and Deployments<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Records live in source files under customMetadata, such as Discount_Rule.Beginner.md-meta.xml. So add them to Git and deploy them with your classes. Protected records in managed packages stay hidden from subscriber code.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Common Mistakes with Custom Metadata in Apex<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Trying to insert or update __mdt records with DML.<\/li>\n\n<li>Reading long text with getAll() and losing text after 255 characters.<\/li>\n\n<li>Querying custom metadata inside loops.<\/li>\n\n<li>Hard-coding record names that admins later rename.<\/li>\n\n<li>Using custom metadata for data that changes every minute.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Best Practices for Custom Metadata in Apex<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Load records once per transaction and cache them in a static map.<\/li>\n\n<li>Use clear DeveloperNames, because code may depend on them.<\/li>\n\n<li>Add an Active__c field, so admins can switch rules off.<\/li>\n\n<li>Keep records in source control with the code.<\/li>\n\n<li>Write tests that read the configured values instead of fixed numbers.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Practice Task<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Add a Max_Seats__c field to a new Level_Setting__mdt type and use it to block enrolments when a course is full. Hint: read it with getInstance(level) in a before insert trigger handler.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Most Asked Interview Questions on Custom Metadata in Apex<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Configuration questions are common in developer interviews. Here are the 21 questions asked most about Custom Metadata in Apex.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Basic Questions on Custom Metadata in Apex<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q1: What is a custom metadata type?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">It is a configurable type whose records are metadata, with an API name ending in __mdt.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q2: How do you read all records in Apex?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Call TypeName__mdt.getAll(), which returns a map keyed by DeveloperName.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q3: How do you read one record?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Call getInstance with the DeveloperName or record Id.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q4: Do getAll() and getInstance() use SOQL limits?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">No, they read from the metadata cache, so they do not count as queries.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q5: Can you query custom metadata with SOQL?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Yes, so you can filter records or read full long text values.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q6: Do custom metadata records deploy?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Yes, because they are metadata, so they move with change sets and the CLI.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q7: Can Apex change custom metadata with DML?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">No, so you use the Metadata API classes, which deploy changes asynchronously.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Code and Scenario Questions on Custom Metadata in Apex<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q8: Why might a long text value look cut off?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Because getAll() and getInstance() return only the first 255 characters.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q9: Are custom metadata records visible in tests?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Yes, tests can read them without SeeAllData.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q10: How do you avoid hard-coded discount rates?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Store them in a custom metadata type and read them at runtime.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q11: How do you make a feature switch?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Add a checkbox record, such as Enable_LearnHub_Sync, and check it before running logic.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q12: How can custom metadata choose an Apex class?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Store the class name in a field, then create it with Type.forName.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q13: Where do records live in source control?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In the customMetadata folder as .md-meta.xml files.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q14: How do you load rules efficiently?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Read them once into a static map and reuse it during the transaction.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Advanced Questions on Custom Metadata in Apex<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q15: When would you choose custom settings instead?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">When code must change values at runtime or values differ per user or profile.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q16: What are metadata relationship fields?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Fields that link a custom metadata record to objects, fields or other __mdt records.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q17: What does protected mean in a managed package?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Protected records are hidden from code outside the package namespace.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q18: How do you update records from Apex?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Build Metadata.CustomMetadata objects and enqueue a deployment with Metadata.Operations.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q19: How do you test logic with records that do not exist?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Read records through a small class you can replace with test data in memory.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q20: Why is custom metadata good for integrations?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Because endpoint paths, mappings and switches can change per org without code changes.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q21: What is a risk of too much configuration?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Logic becomes hard to follow, so document what each record controls.<\/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\/platform\/apex-ref\/references\/apex-ref\/apex-methods-system-custom-metadata-types.html\" target=\"_blank\" rel=\"noopener noreferrer\">Custom Metadata Type Methods (Apex Reference Guide)<\/a><\/li>\n\n<li><a href=\"https:\/\/developer.salesforce.com\/docs\/atlas.en-us.api_meta.meta\/api_meta\/meta_custommetadata.htm\" target=\"_blank\" rel=\"noopener noreferrer\">CustomMetadata (Metadata API Developer Guide)<\/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>Custom Metadata in Apex reads admin-managed settings.<\/li>\n\n<li>getAll() and getInstance() avoid SOQL limits.<\/li>\n\n<li>Use SOQL for filters and full long text.<\/li>\n\n<li>Records deploy with metadata, but DML cannot change them.<\/li>\n\n<li>Cache rules once per transaction.<\/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\">Admins can also call your Apex directly from Flow. Next, read our guide to <a href=\"https:\/\/swapnilzambare.com\/blog\/apex-invocable-methods-flow\/\">Apex invocable methods<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Custom Metadata in Apex lets your code read rules and settings that admins manage in Setup, so business changes do not need a code&hellip;<\/p>\n","protected":false},"author":1,"featured_media":1761,"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":[68,98,78],"swapz_series":[],"class_list":["post-1764","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-salesforce","category-salesforce-developer","category-security-design-patterns","tag-advanced","tag-apex","tag-custom-metadata"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v28.9 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Custom Metadata in Apex: Config-Driven Code<\/title>\n<meta name=\"description\" content=\"Custom Metadata in Apex explained simply: getAll, getInstance, SOQL on __mdt, config-driven rules, deployment, testing and 21 interview Q&amp;A.\" \/>\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\/custom-metadata-in-apex\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Custom Metadata in Apex: Config-Driven Code\" \/>\n<meta property=\"og:description\" content=\"Custom Metadata in Apex explained simply: getAll, getInstance, SOQL on __mdt, config-driven rules, deployment, testing and 21 interview Q&amp;A.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/swapnilzambare.com\/blog\/custom-metadata-in-apex\/\" \/>\n<meta property=\"og:site_name\" content=\"Swapnil Zambare - Blog\" \/>\n<meta property=\"article:published_time\" content=\"2026-10-04T03:30:00+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2026-11-15T06:01:19+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/custom-metadata-in-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=\"8 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/custom-metadata-in-apex\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/custom-metadata-in-apex\\\/\"},\"author\":{\"name\":\"Swapnil Zambare\",\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/#\\\/schema\\\/person\\\/10b09224c8bcc562716e86789427592a\"},\"headline\":\"Custom Metadata in Apex: Build Config-Driven Code\",\"datePublished\":\"2026-10-04T03:30:00+00:00\",\"dateModified\":\"2026-11-15T06:01:19+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/custom-metadata-in-apex\\\/\"},\"wordCount\":1503,\"publisher\":{\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/#organization\"},\"image\":{\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/custom-metadata-in-apex\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/wp-content\\\/uploads\\\/2026\\\/11\\\/custom-metadata-in-apex-featured.png\",\"keywords\":[\"Advanced\",\"Apex\",\"Custom Metadata\"],\"articleSection\":[\"Salesforce\",\"Salesforce Developer\",\"Security and Design Patterns\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/custom-metadata-in-apex\\\/\",\"url\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/custom-metadata-in-apex\\\/\",\"name\":\"Custom Metadata in Apex: Config-Driven Code\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/custom-metadata-in-apex\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/custom-metadata-in-apex\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/wp-content\\\/uploads\\\/2026\\\/11\\\/custom-metadata-in-apex-featured.png\",\"datePublished\":\"2026-10-04T03:30:00+00:00\",\"dateModified\":\"2026-11-15T06:01:19+00:00\",\"description\":\"Custom Metadata in Apex explained simply: getAll, getInstance, SOQL on __mdt, config-driven rules, deployment, testing and 21 interview Q&A.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/custom-metadata-in-apex\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/custom-metadata-in-apex\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/custom-metadata-in-apex\\\/#primaryimage\",\"url\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/wp-content\\\/uploads\\\/2026\\\/11\\\/custom-metadata-in-apex-featured.png\",\"contentUrl\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/wp-content\\\/uploads\\\/2026\\\/11\\\/custom-metadata-in-apex-featured.png\",\"width\":2400,\"height\":1260,\"caption\":\"Custom Metadata in Apex featured image: title card with an Apex code example\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/custom-metadata-in-apex\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Custom Metadata in Apex: Build Config-Driven Code\"}]},{\"@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":"Custom Metadata in Apex: Config-Driven Code","description":"Custom Metadata in Apex explained simply: getAll, getInstance, SOQL on __mdt, config-driven rules, deployment, testing and 21 interview Q&A.","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\/custom-metadata-in-apex\/","og_locale":"en_US","og_type":"article","og_title":"Custom Metadata in Apex: Config-Driven Code","og_description":"Custom Metadata in Apex explained simply: getAll, getInstance, SOQL on __mdt, config-driven rules, deployment, testing and 21 interview Q&A.","og_url":"https:\/\/swapnilzambare.com\/blog\/custom-metadata-in-apex\/","og_site_name":"Swapnil Zambare - Blog","article_published_time":"2026-10-04T03:30:00+00:00","article_modified_time":"2026-11-15T06:01:19+00:00","og_image":[{"width":2400,"height":1260,"url":"https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/custom-metadata-in-apex-featured.png","type":"image\/png"}],"author":"Swapnil Zambare","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Swapnil Zambare","Est. reading time":"8 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/swapnilzambare.com\/blog\/custom-metadata-in-apex\/#article","isPartOf":{"@id":"https:\/\/swapnilzambare.com\/blog\/custom-metadata-in-apex\/"},"author":{"name":"Swapnil Zambare","@id":"https:\/\/swapnilzambare.com\/blog\/#\/schema\/person\/10b09224c8bcc562716e86789427592a"},"headline":"Custom Metadata in Apex: Build Config-Driven Code","datePublished":"2026-10-04T03:30:00+00:00","dateModified":"2026-11-15T06:01:19+00:00","mainEntityOfPage":{"@id":"https:\/\/swapnilzambare.com\/blog\/custom-metadata-in-apex\/"},"wordCount":1503,"publisher":{"@id":"https:\/\/swapnilzambare.com\/blog\/#organization"},"image":{"@id":"https:\/\/swapnilzambare.com\/blog\/custom-metadata-in-apex\/#primaryimage"},"thumbnailUrl":"https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/custom-metadata-in-apex-featured.png","keywords":["Advanced","Apex","Custom Metadata"],"articleSection":["Salesforce","Salesforce Developer","Security and Design Patterns"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/swapnilzambare.com\/blog\/custom-metadata-in-apex\/","url":"https:\/\/swapnilzambare.com\/blog\/custom-metadata-in-apex\/","name":"Custom Metadata in Apex: Config-Driven Code","isPartOf":{"@id":"https:\/\/swapnilzambare.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/swapnilzambare.com\/blog\/custom-metadata-in-apex\/#primaryimage"},"image":{"@id":"https:\/\/swapnilzambare.com\/blog\/custom-metadata-in-apex\/#primaryimage"},"thumbnailUrl":"https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/custom-metadata-in-apex-featured.png","datePublished":"2026-10-04T03:30:00+00:00","dateModified":"2026-11-15T06:01:19+00:00","description":"Custom Metadata in Apex explained simply: getAll, getInstance, SOQL on __mdt, config-driven rules, deployment, testing and 21 interview Q&A.","breadcrumb":{"@id":"https:\/\/swapnilzambare.com\/blog\/custom-metadata-in-apex\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/swapnilzambare.com\/blog\/custom-metadata-in-apex\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/swapnilzambare.com\/blog\/custom-metadata-in-apex\/#primaryimage","url":"https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/custom-metadata-in-apex-featured.png","contentUrl":"https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/custom-metadata-in-apex-featured.png","width":2400,"height":1260,"caption":"Custom Metadata in Apex featured image: title card with an Apex code example"},{"@type":"BreadcrumbList","@id":"https:\/\/swapnilzambare.com\/blog\/custom-metadata-in-apex\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/swapnilzambare.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Custom Metadata in Apex: Build Config-Driven Code"}]},{"@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\/1764","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=1764"}],"version-history":[{"count":1,"href":"https:\/\/swapnilzambare.com\/blog\/wp-json\/wp\/v2\/posts\/1764\/revisions"}],"predecessor-version":[{"id":2342,"href":"https:\/\/swapnilzambare.com\/blog\/wp-json\/wp\/v2\/posts\/1764\/revisions\/2342"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/swapnilzambare.com\/blog\/wp-json\/wp\/v2\/media\/1761"}],"wp:attachment":[{"href":"https:\/\/swapnilzambare.com\/blog\/wp-json\/wp\/v2\/media?parent=1764"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/swapnilzambare.com\/blog\/wp-json\/wp\/v2\/categories?post=1764"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/swapnilzambare.com\/blog\/wp-json\/wp\/v2\/tags?post=1764"},{"taxonomy":"swapz_series","embeddable":true,"href":"https:\/\/swapnilzambare.com\/blog\/wp-json\/wp\/v2\/swapz_series?post=1764"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}