4

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?

2 Answers 2

6

Create an Apex class, and store the data you want in a static variable within that class. Static variables do not reset between trigger execution contexts in the same transaction, so you can use that storage area to store before/after trigger data.

1
  • Very quick and easy way to get this working. Thank you!
    – Rory
    Commented Jun 4, 2020 at 19:28
0

One possible solution is to use Platform Events.

The solution design is:

  1. Create a platform event type with an Id field
  2. In the main record before-delete trigger, capture the Ids of lookup records and publish a platform event for each one.
  3. Optionally, in the Detail delete trigger, also publish a platform event for each detail to be updated. This causes both trigger to use the same strategy.
  4. Create a trigger on the platform event that runs the summing logic

Pros:

  • Centralized the logic for summing into one place
  • Decoupled the summary logic from the delete logic, so the delete will be faster and potentially more robust/easier to maintain.

Cons:

  • No guarantee that the summary logic will complete as part of the delete process. It may be milliseconds to minutes behind. Not sure if that matters.
  • The solution uses more that just one trigger so may be harder for your team to maintain.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .