INVALID_CROSS_REFERENCE_KEY in Salesforce: what it means and how to fix it

Short answer: INVALID_CROSS_REFERENCE_KEY means a field that holds a Salesforce ID, such as a lookup, OwnerId or RecordTypeId, contains an ID that doesn't resolve in the org you're writing to. The referenced record doesn't exist there, is the wrong type, was deleted, or isn't visible to the running user.

Lookups, master-detail fields, OwnerId, RecordTypeId and similar fields store the 15- or 18-character ID of another record. When you save a record, Salesforce checks that each of those IDs points at a real record of the right type that the running user is allowed to reference. If any check fails, the save is rejected:

INVALID_CROSS_REFERENCE_KEY: invalid cross reference id

The message rarely names the field, so the first job is working out which ID in the row is the problem.

Why it happens

  • IDs copied from another org. Every org assigns its own IDs. A CSV exported from production carries production IDs, and a different org has no records with those IDs.
  • Parents loaded after children. If you load Contacts before their Accounts exist in the target, every AccountId points at nothing.
  • The wrong object type. A Contact ID in an AccountId field, or a User ID where a queue is expected, is rejected even though the record exists.
  • Deleted or merged records. A parent that was deleted or merged away in the target leaves its old ID dangling.
  • Record type mismatch. A RecordTypeId from another org, or a record type not assigned to the running user's profile.
  • Visibility. The running user can't see the referenced record because of sharing rules or object permissions.

How to fix it

  1. Find the field. Check each ID column in the failing row with a quick SOQL query against the target, for example SELECT Id FROM Account WHERE Id = '001...'.
  2. Load parents first. Order the load so every referenced object is inserted before the objects that point at it, and capture the new IDs as you go.
  3. Map old IDs to new IDs. Replace each source ID with the ID the same record received in the target org before loading children.
  4. Use External IDs. Give parents an External ID field and reference them by that value (for example Account.Legacy_Id__c in a Bulk API column), so Salesforce resolves the target ID for you.
  5. Map record types by developer name, not by ID, and check the running user's profile has access to them.
  6. Check access. Confirm the integration user can read the referenced objects and records.

How Syntharil handles it

Syntharil treats this as a schema or configuration error and is built to avoid it during replication. It reads the relationships between the objects you select and inserts them parent-first. As each record lands, it stores the mapping from its source ID to its new target ID, then rewrites every lookup on child records through that map, including polymorphic lookups such as WhatId. Record types are remapped the same way. If a lookup points at a parent that isn't part of the copy, the field is cleared instead of sending an ID the target can't resolve; ownership fields fall back to the running user.

FAQ

Why do IDs from production fail in my sandbox?

A Full or Partial Copy sandbox keeps the record IDs that existed at refresh time, but anything created in production afterwards has IDs the sandbox has never seen. Developer sandboxes contain no production records at all, so every production ID fails there.

What's the difference between INVALID_CROSS_REFERENCE_KEY and MALFORMED_ID?

MALFORMED_ID means the value isn't a valid Salesforce ID format at all. INVALID_CROSS_REFERENCE_KEY means the value looks like a valid ID but doesn't point at a usable record in this org.

How do External IDs prevent this error?

Instead of sending the parent's Salesforce ID, you send the parent's External ID value and let Salesforce look up the matching record in the target org. The reference resolves to whatever ID the parent has there.

Can record types cause this error?

Yes. Record type IDs differ between orgs that weren't copied from each other, so a RecordTypeId taken from one org fails in another. Match record types by their developer name and look up the ID in the target org.

Can sharing settings cause INVALID_CROSS_REFERENCE_KEY?

Yes. If the running user can't see the referenced record because of sharing or object permissions, Salesforce treats the reference as invalid even though the record exists.

All Salesforce errors →

Try Syntharil free