
Salesforce Invalid Cross Reference ID: How to Fix Errors
The “Invalid Cross Reference ID” error means Salesforce can’t find a record that another record or process is trying to reference by its ID. It’s a common issue during Data Loader operations, Flows, API integrations, metadata deployments, and Apex transactions.
This usually happens because an ID is missing, formatted incorrectly, points to a deleted record, or comes from a different environment.
INVALID_CROSS_REFERENCE_ID in Data Loader
Problem:
The “Invalid Cross Reference ID” error occurs when Salesforce Data Loader cannot locate a record referenced by an ID in your import file. This happens when:
- 15-character IDs get corrupted by Excel’s auto-formatting (e.g., 001A0000006yJ9F → 1.00E+14).
- Copy-pasting IDs between environments where they don’t match.
- One invalid row failing an entire batch.
- A referenced record is missing or deleted.
- Using display names (e.g., “Support Queue”) instead of 18-character record IDs.
How to fix it:
- Always work with case-insensitive 18-character IDs. Add a formula field using CASESAFEID(Id) to get the full ID.
- Disable Excel auto-formatting: Prepend IDs with ‘ or format columns as Text.
- Use XL-Connector or a trusted export tool to pull data directly with 18-character IDs and valid related IDs. This prevents manual copy-paste errors.
- Check that all related records actually exist. Use a simple VLOOKUP in Excel or a SOQL query to confirm the IDs are valid before importing.
- Reduce the batch size to 1 in Data Loader if you run into this error. This isolates the problematic row quickly.
- Remember that IDs are org-specific. If you export from a sandbox and import to production, lookup IDs like Queue IDs, RecordTypeIds, and OwnerIds won’t match unless they’re recreated.
INVALID_CROSS_REFERENCE_KEY when Deleting EmailMessage Records in a Flow
Problem:
You’ll encounter the INVALID_CROSS_REFERENCE_KEY error when a Flow tries to delete an EmailMessage record linked to a parent object (like a Case or Account) that’s already been deleted. The EmailMessage becomes “orphaned” – its RelatedToId field points to a non-existent record. Salesforce blocks the deletion to prevent data integrity issues.
How to Fix it:
Implement a Conditional Check in Your Flow
Before deleting an EmailMessage record, validate that its RelatedToId field is populated and references a valid record. Use a Get Records element to confirm the existence of the linked parent object (e.g., Case or Account). If the parent record is missing or RelatedToId is blank, terminate the deletion process.
Start with a Single Record
Begin by executing your flow in debug mode using a single EmailMessage recordId.
Run the flow in debug mode for each of these scenarios:
- EmailMessage with an existing parent – The Flow should delete the record successfully.
- EmailMessage with a deleted parent or no RelatedToId – The Flow should skip this record.
Perform Bulk Testing
After confirming the Flow logic with individual records, test it in bulk with more than 200 records. This helps identify any potential SOQL or DML governor limit issues and ensures the flow can scale efficiently in production environments.
Error Inserting EmailMessage via Apex
When inserting EmailMessage records using Apex, developers may encounter issues that prevent the successful creation of records.
Problem:
- Missing Required Fields: Salesforce requires specific fields like ToAddress and FromAddress to be populated when inserting an EmailMessage. If these fields are blank, the insert operation will fail.
- Invalid or Unsupported Related Records: RelatedToId must reference an existing Case or Lead record. Two common oversights cause errors here:
- The referenced record ID doesn’t exist (e.g., deleted or never created).
- For any object, the org’s “Allow Activities” setting must be enabled (a frequently missed requirement).
To enable ‘Allow Activities,’ go to Object Manager, select the required object, open the Details section, and click Edit:
You can mark the “Allow Activities” checkbox under Optional Features and click Save:
- Automation Conflicts: Validation rules, triggers, or processes (e.g., Flows, Process Builder) may block insertion if their conditions aren’t met.
How to fix it:
- Verify Required Fields: Before inserting the EmailMessage, ensure that both ToAddress and FromAddress are populated with valid email addresses.
- Validate RelatedToId: Confirm that the RelatedToId points to a valid and existing Case or Lead. You can perform a SOQL query to verify the existence of the referenced record.
- Use Debug Logs: Enable debug logs for the user running the Apex code to capture detailed error messages. These logs can help identify whether a validation rule, trigger, or missing field is causing the failure. Audit all automation processes (Flows, Process Builder) for conflicting email actions. Always insert EmailMessage records in after triggers when using parent record IDs.
Deployment Error: Custom Application Metadata Deployment
The “Invalid Cross Reference Id” error often occurs during the deployment of Custom Application metadata in Salesforce, especially when the app includes ProfileActionOverride references.
Problem:
When you deploy a Custom App that references new profiles (included in the same deployment), Salesforce tries to save the app before the profiles. At that point, the profile IDs don’t exist yet in the target org. As a result, the app can’t resolve the references and throws the “Invalid Cross Reference Id” error.
How to Fix it:
You can resolve the issue using either of the following methods:
- Add the Profile Metadata: Include the related profiles in your deployment package. This helps Salesforce recognize the dependencies during deployment.
- Use a Two-Step Deployment:
- Manually create the referenced profiles in the target org before deploying the app.
- Or, deploy the profiles first, then deploy the app separately.
Error When Exporting CaseComments
When you export CaseComment records using Data Loader, you might see this error.
It usually happens in orgs that still have Self-Service Users. These users do not exist as standard User records in Salesforce, so specific fields that depend on a valid user, like CreatorFullPhotoUrl and CreatorSmallPhotoUrl, can cause issues.
How to Fix It
Exclude Problem Fields
When exporting CaseComment records with Data Loader:
- Do not include the CreatorFullPhotoUrl and CreatorSmallPhotoUrl fields.
- This avoids trying to reference a user record that does not exist.
Use the Data Export Tool
If you prefer, use the built-in Data Export tool in Setup. It handles these references automatically so that you won’t run into this error.
Debug Checklist: “Invalid Cross Reference ID”
- Use 18-character IDs: Always reference records using their full 18-character ID (CASESAFEID). 15-character IDs can cause errors in some contexts.
You can quickly convert IDs using the “Convert IDs” feature available in XL-Connector and G-Connector. These tools make bulk conversion easy and help maintain accuracy across case-insensitive systems.
Read the full guide: How to Convert 15 to 18 Character IDs in Salesforce
- Test with a batch size of 1: Reduce batch operations (Apex, integrations, data loads) to 1 record per batch to pinpoint the exact failing record.
- Verify the ID type: Ensure the ID matches the correct Salesforce object. Check the 3-character prefix (e.g., 001=Account, 003=Contact). An Account ID (001…) won’t work in a Contact lookup.
- Check record access: Confirm the user/profile running the operation has “Read” permission on the target object and record. Review sharing rules, OWD, and FLS.
- Confirm the record exists: Query your org (SELECT Id FROM Object WHERE Id = ‘…’) to ensure the referenced ID hasn’t been deleted and exists in this org (not a sandbox or different environment).
Conclusion
The “Invalid Cross Reference ID” error usually means Salesforce can’t find a record you’re trying to reference. It often comes down to missing, deleted, or incorrect IDs. Whether you’re using Data Loader, Flows, Apex, or deploying metadata, the fix lies in identifying the faulty ID and checking if it’s valid, exists in the org, and matches the correct object type.
Use the checklist above to narrow down the issue quickly. Tools like XL-Connector can help reduce these errors by pulling accurate 18-character IDs and avoiding manual mistakes.

Xappex CRM data management solutions

Looker Studio for Salesforce
Connect Salesforce reports and queries to your Google Data Studio dashboards.

Excel Merge
Calculate advanced Excel models. Generate Excel documents based on Salesforce data. All with a single click from a Salesforce record page.