+ Start a Discussion
Daniel KDaniel K 

apex batch job with "total batches"

Hi All,
            Noticed that after perparing and before processing,Total Batches field will be updated.

            Want to understand if preparing is the status where START method query is executed and 
            based on batch size total batches gets calculated ? All this happend during perparing state of batch job ?
            Can we understand if perparing is done means START method has completed successfully ?
            And BEGIN method won't start executing when status is perparing?

           Please comment on my above understandings.

Thanks
D
Raj R.Raj R.
Looking at the https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_batch_interface.htm (Using Batch Apex), it will show you what the batch job status mean. 

Preparing
  • The start method of the job has been invoked. This status can last a few minutes depending on the size of the batch of records.
Daniel KDaniel K
Hi Raj,
                Thanks for the link.
                But that doesn't answer all my questions.
Raj R.Raj R.
The documentation about the job status is quite clear. Can you provide what is answered and what is not?
Raj R.Raj R.
  1. Want to understand if preparing is the status where START method query is executed and based on batch size total batches gets calculated?
    1. Yes, this will return a list of records that will be broken up in N number of batches where the batch size is M.
    2. As stated in documentation "Batch size specified in the scope parameter of Database.executeBatch. Default is 200 records."
    3. As stated in documentation "To collect the records or objects to pass to the interface method execute, call the start method at the beginning of a batch Apex job. This method returns either a Database.QueryLocator object or an iterable that contains the records or objects passed to the job."
  2. All this happend during perparing state of batch job ?
    1. Yes, once the N number of batches of size M is determined, then the job will have a set of records it will process per batch. 
    2.  
  3. Can we understand if perparing is done means START method has completed successfully?
    1. Yes as long as you have not reached any governor limits and the query gets executed correctly. 
  4.  And BEGIN method won't start executing when status is perparing?
    1. Correct. 

Helpful trailhead
  1. https://trailhead.salesforce.com/en/modules/asynchronous_apex/units/async_apex_batc
Daniel KDaniel K
Hi Raj,
           Thanks for your answers.

           The reason for my question on "total batches" was, I had an apex batch job which failed as: cpu time limit exceeded
            But, this has failed during Preparing state.
            In the Preparing state(START), I have only SOQL to be executed, nothing other than that.
            When checked documentation on CPU time limit.. error, it is mentioned that they won't count SOQL execution time in CPU time limit.
            If they don't count SOQL in CPU time limit, then how come I got CPU time limit error in my START method(during Preparing state) ?

            Hope you understood what I meant.

D
           
Raj R.Raj R.
Daniel,

This information was missing so it was hard to understand what the issue was. Considering your scenario, cpu time limit exceeded has several reasons such as workflow rules running in parallel because the code inside your START may be triggering them or even the logic that resides within the batch taking a while to run. Now without seeing the actual batch class, I am going to ask:
  1. Are you doing any queries in your START method?
  2. Is your code bulkified so that you are using Maps, List, or something else necessary?
  3. Are you able to do SOQL for loop? (https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/langCon_apex_loops_for_SOQL.htm)
  4. Have you tried having smaller batch sizes? The default is set to 200 if you don't specify it, but try limiting it to 100.

Is this helpful? 
  1. https://help.salesforce.com/articleView?id=000232681&language=en_US&type=1 
  2. https://developer.salesforce.com/forums/?id=906F0000000kA5cIAE