I have an Apex class that takes every address for Account, Contact, and Lead objects, verifies and standardizes them via callouts in groups of 100, and begins a batch job to update all the records.

I am getting a CPU time limit exceeded error, but I'm not sure how to best get around it. I'm not looking for small optimizations and tweaks to make the code run a little faster; I've already done that. I'm looking for a scalable solution that will make it so the verification part of the code is separated into multiple transactions.

Is it possible to make regular Apex code execute as a batch process?

EDIT: I tried chaining queueables, and ran into a Maximum callout depth has been reached error. Evidently Queueables executed from other Queueables aren't allowed to make callouts.

  • 3
    that is correct. if you want to process bulk data you should go for batch process where you will execute records in 200 chunk.
    – Himanshu
    Nov 19, 2015 at 17:34
  • I know I can update a large list of records with a batch job that breaks it up into batches of 200 records each. I'm trying to figure out if I can create a batch job to make the callouts and process the results prior to updating the records.
    – Neo
    Nov 19, 2015 at 17:39
  • yes you can easily make the callout. do you see any issue in that ?
    – Himanshu
    Nov 19, 2015 at 17:41
  • It might be helpful to share some code to illustrate where you are stuck. Perhaps using Queueables will be helpful, though you can only kick off one from a given batch execute, so you would have to chain them.
    – Adrian Larson
    Nov 19, 2015 at 17:44
  • Agreed with the previous commenter. Can you stub out a bit of what you're doing? Apex is definitely batchable, but we'd like to see how your code is structured before we recommend a path Nov 19, 2015 at 19:32

1 Answer 1


I have a similar situation with the Queueable class. So this is what I did. The Queueable class is invoked from another class. This class is exposed as REST interface.

In my Rest class, I did the following.

integer currentQueueableJobs  = 0;
integer totalQueueableJobs = System.Limits.getLimitQueueableJobs();
//Queue the class.
if(currentQueueableJobs  < totalQueueableJobs){
ID jobID = System.enqueueJob(new AsyncExecutionExample());
currentQueueableJobs = System.Limits.getQueueableJobs();

Can you do something similar with getCpuTime() and getLimitCpuTime() before you continue to process the data?

Yes! You can change your process to a Batch process in order to get a little bit relaxed governer limits.

  • I was able to change it to a chained Batch process, and can now avoid the CPU time limit. Now I'm trying to figure out how I can show the user the progress of the process. It's hard because each link in the chain is a separate batch job with its own ID.
    – Neo
    Nov 30, 2015 at 22:22

You must log in to answer this question.

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