Loading

INVALID_CROSS_REFERENCE_KEY When Deleting EntitySubscription Asynchronously in Field Service Reassignment

Veröffentlichungsdatum: Aug 25, 2026
Beschreibung
Field Service admins and developers using custom Apex to manage EntitySubscription (Chatter Follow) records may encounter INVALID_CROSS_REFERENCE_KEY errors when a technician  is reassigned, changing the Work Order status from Confirmed to To Schedule.
 
Note: This behavior only manifests in organizations using custom Apex automation to manage EntitySubscription deletion during FSL reassignment. It is not a product defect.
 

When a technician is reassigned in Field Service, the platform executes FSL sharing recalculation, which revokes the technician's read access to the Work Order. This access revocation causes the Salesforce platform to automatically cascade-delete the EntitySubscription record (the technician's "Follow" record on the Work Order). If custom Apex code queried the EntitySubscription ID before this cascade-delete and then enqueues a Queueable to delete it asynchronously, the Queueable encounters a now-invalid foreign key reference — throwing INVALID_CROSS_REFERENCE_KEY.

System Administrators are not affected because they have "View All Data" permission. Sharing recalculation never revokes SysAdmin access, so the EntitySubscription is never cascade-deleted by the platform. The Queueable delete succeeds normally for SysAdmins.

The error is typically reported in Nebula Logger or system debug logs tied to a Queueable job.

Lösung
Cause
This occurs because the FSL sharing recalculation cascade-deletes the EntitySubscription before the Queueable executes. Re-querying inside the Queueable Execute () method resolves the issue. 

Suggested Approach

Modify the Queueable class to re-query EntitySubscription records inside the execute() method rather than relying on IDs captured before the job was enqueued. Wrap the delete in a try-catch to handle graceful failure if records are already deleted.

 

Pattern to implement:
public class DeleteSubscriptionsQueueable implements Queueable {
private List<Id> subIds;

 

public DeleteSubscriptionsQueueable(List<EntitySubscription> subs) {
this.subIds = new List<Id>();
for (EntitySubscription sub : subs) {
this.subIds.add(sub.Id);
}
}

 

public void execute(QueueableContext context) {
// Re-query inside execute() to check records still exist
List<EntitySubscription> subsToDelete = [
SELECT Id FROM EntitySubscription WHERE Id IN :subIds
];
if (!subsToDelete.isEmpty()) {
try {
delete subsToDelete;
} catch (DmlException e) {
System.debug('EntitySubscription deletion failed: ' + e.getMessage());
}
}
}
}

 

Key changes:
1. Pass List<Id> (not List<EntitySubscription>) to the constructor to avoid stale object references
2. SELECT inside execute() rather than outside the Queueable
3. Wrap delete in try-catch to handle INVALID_CROSS_REFERENCE_KEY or ENTITY_IS_DELETED gracefully
Note:   The try-catch example hides genuine failures.
It catches everyDmlExceptionand only writesSystem.debug. That can suppress permission, trigger, validation, and platform errors unrelated to the expected race. Handle only the anticipated missing-reference outcome and rethrow or log unexpected failures through the customer’s established logging framework.

Alert Note-
This suggested approach is based on the use case described, and isn't an out-of-the-box Salesforce feature. As it involves a custom implementation, it must be thoroughly tested in a lower environment (sandbox) to validate it against your specific business requirements before applying it in production.
Salesforce Support isn't liable for the outcomes of this implementation suggestion. Since it falls outside standard/supported functionality, its adoption and testing remain at the customer's discretion and responsibility.
Zusätzliche Ressourcen
Delete EntitySubscription records synchronously in the same transaction as the reassignment logic. This eliminates the race condition entirely. If you previously encountered INSUFFICIENT_ACCESS_OR_READONLY during synchronous deletion, review whether the Apex class uses "with sharing" (which would restrict access when called in the technician's context) and consider using "without sharing" or invoking the delete from a context with elevated permissions.
Verification
1. Enable debug logging for the Queueable class after implementing the fix
2. Trigger a technician reassignment .
3. Confirm no INVALID_CROSS_REFERENCE_KEY exception appears in the Queueable log
4. Confirm the Queueable executes successfully (or logs a handled debug message if the EntitySubscription was already deleted by the platform)

Note: The cascade-delete of EntitySubscription on sharing loss is standard platform behavior, not a defect. The fix is to make custom Apex resilient to this behavior.
 
Nummer des Knowledge-Artikels

005391128

 
Laden
Salesforce Help | Article