0

Is it possible to initiate another batch from the Execute method of a running batch?

My requirement:

Users charge time to Timecards. At the end of the week I have a group of timecards (Timecard Header records) where time is charged to place holder projects. These place holder projects have child projects that are Billable projects. The time charged to these place holder projects should be distributed to all the active milestones of these child projects. Currently, I have a future method that takes the time from this timecard with the place holder project and distributes it the active milestones of the child projects by creating time card Header record for each of these active milestone/project combination. We are running into CPU time limit error when a place holder project has large no. of child projects.

In order to get around this problem I chose to implement this using batch apex. The idea is to have one batch apex batch1 to process these place holder timecards in batches of 1. Then in the Execute method of batch1, if the child project count > 50 then initiate batch2 from the Execute method to process 10 child projects at a time.

  • I dont think you can do that. You can chain batches by calling the second batch in the future method of the first one but in the execute() it would run totally differently, and quite likely you would hit the governor limits. – Mahmood Mar 22 '17 at 20:48
  • Did you profile the code to see if you could solve the time limit error? Could be a small change is all that's needed – Eric Mar 22 '17 at 20:56
  • @Eric. yes I did. The problem is that a manager could approve around 10 place holder timecards. The place holder project in each of these timecards could have 100 child projects . All this is now being processed in a future method and hence running into cpu time limit. – Neema Mar 23 '17 at 13:58
5

No, you can't execute a separate batch anywhere other than the finish method. However, you are allowed a single System.enqueueJob call that you could use to process the data using a Queueable class. This queueable class can chain itself indefinitely (in production) to use as much CPU time as it pleases (but there will be delays between each successive chain to limit resource usage).

| improve this answer | |
  • So are you suggesting to use a combination of batch and Queueable class? Could I make this call in the Execute method of the batch class – Neema Mar 22 '17 at 21:13
  • @Neema Yes, you are allowed a single Queueable call per Batchable and Queueable execute method, so you can jump to a Queueable from your Batchable, then chain that as many times as you need to complete your processing. – sfdcfox Mar 22 '17 at 21:19
  • Thank you. That is very helpful. I would just like to confirm that once the chained Queueable completes processing , the execution will continue with the original batch until the next time the Queueable call is made. In this manner the execution jumps between batch and queueable successfully. Is my understanding correct? – Neema Mar 22 '17 at 21:27
  • @Neema No. The Batchable will not wait for the Queueable, but continue processing the next execute method. The Queueable will run in parallel to the batch. – sfdcfox Mar 22 '17 at 21:41
  • ok. Thank You. If the Batchable continues with processing the next Execute method, the next Execute method could call the Queueable again, correct? What I am trying to get at is that during the execution of the entire batch, I wont run into any limits even if each Execute method makes a call to the Queueable? – Neema Mar 22 '17 at 21:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.