4

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?

  • 1
    What you're looking for is this. Go ahead and vote, I'll wait here. – sfdcfox Jul 13 '17 at 17:54
  • I voted! - Where is my sticker :) – Eric Jul 13 '17 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 Jul 13 '17 at 21:11
  • @sfdcfox This is from last year, still hasn't been added. Any ideas how this can be accomplished now? – shmuli Jul 15 '18 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. – Shane Steinfeld Oct 9 '18 at 16:00

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Browse other questions tagged or ask your own question.