<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Thisura Manjitha Samarakoon</title>
    <description>The latest articles on DEV Community by Thisura Manjitha Samarakoon (@this8).</description>
    <link>https://dev.to/this8</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F732253%2F1d1e20cf-aa39-4958-bdb1-53ee8134d107.jpeg</url>
      <title>DEV Community: Thisura Manjitha Samarakoon</title>
      <link>https://dev.to/this8</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/this8"/>
    <language>en</language>
    <item>
      <title>Using Custom Metadata in Apex to Replace Hardcoded Configuration Values</title>
      <dc:creator>Thisura Manjitha Samarakoon</dc:creator>
      <pubDate>Mon, 18 Aug 2025 10:58:45 +0000</pubDate>
      <link>https://dev.to/this8/using-custom-metadata-in-apex-to-replace-hardcoded-configuration-values-506f</link>
      <guid>https://dev.to/this8/using-custom-metadata-in-apex-to-replace-hardcoded-configuration-values-506f</guid>
      <description>&lt;p&gt;In many Salesforce projects, developers often start by hardcoding configuration values (like API URLs, keys, or feature toggles) directly into Apex classes. While this might seem quick and simple at first, it creates long-term maintenance problems. &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Changing values requires code edits and deployments. &lt;/li&gt;
&lt;li&gt;Supporting multiple environments or services becomes tricky. &lt;/li&gt;
&lt;li&gt;Non-developers (like admins) cannot update these values.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;A much better approach is to use Custom Metadata Types (CMDT) to externalize configurations. This keeps your Apex clean, makes maintenance easier, and empowers admins to manage settings without touching code. &lt;/p&gt;

&lt;h2&gt;
  
  
  Example Use Case: Cloud Integration Endpoints
&lt;/h2&gt;

&lt;p&gt;Let’s take the example of integrating Salesforce with multiple cloud storage providers. Initially, you might write something like this.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;private final static String uploadEndpoint   = 'https://.../s3-salesforce-integration'; 
private final static String downloadEndpoint = 'https://.../s3-salesforce-integration-download'; 
private final static String deleteEndpoint   = 'https://.../s3-salesforce-integration-delete'; 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This works fine until you add more providers (Google Drive, OneDrive, etc.) or need to update the URLs. &lt;/p&gt;

&lt;h3&gt;
  
  
  Defining Custom Metadata
&lt;/h3&gt;

&lt;p&gt;To make this flexible, create a Custom Metadata Type (e.g. Integration_Config__mdt) with fields like:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Upload_Endpoint__c &lt;/li&gt;
&lt;li&gt;Download_Endpoint__c &lt;/li&gt;
&lt;li&gt;Delete_Endpoint__c &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Then, create records for each provider. &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;AWS &lt;/li&gt;
&lt;li&gt;Google &lt;/li&gt;
&lt;li&gt;OneDrive &lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  The Pitfall - Initialization Error
&lt;/h3&gt;

&lt;p&gt;My first attempt to pull metadata into Apex looked like this.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Integration_Config__mdt awsConfig = Integration_Config__mdt.getInstance('AWS'); 

private static final String uploadEndpoint   = awsConfig.Upload_Endpoint__c; 
private static final String downloadEndpoint = awsConfig.Download_Endpoint__c; 
private static final String deleteEndpoint   = awsConfig.Delete_Endpoint__c; 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But Salesforce gave an error on deployment. &lt;/p&gt;

&lt;p&gt;&lt;code&gt;Variable does not exist: awsConfig&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;That’s because you can’t directly use a runtime fetched variable (getInstance) to initialize static final variables at the class level. &lt;/p&gt;

&lt;h3&gt;
  
  
  The Solution - Static Initializer Block
&lt;/h3&gt;

&lt;p&gt;The correct way is to use a static initializer block, which executes once when the class is loaded.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;private static final String uploadEndpoint; 
private static final String downloadEndpoint; 
private static final String deleteEndpoint; 

static { 
    Integration_Config__mdt awsConfig = Integration_Config__mdt.getInstance('AWS'); 

    if (awsConfig != null) { 
        uploadEndpoint   = awsConfig.Upload_Endpoint__c; 
        downloadEndpoint = awsConfig.Download_Endpoint__c; 
        deleteEndpoint   = awsConfig.Delete_Endpoint__c; 
    } 
} 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now the values are dynamically loaded from metadata and available across all methods. &lt;/p&gt;

&lt;h2&gt;
  
  
  Before vs After
&lt;/h2&gt;

&lt;h3&gt;
  
  
  1. Hardcoded Configuration (Rigid &amp;amp; Risky)
&lt;/h3&gt;

&lt;p&gt;Apex Class &lt;br&gt;
   ├── Upload URL   -&amp;gt; hardcoded &lt;br&gt;
   ├── Download URL -&amp;gt; hardcoded &lt;br&gt;
   └── Delete URL   -&amp;gt; hardcoded&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Requires redeployment for every change. &lt;/li&gt;
&lt;li&gt;Hard to scale for multiple providers. &lt;/li&gt;
&lt;li&gt;Not manageable by admins. &lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  2. Metadata-Driven Configuration (Flexible &amp;amp; Scalable)
&lt;/h3&gt;

&lt;p&gt;Custom Metadata (Integration_Config_&lt;em&gt;mdt) &lt;br&gt;
   ├── Record: AWS &lt;br&gt;
   │    ├── Upload_Endpoint&lt;/em&gt;&lt;em&gt;c &lt;br&gt;
   │    ├── Download_Endpoint&lt;/em&gt;&lt;em&gt;c &lt;br&gt;
   │    └── Delete_Endpoint&lt;/em&gt;_c &lt;br&gt;
   ├── Record: Google &lt;br&gt;
   ├── Record: OneDrive &lt;br&gt;
   └── ... &lt;/p&gt;

&lt;p&gt;Apex Class &lt;br&gt;
   └── Reads values dynamically using static block &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Values stored in metadata records. &lt;/li&gt;
&lt;li&gt;Easy to update without touching code. &lt;/li&gt;
&lt;li&gt;Supports multiple configurations. &lt;/li&gt;
&lt;li&gt;Admin friendly. &lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Key Takeaways
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Avoid hardcoding any configuration values (URLs, IDs, toggles, etc.) in Apex. &lt;/li&gt;
&lt;li&gt;Use Custom Metadata Types to make your code flexible and environment friendly. &lt;/li&gt;
&lt;li&gt;Remember, static variables that depend on metadata should be initialized inside a static block, not inline. &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;With this approach, you’ll make your Apex code more maintainable, scalable, and admin friendly whether you’re managing API endpoints, feature flags, or any other configuration values. &lt;/p&gt;

</description>
      <category>salesforce</category>
      <category>apex</category>
      <category>custommetadata</category>
      <category>programming</category>
    </item>
  </channel>
</rss>
