SfSkills Domains Docs GitHub

Home / devops / feature-flag-custom-metadata

feature-flag-custom-metadata

Implement environment-safe feature flags using Custom Metadata Types for Apex, LWC, and Flow. NOT for user-level entitlements or permission sets — use admin/custom-metadata-types.

Open the skill folder on GitHub


Feature Flags via Custom Metadata

Feature flags decouple deploy from release. Custom Metadata Type records are metadata: they deploy with the codebase, are readable from Apex, flows, formula fields, and validation rules through the application cache, and change per environment through a metadata deployment or Setup. This skill defines a canonical Feature_Flag__mdt layout (Is_Enabled__c, Percent_Rollout__c) and a FeatureFlags Apex accessor; the canonical CMDT accessor shape lives in templates/apex/cmdt/.


Before Starting


Questions to Ask Before Configuring

AskWhy it mattersWhat a good answer adds
What value does the committed record carry, and who enables it per environment?A record committed enabled turns the feature on the moment the deployment finishes (gotcha 3).A disabled default in source and a named, per-environment enablement step.
Will later deployments re-send the record, and with which <values>?Leaving a field's <values> out of an updated record keeps the org's current value; an explicit xsi:nil clears it (gotcha 6).A rule for whether source or the org owns the live value after first deploy.
Is the rollout a percentage of users, and must a user see a stable answer?Random selection flips per evaluation; only a hash of user and flag gives a stable cohort (gotcha 1).A deterministic bucket function and the seed it uses.
Does any flag field hold a list (users, profiles, record types)?getAll() / getInstance() return only the first 255 characters of a field, and there is no multi-select field type for custom metadata values (gotcha 4).Short scalar fields on the flag, with lists moved to permissions or child records.
Does anything need to change the flag from code at runtime?Custom metadata can't be changed with DML; Apex deploys it asynchronously through Metadata.Operations (gotcha 5).A deliberate toggle path (Setup, deployment, or an enqueued deployment with a callback).
How will tests control the flag?Tests cannot insert or update custom metadata records with DML (gotcha 7).An accessor that accepts an in-memory override so tests never depend on org records.
When is the flag deleted?Dead flags accumulate as permanent branches nobody dares remove (gotcha 2).A removal date or release, recorded with the flag.

A properly configured flag adds a disabled default in source, a deterministic rollout, a documented owner of the live value, and a removal date, where "just adding a checkbox" ships a feature that turns itself on at deploy time and never goes away.


Core Concepts

Why Custom Metadata, not a Custom Setting

Custom metadata records are deployable metadata (CustomMetadata, suffix .md, folder customMetadata). Custom setting rows are data: they are copied into sandboxes but are treated as data for Apex test isolation and are not part of a deployment. The SOQL query limit doesn't apply to custom metadata types — a transaction can run unlimited SOQL against them — and getAll() / getInstance() read from the application cache without a query at all.

The flag type and a record

<?xml version="1.0" encoding="UTF-8"?>
<!-- customMetadata/Feature_Flag.NewDiscountEngine.md-meta.xml: committed DISABLED -->
<CustomMetadata xmlns="http://soap.sforce.com/2006/04/metadata"
                xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <label>New Discount Engine</label>
    <protected>false</protected>
    <values>
        <field>Is_Enabled__c</field>
        <value xsi:type="xsd:boolean">false</value>
    </values>
    <values>
        <field>Percent_Rollout__c</field>
        <value xsi:type="xsd:double">10.0</value>
    </values>
</CustomMetadata>

Is_Enabled__c is a Checkbox (xsd:boolean); Percent_Rollout__c is a Number with scale 1 (xsd:double; a scale-0 Number would be xsd:int). The type definition (objects/Feature_Flag__mdt/) is shown in references/metadata-examples.md.

Reading the flag

Apex reads through Feature_Flag__mdt.getInstance('NewDiscountEngine'), which returns null when no record matches — treat null as "off". LWC calls an @AuraEnabled(cacheable=true) Apex method that wraps the same accessor. Flows read the record directly, since custom metadata is exposed to flows as well as Apex. Keep the read above any per-record loop: the query is exempt from the SOQL limit, not from CPU time.


  1. Answer the Questions table; if any answer describes an entitlement, stop and design a Custom Permission instead.
  2. Create Feature_Flag__mdt with Is_Enabled__c (Checkbox) and Percent_Rollout__c (Number, 0–100) — start from templates/apex/cmdt/ and references/metadata-examples.md.
  3. Commit each flag record with Is_Enabled__c = false under customMetadata/, and write the FeatureFlags accessor from references/examples.md (deterministic hash, null-safe, test override).
  4. Run python3 skills/devops/feature-flag-custom-metadata/scripts/check_feature_flag_custom_metadata.py --manifest-dir force-app and fix every ERROR (enabled-by-default record, missing Is_Enabled__c) before merging.
  5. Enable per environment through Setup or a targeted deployment, and record who enabled it and when.
  6. After the rollout, delete the flag and its branch in code in the next release, or keep it deliberately as a documented kill switch.

Decision Guidance

NeedUseNot
Org-wide on/off for a code pathFeature_Flag__mdt.Is_Enabled__cHierarchy Custom Setting (data, not deployed)
Stable percentage rolloutHash of user ID + flag name, compared to Percent_Rollout__cMath.random()
Per-user or per-profile permanent accessCustom Permission + permission set, FeatureManagement.checkPermissionAllow-list text on a flag
Flip from an admin screenMetadata.Operations.enqueueDeployment + Metadata.DeployCallbackupdate on the __mdt record
Long configuration textSOQL on the __mdt typegetInstance() (255-character truncation)

Review Checklist


Salesforce-Specific Gotchas

Full list in references/gotchas.md. The two that cause incidents:

  1. Committed enabled — the record deploys enabled and the feature is live at deploy time.
  2. Omitted values persist — re-deploying a record without a field's <values> leaves the org's current value in place.

Output Artifacts

ArtifactDescription
Feature_Flag__mdt typeCheckbox + percentage fields, deployed with the source
Flag recordsOne .md-meta.xml per flag, committed disabled
FeatureFlags accessorNull-safe, deterministic rollout, test override
Checker outputcheck_feature_flag_custom_metadata.py run attached to the PR

Reference Files

FileUse it for
references/examples.mdThe accessor with a deterministic canary, and an admin toggle via Metadata.Operations
references/gotchas.mdSeven behaviours that break flags in production
references/llm-anti-patterns.mdFlag mistakes AI assistants make, with detection hints
references/well-architected.mdPillar reasoning and the corpus lines behind each claim
references/metadata-examples.mdThe Feature_Flag__mdt type definition and a test override pattern

Package files