Deleted records now I get Entity is deleted on reinsert

apexrest-api

I have run the following commands in hope to double check a script

1: List<Culture_TCH__c> oldCultures = [SELECT Id FROM Culture_TCH__c WHERE Assessment_TCH__c = :oldAssessment.Id];

2: delete oldCultures;

But when I insert the same records again I get

ENTITY_IS_DELETED:entity is deleted:--

do I need to empty my recycle bin or something ? So I can insert the same records ? If so How

Best Answer

The old Id value is still in memory. You need to clear out the Id field:

List<Culture_TCH__c> oldCultures = [SELECT Id FROM Culture_TCH__c WHERE Assessment_TCH__c = :oldAssessment.Id];
delete oldCultures;
Culture_TCH__c[] newCultures = oldCultures.deepClone(false);

The first parameter set to false will clear out the ID values from memory and allow a fresh insert.

Related Topic