function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion

In what situation can I query a record and not update it (ENTITY_IS_DELETED)?

I'm running into a curious situation where I'm getting an "ENTITY_IS_DELETED" error when trying to update a record that was previously inserted within the same transaction.  The error is occurring within an After Insert trigger on the CampaignMember object where I'm trying to update the new Lead.

This isn't the original code that was giving me the issue, however while troubleshooting I was able to cause the same error to happen with the following update:
Lead l = [SELECT Id, LastName FROM Lead WHERE Id = :leadId];  // <== gets the previously inserted lead
if (l.LastName == 'Sinatra') {  // temporary for testing
    l.FirstName = 'Ted';
    update l;  // <=== ENTITY_IS_DELETED, Cannot save already-deleted object: id = 00Qm0000008Sn20: []
Full error message:
15:59:20:995 EXCEPTION_THROWN [39]|System.DmlException: Update failed. First exception on row 0 with id 00Qm0000008Sn20EAC; first error: ENTITY_IS_DELETED, Cannot save already-deleted object: id = 00Qm0000008Sn20: []

I've also used the Database.update method and printed out the SaveResult:
Database.SaveResult[getErrors=(Database.Error[getFields=();getMessage=Cannot save already-deleted object: id = 00Qm0000008Sn8m;getStatusCode=ENTITY_IS_DELETED;]);getId=null;isSuccess=false;]

Like I mentioned, the Lead being updated was previously inserted within the same transaction.  I don't understand why I'd be able to query it and have an attempt to update it result in an ENTITY_IS_DELETED error.

I've searched around and haven't found an answer to this issue.  The only thing I can think of is that it's not the Lead being updated that's causing the error, but potentially something else that's being updated as a result of the lead update and that error is hidden somewhere.
A little more information on this:

The process that's executing is basically recreating converted Leads.  There are a number of existing converted Leads in the system that are pointing at the wrong ConvertedOpportunityId.  So this process is recreating those converted leads with the correct ConvertedOpportunityId value ("Create Audit Fields" permission is being used in order to set ConvertedOpportunityId and keep the other audit field values the same on the recreated leads).

So - the Lead being updated in this situation is a newly inserted Lead with IsConverted = true, however my user has the "View and Edit Converted Leads" permission, so I'm still not sure what the problem is.