- Fix Guides
- Salesforce Deployment Failures: Fix Errors, Understand Rollback, and Avoid Common Mistakes
Salesforce Deployment Failures: Fix Errors, Understand Rollback, and Avoid Common Mistakes
Step-by-step fix guide with AI-powered diagnosis from Wrenk.
Most Salesforce deployment failures fall into four buckets: (1) Test coverage below 75% — but the org-wide number can be 90% while a single class is at 71%, and that one class blocks the deploy. (2) Missing metadata dependencies — your validation rule references a field that doesn't exist in the target org, producing INVALID_CROSS_REFERENCE_KEY. (3) Profile permission references — profiles list every object and field they grant access to, so deploying a profile before the objects it references fails with 'no such entity'. (4) Hard-coded record IDs — IDs are org-specific and don't survive a deployment. Change sets have no undo button — to roll back, you must redeploy the previous version of changed components (which requires capturing the 'before' state first) or manually reverse each change. The path to a clean deploy: always run validation first, order metadata correctly (objects → fields → automations → profiles last), and keep a retrievable backup of production metadata before every deploy.
Free, no account needed to start. Connect the org afterwards to run the real scan.
The hardest deployment failures are the ones that succeed in sandbox and fail in production — because they're the ones you can't catch in a dry run. Production has more data, different running users, additional metadata that's drifted from sandbox, and frequently a completely different set of profile assignments. A test class that hits 80% coverage in sandbox may hit 73% in production because production data triggers an Apex branch the sandbox data never reached. Same code, different result.
Validation deployments (`sf project deploy validate` or 'Validate' in change sets) are the single highest-leverage tool against this. Validation runs the full deployment including all tests against the target org's data and metadata, but doesn't actually persist the changes. If validation passes, you have 10 days to run Quick Deploy (skips re-running tests) — meaning your real production deploy can complete in under a minute with the test results already locked in.
Rollback planning has to happen before you deploy, not after. Change sets have no undo button — Salesforce doesn't store the previous version of components you overwrite. If you need to roll back, you need to have already captured the 'before' state (via a metadata retrieve or sandbox refresh), then redeploy that captured state. For destructive changes — fields or classes you deleted — the data is gone and cannot be recovered from a metadata rollback alone.
Salesforce enforces 75% per-class test coverage for any class included in a deployment AND 75% org-wide coverage at the time of deployment. Quick Deploy preserves validation test results for exactly 10 days. Change sets have no native undo — rollback requires redeploying previously-captured metadata.
Source: Salesforce Help — Deploy Metadata From a Sandbox to Production
Symptoms
Deployment returning 'APEX_TEST_SUITE_FAILURE' or 'TEST_LEVEL_INCOMPATIBLE' errors
Error: 'No such column X on entity Y' when deploying validation rules or formulas
Error: 'Invalid cross reference id' or INVALID_CROSS_REFERENCE_KEY
Error: 'You cannot deploy to a required field' or field is required but missing
Profile deployments failing with permission reference errors
Deployment succeeds in sandbox but fails in production
Change Set deployments stuck in 'Queued' status for hours
Root Causes
Test coverage below 75% for specific classes
Salesforce requires 75% test coverage for the entire org AND for any class being deployed. A class at 74% will block a deployment even if org-wide coverage is 90%. The error message usually only shows total coverage, hiding the specific violating class.
Invalid Cross Reference Id
The INVALID_CROSS_REFERENCE_KEY error means a component references something that doesn't exist in the target org — a field, object, record type, or picklist value that was renamed, deleted, or never created. Common in Flows and validation rules that reference fields by API name.
Cannot Deploy to a Required Field
This error occurs when the target org has a field marked as required (at the field definition level, not just page layout) but the deployed Flow, Apex, or process tries to create a record without populating it. The source org may not have the field marked required.
Profile permission reference errors
Profiles reference every object, field, and app the user should have access to. If you deploy a Profile before the custom objects and fields it references, the deployment fails. Profiles must deploy after all their dependencies.
Hard-coded IDs in components
Record IDs in Salesforce are org-specific. Validation rules, flows, or Apex code with hard-coded record IDs fail when deployed to an org where those records have different IDs.
How to Fix It — Step by Step
Read the full deployment error log
In Setup → Deployment Status, click 'View Details' on the failed deployment. Expand the 'Component Failures' and 'Test Failures' sections. The summary line often hides the real error — always read the full failure detail for the specific component and line number.
Fix Invalid Cross Reference Id errors
When you see INVALID_CROSS_REFERENCE_KEY, identify the exact field or object API name in the error. Check if it exists in the target org (it may have been renamed or deleted). Common causes: a Flow references Custom_Field__c but production renamed it to Legacy_Field__c; a validation rule references a record type that doesn't exist; a lookup references an object that was never deployed.
Fix 'Cannot Deploy to a Required Field' errors
This means the target org requires a field value that your deployed component doesn't provide. Compare field definitions between source and target — look for fields marked 'Required' in Object Manager → Fields & Relationships. Either add a default value in your Flow/Apex, or remove the required flag in target (if appropriate), or ensure the field is populated before the record is saved.
Fix test coverage failures
Run the specific failing test classes in the target org to see current coverage. Query ApexCodeCoverageAggregate to find classes below 75%. Coverage that passes in sandbox may fail in production if production has more records triggering different code paths.
SELECT ApexClassOrTrigger.Name, NumLinesCovered, NumLinesUncovered
FROM ApexCodeCoverageAggregate
WHERE ApexClassOrTrigger.Name = 'YourClassName'Order components correctly in change sets
Deploy in dependency order: custom objects and fields first, then validation rules and formulas, then workflows and flows, then profiles and permission sets last. Change sets deploy in the order you added components — reorder if needed.
Plan rollback before you deploy
Before any production deploy, retrieve the current state of the components you're changing. Use the Salesforce CLI: `sf project retrieve start --manifest package.xml --target-org production`. Save this backup — it's your rollback source. Change sets have no undo; you can only roll back by redeploying the previous version.
# Capture current production state before deploying
sf project retrieve start --manifest package.xml --target-org production --output-dir backup/Validate before deploying
Always run a validation deploy first. This runs all tests and validates all metadata against the target org without actually deploying. If validation passes, you can Quick Deploy within 10 days to skip re-running tests.
sf project deploy validate --source-dir force-app --target-org production --test-level RunLocalTestsLet Wrenk find and fix this in your org
Create a free account, connect the org, and run the check that finds this issue and the ones around it — with a fix plan for each, in minutes, not a sales call.
Create a free accountPrefer a walkthrough? Book a demo
How Wrenk prevents this in the first place
Catch deployment failures before they hit production with Wrenk's AI-assisted preview.
Validate org readiness with 200+ checks before pushing changes from sandbox to production.
Common Questions
More answers about this issue and how to resolve it.
Related fix guides
All fix guidesStop debugging manually. Let AI do it.
Wrenk runs automated checks across your Salesforce org and tells you exactly what's broken and how to fix it.