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?

  • 2
    What you're looking for is this. Go ahead and vote, I'll wait here.
    – sfdcfox
    Commented Jul 13, 2017 at 17:54
  • I voted! - Where is my sticker :)
    – Eric
    Commented Jul 13, 2017 at 18:59
  • Interesting idea. What I'm describing is slightly different - not to run at the end of the transaction, just after the present execution of the present trigger reaches its last set of records.
    – Charles T
    Commented Jul 13, 2017 at 21:11
  • @sfdcfox This is from last year, still hasn't been added. Any ideas how this can be accomplished now?
    – shmuli
    Commented Jul 15, 2018 at 20:01
  • 1
    Aw, crap. @CharlesT, I set my query to LIMIT 1000...but there were only 14 records in my sandbox! Ugh. Okay, yes, I inserted 1k records and did see the start and finish debug lines in my code 5 times each. That makes more sense now. I'm going to post a related question, probably later today. I'll ping you with the URL here if you're interested. I appreciate the help and want to make sure you get some credit for it! Thanks, Charles. Commented Oct 9, 2018 at 16:00


You must log in to answer this question.

Browse other questions tagged .