I have a corner case that I have never experienced before while using triggers. I have three objects in this scope: A master object, a detail object off of that master, and a lookup between the detail object and a separate object. Master -< Detail >- Lookup.
Right now I have a trigger on the detail object to catch any time it is inserted, updated or deleted. After the change, some logic is run to aggregate data on the lookup record. For example if there are 3 detail records associated with the lookup record and one is deleted, the trigger logic will aggregate the data from the remaining two detail records and update the aggregate field on the lookup record. This part works great!
However, in the case where the master record is deleted, the trigger on the detail object does not fire even though they are deleted (this is standard for apex triggers). This means that lookup record is not updated correctly in this situation.
I began working on a trigger for the master object but hit a snag.
On an after delete trigger, the detail records no longer exist so I can't find the affected lookup records through the relationship.
However, if I use a before delete trigger, I can gather the Ids of the lookup records that need to be updated, but can not perform the aggregate summing logic because the detail record has not been deleted yet.
I need a way to gather the Ids of these lookup records in the before delete portion of the trigger but hold off on the aggregating logic until after the deletion has happened.
Is this possible? Or is there an easier way to go about this?