INVALID_CROSS_REFERENCE_KEY: invalid cross reference id
You set a relationship field to an Id that the platform rejects — either the Id doesn't exist, points at the wrong object type, has been deleted, or refers to a record the running user can't see.
Also seen asINVALID_CROSS_REFERENCE_KEY·invalid cross reference id·INVALID_CROSS_REFERENCE_KEY: invalid cross
The value looks like a Salesforce ID but the platform won't accept it. Five reasons, with how to tell them apart:
1. Wrong sObject for the relationship
Contact c = new Contact(LastName = 'Smith');
c.AccountId = someUserId; // INVALID_CROSS_REFERENCE_KEY — User is not Account
insert c;
Salesforce IDs encode their object type in the prefix (3 chars):
| Prefix | Object |
|---|---|
001 | Account |
003 | Contact |
005 | User |
006 | Opportunity |
00Q | Lead |
500 | Case |
00G | Group |
0DM | (varies — custom prefixes for custom objects start at letters and digits per org) |
Inspect the prefix before assigning. If it's not the type the relationship expects, that's your bug.
2. The Id doesn't exist (or was deleted)
A common cause in integrations: an external system holds a Salesforce ID that was deleted. The record is in the recycle bin or hard-deleted. Either restore (Database.queryAll shows IsDeleted=true rows; undelete brings them back) or update the external mapping.
3. The user can't see the referenced record
Same as the previous error — the running user lacks read access to the parent. This also throws this error, not INSUFFICIENT_ACCESS_OR_READONLY, when the access failure is detected at the cross-reference level instead of during the save.
// Running user has Edit on Contact but no read on this specific Account.
Contact c = [SELECT Id FROM Contact LIMIT 1];
c.AccountId = '001xxxxxxxxxxxxxxx';
update c; // INVALID_CROSS_REFERENCE_KEY
The fix is sharing, the same as the previous error.
4. The Id is from a different org
Sandboxes and production orgs have different IDs for the same record. If you copy data from prod to a sandbox manually (or via export/import without ID remapping), every reference field carrying a prod Id will fail.
Use Sandbox Sandbox Replication, Salesforce Inspector, or a properly-mapped data loader workflow that translates IDs.
5. The Id is malformed
15-character IDs are case-sensitive; 18-character IDs are not (the last 3 are a checksum). If a code path uppercases or trims a 15-char ID, it can become a different record's ID — or no record at all. Always use the 18-character form when storing IDs externally.
How to diagnose fast
Copy the failing ID from the error and paste it into the URL bar: https://yourdomain.lightning.force.com/<id>. If it 404s, the record is gone. If you land on the right object, your code is sending the right type. If you land on a different object's page, your code is sending the wrong type.
