Execute some code after all trigger batches complete?

Suppose I update 1000 records and I have an after update trigger. That trigger will run 5 times for each batch of 200. Is there any possible way to collect data over the course of the 5 executions and then have some code run, only once, on that data after the last batch of 200 is complete?

The only way that’s occurred to me is to set up a Queueable. In theory I could stash in a static variable the accumulated data that I want to pass to it. Enqueue it once during the first execution and then use a static variable to mark that it’s already run. The trouble is that once enqueued, the Queueable remembers only the data passed to it and doesn’t check for further changes. So with each execution I’d have to abort the previous Queueable instance, then re-enqueue it with the new data. But when you queue a job and then abort it, it still counts against limits. So ideally I’d just like it to run only once at the end.

Any thoughts?

Answer

Attribution
Source : Link , Question Author : Charles T , Answer Author : Community

Leave a Comment