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

Batch apex


Currently I have an anonymous block scheduled to run everything, however I've hit a brickwall as for as script statements goes.  The script parses a number of records for patterns in the data.  This requires a number of different loops, and as a result ~10,000 records quickly balloon into 200,000 script statements.  My question is, is this something that can be solved via the database.batchable interface, or am I still limited to a number of script statements.  I know I can query 5 million records from the batachable interface, but am I limited to how much I operate on these records?  Any advice would be greatly appreciated.




You will hit the same script limitation with Apex batch. However with Apex batch you can define the number of records per batch. With a smaller number of records you will probably not hit this limit.


Refer to for more information on how to define the # of records per batch. Look in the "ExecuteBatch" method.


Yes you face the same limits with batch but the difference is that batch breaks your processing into chunks of 200 records (by default, you can also lower this) and each chunk gets its own set of limits. In other words you can process each chunk of records with the same limits you are processing 10k with today in your current solution.


If your current process starts with issuing a query then it's probably not going to be a tremendous effort to refactor this. The biggest difference will be the asynchronous nature of the processing.


See the Batch Apex section of the Apex Documentation for more info.




Would the chunks be transparent?  ie. if a pattern starts in one chunk and ends in another will I be able to detect that, or will my methods be run on each individual chunk?


Your methods will run on each individual chunk.  If you need to maintain state across all the chunks, i.e. chunk 2 needs to know that chunk 1 found something of interest in the data,  you can do so but that requires the code to be defined as such explicitly. 


I understand that Database.Stateful will help us maintain states across batches for variables. But one thing is "very" unsure to me, how will Governor limit behave such that I need to do a RollUp calculation job on around 1 million records, in a Batch and thus I need to maintain state across batches, but will Governor limit still re-initialise from start on each batch execution .. ."what" worse issue will I lead into with stateful execution? Can anybody shed some light onto that .. plz :) ...