I have use case which i'm trying to wrap my head around for best approach. I have a vfp showing list of records where user can add/edit/remove records and do a final SAVE (which includes a dml->upsert followed by dml->delete(if there are any). I also have a trigger on after insert, after update, after delete where when a user edits a record & updates/deletes these records then the trigger handler is called to aggregate currency field on all the related records & create a opportunitylineitem with that aggregated value.
So here's what I'm having trouble with - user removes and updates records on the vfp followed by SAVE which will - then controller will perform an upsert dml followed by delete dml - then as per the order of exec 1st the after insert trigger is fired -> which in our case will bypass as there are no new records inserted - 2nd enters after update - > here it aggregates all the records (including the deleted record as here afterupdate is still in "upsert" state & doesn't know there is a delete operation performed on the record. -> finally it enters after delete where it aggregates all the records (which will now have appropriate aggregation by eliminating deleted record). This part is where I'm struggling with on how to over come this issue where if there is an update + delete then do not execute this method in udpate block & instead execute only delete block. Appreciate all you inputs here. Here's the thread to my code & vfp. Thanks! Method used in After Insert, After Update and After Delete must fire only when once even though there are multiple DML(Add,update,delete)