2

I was wondering the following scenario -

  • Consider in start method of batch Apex I query all the contacts which are having status = active - Let's assume it will return 50 million records
  • Execute method will perform some lengthy operations such that the entire batch job will take some time. Also I'll be updating one field on the contact (say Batch_Processed = True)
  • I start the batch job and after execution of start method, I update one contact so that its active = false.

Will this contact record be considered? i.e. after completion of the batch job, will it have Batch_Processed = True?

I couldn't find anything in the documentation related to this. However, I did find a sentence in this link https://developer.salesforce.com/blogs/2019/01/building-a-batch-retry-framework-with-batchapexerrorevent.html

enter image description here

I'm not quite sure - Does it mean that batch job will consider the 'original state' when the start method was executed and ignores the data updates?

  • If the above is true, then what would happen if I delete a record which was originally 'considered' by start method? How Salesforce handles this internally?

1 Answer 1

3

I am assuming your batch uses a Query Locator, rather than an iterator.

There's an interesting blog that looked into how fresh or stale batch data is. Whilst it is quite old now, a rather interesting point it covers is:

Originally, the way this worked was that the data was queried in the start, and the entire query results (all the queried fields) put into temporary storage, to be chopped into batches and processed in the execute method (stale batches).

However, some time ago (around 2012) this was changed so that only the IDs of the records were put into temporary storage. The platform will retrieve the records by ID just-in-time to be processed for each batch.

This blog went on to say there appears to be some amount of "query ahead" that can happen, so there is a chance there could be a whiff of staleness to some records.

I suspect that behaviour may have been changed after this blog was written, since batches actually lock the records they pass to the execute method (a bug was fixed for this recently). As such, I see no reason to re-query the records passed to execute.

However, to answer your specific queries; since the IDs were queried at the time start was called, I would suggest that the Contacts passed to execute may no longer match the original query criteria (see the comment below for the experience of another Salesforce developer that appears to confirm this).

1
  • 1
    I have empirically tested and it absolutely will execute on records that no longer meet the criteria Sep 22 at 2:15

You must log in to answer this question.

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