<?xml version= "1.0" encoding= "utf-8" standalone= "yes"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"
  xmlns:content="http://purl.org/rss/1.0/modules/content/">
  <channel>
    <title>SOQL on Spinning Code</title>
    <link>https://spinningcode.org/tags/soql/</link>
    <description>Recent content in SOQL on Spinning Code</description> <generator>Hugo -- 0.157.0</generator>
    <language>en-US</language> <lastBuildDate>Sat, 04 Oct 2025 04:00:00 +0000</lastBuildDate> <atom:link href= "https://spinningcode.org/tags/soql/feed.xml" rel= "self" type= "application/rss+xml" /> <item>
      <title>Prevent Salesforce Custom Metadata Text Field Truncation</title>
      <link>https://spinningcode.org/2025/prevent-sf-metadata-truncation/</link>
      <pubDate>
        Sat, 04 Oct 2025 04:00:00 +0000
      </pubDate> <guid isPermaLink="false">https://spinningcode.org/2025/prevent-sf-metadata-truncation/</guid>  <description>Learn how to prevent Salesforce from truncating Custom Metadata text fields and discover the simple solution.</description> <content:encoded><![CDATA[<p>While working on my <a href="/2025/salesforce-email-invalidator-tool/">Email Invalidation Tool</a>, I ran into Salesforce truncating Custom Metadata text fields at 512 characters. I struggled at first to understand why the static methods – my goto with Custom Metadata – were resulting in broken records. Turns out the solution is simple – use SOQL.</p>
<h2 id="the-problem-salesforce-truncates-custom-metadata-text-fields">The Problem: Salesforce Truncates Custom Metadata Text Fields</h2>
<p>When loading Salesforce Custom Metadata records using the <a href="https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_methods_system_custom_metadata_types.htm">static methods getInstance() or getAll()</a> long strings are truncated at 255 characters (I actually ran into it at 511, but the documentation says 255).</p>
<h3 id="my-use-case-and-reproduction">My Use Case, and Reproduction</h3>
<p>One of the features I wanted to add to my email invalidation tool was auto-detection of email fields. That&rsquo;s turned out to be a whole complicated process that <a href="https://github.com/acrosman/SalesforceInvalidateEmail/pull/8">I&rsquo;m still finishing as of this post date</a>. Part of the solution I am using is to create a custom metadata object to store a list of possible fields. For a variety of unimportant reasons I elected to store that information as a JSON string. So I created a long textarea field maxed out at 131,072 characters. Everything went fine at first: the process scans the objects and drops the results into the metadata record. When I looked at the records in the UI or after downloading to a file it looked just as I expected. But when I started to load the records again I got JSON parser errors because the value had been truncated to 511 characters (see note about about 255 vs 511).</p>
<h2 id="preventing-salesforce-custom-metadata-text-truncation">Preventing Salesforce Custom Metadata Text Truncation</h2>
<p>The answer is to use SOQL. I typically use the static methods when working with Custom Metadata to reduce the number of SOQL queries in my code – but in this case that doesn&rsquo;t work. To reliably get all the data in long text fields you have to use SOQL.</p>
<p>This approach consistently gives you a truncated values:</p>
<pre><code>Email_Invalidator_Cache__mdt cacheRecord = Email_Invalidator_Cache__mdt.getInstance(
  'Field_Scan'
);
</code></pre>
<p>But using a simple SOQL statement to load the same record works just fine:</p>
<pre><code>Email_Invalidator_Cache__mdt cacheRecord = [
  SELECT QualifiedApiName, Cached_Value__c, SystemModstamp
  FROM Email_Invalidator_Cache__mdt
  WHERE DeveloperName = 'Field_Scan'
];
</code></pre>
<h3 id="ask-salesforce-to-fix-this">Ask Salesforce to Fix This</h3>
<p>There is <a href="https://ideas.salesforce.com/s/idea/a0B8W00000GdakyUAB/custom-metadatas-getinstance-doesnt-retrieve-complete-long-text-area-fields">an idea open requesting a fix</a> to this issue. Please up vote.</p>
<h2 id="special-thanks">Special Thanks</h2>
<p>Thank you to <a href="https://www.robbieduncan.com/">Robbie Duncan</a> for helping me out on <a href="https://www.linkedin.com/company/ohanaslack/">Ohana Slack</a> with this one.</p>
]]></content:encoded> </item> </channel>
</rss>
