function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
ivetkinivetkin 

SELECT FOR UPDATE" statement in batches

Can i use "SELECT FOR UPDATE" statement in batches? Now we have an exception:
First error: Locking is implied for each batch execution and therefore FOR UPDATE should not be specified

Avidev9Avidev9
Batch apex uses cursors(queryMore) and breaks the data into chunks and I dont think it is supported in such queries.

FOR Update works for normally SOQL.
sfdcfoxsfdcfox
If you queried the entire database using SELECT ... FOR UPDATE, you would lock the entire database as long as the batch was active. Think about that for a moment, and you'll understand why you are not allowed to attempt to do this. You could tie up, for example, every account in an organization for hours while your batch processes. In the interim, anything that had a trigger to update accounts would fail, updating accounts would fail, and basically the entire organization wouldn't be able to get any work done.