Halloween DealsAmazon USHalloween week deals: useful picks before month-endAmazon US: current deals, useful picks and tech finds.Check DealsPC HealthRecommendedCrashes, freezes, slowdowns? Check your PC nowSpot repairable issues before they interrupt work.Check PCHalloween DealsAmazon USOctober picks are moving - compare before checkoutAmazon US: current deals, useful picks and tech finds.See Picks×
FREEDOM251

Freedom251 › Blog › APEX

How to Move Salesforce Configuration Out of Apex with Custom Metadata Types

By Freedom251 Team · · 4 min read

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Salesforce Custom Metadata Types let you store deployable configuration records—such as mappings, rules, and allowlists—in metadata, then have Apex, formulas, or other supported features read them to control behavior. This keeps changeable values out of hard-coded literals without turning custom metadata into a general-purpose runtime database.

What Custom Metadata Types do

A Custom Metadata Type defines the fields and records used to describe a configuration. Your application logic reads those records and applies their values, so a mapping or rule can be changed through a metadata workflow rather than by editing each Apex literal that uses it. Salesforce defines custom metadata as “customizable, deployable, packageable, and upgradeable application metadata.” Salesforce Help: What are Custom Metadata Types?

As an Amazon Associate I earn from qualifying purchases.

Common fits include mapping cities or provinces to regions, routing payments to endpoints through configuration interpreted by Apex, defining charges such as duties and VAT, and maintaining allowlists. These examples show the range of possible uses; they do not mean every setting or business value should be modeled as custom metadata.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

When this approach fits—and when it does not

Custom metadata is a good candidate when application behavior depends on stable, structured values that should travel with the application as metadata. A useful pattern is to keep the mapping or rule in records and let reusable code interpret it. The code remains responsible for behavior; the records supply the configuration.

  • Good candidates: deployable mappings, thresholds, routing choices, and similar rules that application logic or formulas can consume.
  • Reconsider the fit: values that must be changed through ordinary runtime data operations, especially deletion, or that contain confidential personal or private information.
  • Check the consumer: confirm that the intended Apex, formula, Flow, or API use supports the access pattern and limits your design needs.

Salesforce’s documentation establishes how the feature behaves and gives examples; it does not prescribe custom metadata as a universal architecture choice. Compare any alternative against deployment, who can change records, runtime read/update/delete needs, package visibility, data sensitivity, and consuming interfaces. The documented material here does not establish a complete feature-by-feature comparison with Custom Settings or other stores.

Define, populate, and deploy the configuration

  1. Define the type and fields. Create the Custom Metadata Type and the fields that describe each configuration record.
  2. Populate records. For a straightforward setup, create and manage records declaratively in Setup. Salesforce also documents programmatic management through Metadata API.
  3. Read the records in application logic. Apex can query accessible custom metadata records with SOQL. Keep the interpretation of the record in reusable logic rather than duplicating values across callers.
  4. Move the configuration with the application. Custom metadata records can be deployed with application metadata through packages or Metadata API; Salesforce also documents change-set deployment. Use the workflow your team already uses to move metadata between environments.

Salesforce documents the management options in Create, Edit, and Delete Custom Metadata Types and Records and programmatic access in Access Custom Metadata Records Programmatically. Exact availability and permissions can depend on the org, feature, and packaging context; confirm those details for the environment where you will implement the type.

Use metadata references in formulas where appropriate

Formulas can reference a record field with this pattern: $CustomMetadata.CustomMetadataTypeAPIName.RecordAPIName.FieldAPIName. Salesforce’s advanced-formula guidance demonstrates holding minimum and maximum values in a metadata record so a configuration change can replace repeated hard-coded values in formulas. Long Text Area fields are not supported in formula references. See Custom Metadata Types and Advanced Formula Fields.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Custom metadata types also support relationships to other metadata entities and definitions. Where a direct reference is appropriate, Salesforce recommends using a relationship rather than a text field; relationships can simplify Apex and help enforce referential integrity in packaging. Validation rules are also available on custom metadata types. See Salesforce Help on Custom Metadata Type Relationships.

Plan visibility around users and packaging

Salesforce documents Public, Protected, and PackageProtected visibility. Public types can be accessed by Apex, formulas, and Flows, while API access is subject to the documented permissions. Protected types in managed packages restrict access to code in the same namespace. PackageProtected visibility in second-generation managed packaging restricts access to code in the same package. The packaging context therefore affects who can consume the type and its records; choose visibility to match the intended boundary rather than assuming all org code or users will see the same records.

Visibility and user permissions can also make API results differ from record counts shown in Setup. Salesforce describes system-mode Apex and user-mode surfaces differently, so verify access in the exact execution context of the interface that consumes the configuration. For the detailed access rules, see Access Rules When Packaging Custom Metadata Types and Records.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Do not use custom metadata as a secret store or runtime database

An unpackaged Protected type is not secret: Salesforce says protected custom metadata types behave like public types outside a managed package. Do not put secrets, personally identifying information, or other private data in public or unpackaged protected records. Salesforce says protected custom metadata in a managed package can suit certain secrets; outside that context, it points to named credentials or encrypted custom fields for confidential values. See Protection and Privacy Options for Custom Metadata Types.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Custom metadata also has important write restrictions. Apex can read and update subscriber-controlled records visible within the code’s namespace under the conditions Salesforce documents, but Apex cannot delete those records. DML operations on custom metadata are not allowed in Partner or Enterprise APIs. Treat changes as metadata-management work, not ordinary runtime DML, if your design depends on reliable deployment and controlled configuration changes. Salesforce documents these behaviors in Access Custom Metadata Records Programmatically.

Account for SOQL and Flow limits

Custom metadata SOQL supports a subset of query syntax. Salesforce lists supported operators and restrictions that include compound OR filters, relationship ordering, and multiple FROM objects. The limitations page says Apex queries of custom metadata do not count toward standard SOQL row limits; the allocations page says custom metadata queries in Flows count toward Apex governor limits. These are specific accounting and syntax rules, not a general performance or scale guarantee. Check the current Custom Metadata Types Limitations and Custom Metadata Allocations and Usage Calculations when designing queries.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

APEXconfiguration managementCustom Metadata TypesSalesforceSalesforce development

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Recommended PC Tool
Recommended PC Tool
Crashes, No Sound, or Screen Glitches?Free driver scan
PC Slower Than It Used to Be?Free scan - under a minute

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.