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
Ben Wild 8Ben Wild 8 

Deleting Child Object records before Update in Trigger

I am working on an automatic forecasting system that looks at the start, end date and value of a project and then automatically assigns the revenue as forecasts across the deployment months. Forecasts are created as custom objects below the opportunity object. Originally I would have my trigger delete all the forecasts and create new ones with each update. The issue is, with the trigger running after update, the old forecasts are triggering off validations as the forecasts update after the validations.

To get around this I created a before update trigger to delete the old forecasts before the validations run, my code is as follows;
trigger deleteExistingForecasts on Opportunity (before update) {
   
    Set<Id> ids = new set<Id>();
    
    for(Opportunity op: Trigger.new){	    
	    if(op.Automatic_Forecasting__c == true){
	    	ids.add(op.Id);	
	    }
    }
    delete [SELECT Id FROM Opportunity_Forecast__c WHERE Opportunity__c IN :ids];
}
My issue with this is that it just throws the error: Error:Apex trigger deleteExistingForecasts caused an unexpected exception, contact your administrator: deleteExistingForecasts: execution of BeforeUpdate caused by: System.DmlException: Delete failed. First exception on row 0 with id a099E000002GWg3QAG; first error: SELF_REFERENCE_FROM_TRIGGER, Object (id = 0069E000006JLS7) is currently in trigger deleteExistingForecasts, therefore it cannot recursively update itself: []: Trigger.deleteExistingForecasts: line 10, column 1

I'm a bit confused as to why though?
 
v varaprasadv varaprasad
HI Ben,

Use After Update instead of before update.


Hope it will helps you.


Thanks
Varaprasad

 
Ben Wild 8Ben Wild 8
I cant use after update as the validations will run and throw an error.
v varaprasadv varaprasad
No we cannot use after events for validations .
We need to use lway for before events to through validations.


Use Before Trigger:
In the case of validation check in the same object.
Insert or update the same object.

Use After Trigger:
Insert/Update related object, not the same object.
Notification email.
We cannot use After trigger if we want to update a record because it causes read only error. This is because, after inserting or updating, we cannot update a record.

Thanks
Varaprasad
 
Ben Wild 8Ben Wild 8
Thanks for the help with this so far. Sorry I may not have been clear enough, I have validation rules set up in the standard way against the Opportunity object, these run at the same time as the database is updates so a before update trigger runs before this and after update runs after.

The trigger that runs ater the update is too big to post here but basically what it did do is check if the opportunity has forecasts against it, if automatic forecasting is set to true, it then deletes the exisiting forecast records and creates new ones based on the dates and values in the opportunity.

The Issue is that deleting the old records after update means that if for example I decrease the value of the opportunity, the old forecasts remain there until after the update. The issue with this is I have various validations, one being a check to see that the opportunity owner hasnt forecast more than the opportunity value. This validation then goes off and stops the update which then stops the trigger from running.

The idea of the before update delete is that I could delete the old forecasts before the update and then any validations for the forecast wont go off and stop the update.