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
Max_gMax_g 

Delete trigger is returning an error

I am working on year end processes and need to delete some zero records from a custom object.  I have written a trigger that will fire when a field on a custom object is set to YES for delete zero records.  The trigger appears to be working correctly until ready to delete the record.  I am getting the following error.

EXCEPTION_THROWN|[28]|System.DmlException: Delete failed. First exception on row 0 with id a0AJ0000005AY6fMAG; first error: ENTITY_IS_DELETED, entity is deleted: []
09:37:48.247 (1247331000)|FATAL_ERROR|System.DmlException: Delete failed. First exception on row 0 with id a0AJ0000005AY6fMAG; first error: ENTITY_IS_DELETED, entity is deleted: []

 

Here is the Trigger code:

 

trigger DeleteZeroOrders onStart_Budget__c (afterupdate) {

 

if(trigger.isUpdate)

{   

Start_Budget__c sb = [Select Delete_Zero_Open_Orders__c fromStart_Budget__c];

system.debug('Delete Open Orders = ' + sb.Delete_Zero_Open_Orders__c);

  

    If(sb.Delete_Zero_Open_Orders__c == 'Yes'){

 

 

 

 

List<Open_Order__c> OrdersToDelete = new List<Open_Order__c>();  

 

for(Open_Order__c o : [Select Id,Current_Year_YTD__c,Future_Year_YTD__c fromOpen_Order__c])

  {   

   

system.debug('Selected Open Orders = ' + o);

   

if(o.Current_Year_YTD__c == 0){

 

    IF(o.Future_Year_YTD__c == 0){

     

     OrdersToDelete.add(o); 

  

system.debug('Orders to Delete = ' + OrdersToDelete);    

  }

    }

    If(OrdersToDelete.size() != 0)

 

Delete OrdersToDelete;

 

  }

    }

}

}

Best Answer chosen by Admin (Salesforce Developers) 
Ranu JainRanu Jain

Hi Max,

 

Please try by putting 'Delete' command out side of for loop. 

This error may cause we are not clearing the list and in second cycle we are trying to delete the the id which has been deleted in first cycle.

All Answers

Ranu JainRanu Jain

Hi Max,

 

Please try by putting 'Delete' command out side of for loop. 

This error may cause we are not clearing the list and in second cycle we are trying to delete the the id which has been deleted in first cycle.

This was selected as the best answer
Max_gMax_g

Thanks for the update.  Worked great.