Try Serpent Free

How to fix INVALID_FIELD in Salesforce deployments

A SOQL query, formula, or automation references a field API name that doesn't exist on that object in the target org.

Surfaces during: Apex compile or SOQL execution, most often inside test runs

What it means

INVALID_FIELD means Salesforce could not find the field name used in a SOQL query, formula, or automation on the object it was queried against, in the org where the deployment or test is running. Unlike MISSING_FIELD, which is specific to list views and reports, INVALID_FIELD most often shows up inside Apex tests, SOQL queries, or formula fields at compile or execution time.

Because Apex compiles against the org's live schema at deploy time, a query referencing a field that hasn't landed yet fails the whole class's compilation, not just the one test method, which is why this error often blocks far more tests than the one that actually needs the field.

Diagnosis

Common causes

Apex test queries a field that hasn't deployed yet
A test class's SOQL statement selects a field that ships in a later deployment or a dependent package.
Field renamed after code was written
The field's API name changed, but Apex, a formula, or a Flow still references the old name.
Field is scoped to a different object
The query runs against the wrong object, so a field that exists elsewhere in the org doesn't exist on the object actually being queried.

The fix

  1. Deploy the field ahead of the code that queries it
    Sequence the field's deployment before any Apex or Flow that references it in a SOQL query.
  2. Update every reference after a rename
    Search Apex, Flow, and formula fields for the old API name and replace it with the current one.
    grep -rln "Legacy_Score__c" force-app/main/default/
  3. Confirm the field lives on the queried object
    Check the field's object in Setup against the object in the SOQL FROM clause or formula context.
In practice

How Serpent prevents this

Serpent AI scopes a task by everything it touches, including the fields an Apex test or Flow queries, so a field that a test depends on ships in the same task instead of a later one. See the Salesforce deployment error library.

Metadata and data in one deployment flow in Serpent

Prevention

Bundle a new field with the very first code that queries it
Ship a custom field's metadata in the same deployment as the first SOQL query or trigger referencing it, never in a later, separate release.
Treat a field rename as a full-codebase search-and-update, not a Setup edit
Grep every Apex class, Flow, and formula for the old API name before renaming, and update them all in the same change.
Compile against the actual target org before releasing
Run a check-only deploy against the real target org's current schema so a missing or renamed field fails in CI, not in the production deploy window.
Common questions

INVALID_FIELD, answered

Is INVALID_FIELD the same as MISSING_FIELD?
They're related but not identical. MISSING_FIELD is specific to list views, reports, and layouts; INVALID_FIELD shows up in SOQL, Apex, and formulas, so the fix usually means a code or query change rather than a metadata reference.
Does this only apply to custom fields, or can standard fields trigger it too?
Standard fields can trigger it too, most often when a field is edition- or license-gated (like a Person Accounts field) and the target org's edition doesn't include it.
Why does the same query work in the Developer Console but fail in a test class?
The Developer Console runs against the org you're logged into directly, with its current schema. A test class deployed to a different target org runs against that org's schema, which may not have the same field yet.

Start free. No credit card, no install, no commitment.

Set up in under 15 minutes. No DevOps hire needed.

Curious about faster shipping before you dive in? Let's talk

Commitment free!