TransWikia.com

Understanding Batch Apex execution

Salesforce Asked on November 15, 2021

This is a generic question which I couldn’t find enough data online. I have a batch job that runs once every week. This job updates Contact records based on updates on the parent Account.

Scenario: For instance, if the batch job started executing at 4:00 Am PST. And by scope has almost 100,000 Contacts. One random user opens a Contact record at around 4:01 Am PST, which is already in the batch’s scope but yet to get executed (future batch chunk), and commit to the database.

Questions:

  1. Will the Contact record get updated (committed to the database) with the changes from the user and comes out of the batch’s scope?
  2. Contact gets updated with user changes, but still remains in the batch’s scope and get executed and recommitted to the database with new changes based on previous Contact value?
  3. Contact gets updated with user changes, but still remains in the batch’s scope and get executed and recommitted to the database with new changes based on the new updates(from user) that exist in the database?

One Answer

Your records in the execute block will be stale. You retrieved a Database.QueryLocator in the start method, and records will be chunked as if you queried them all at that time. However, these updates from the user will not remove the record from the QueryLocator either, it will still get chunked eventually. So if you just re-query the records in your execute block, your batch should work more or less intuitively.

Answered by Adrian Larson on November 15, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP