1

The whole aim of having the batch class is to create an extract csv. in my execute method, I am processing over my records and grouping them and performing calculation and storing it in a map of String and csvformatWrapperClass. In the finish method , I am sorting the map values and then iterating over the map values and creating a csv file. This process runs fine for smaller number of records, however fails when I am trying to perform it with 70K+ records with the below error

First error: Apex CPU time limit exceeded

When I monitor the job, it process every batch of records and status changes to complete , and then after few seconds of it saying complete , I get the above message on status details, which makes me think that I am doing some thing wrong in finish method.

enter image description here

6
  • 1
    You still have to deal with CPU limits in the execute method and the amount of data you are dealing with is causing you to hit it. One way to get around this is, instead of storing the data in a map, write it to a custom object. Then in the finish method, execute another batch that processes the records and with each scope of the batch adds the data to a saved document, then in that batch's finish method send the file and clear out the custom object. Basically you need to find a way to bath the compilation of the final data as well if you want it to be scalable.
    – Eric
    Commented Sep 18, 2015 at 6:18
  • Hi @Eric, Thanks for the explaination , however using your suggested approach , could affect my sorting which I am doing using my wrapper class. Sorting is happening based on two fields first on one fields and if first field is equal then based on second field. Any suggestions to that?
    – Aren
    Commented Sep 21, 2015 at 0:50
  • When you run the second batch, use Order By with two fields in the SOQL query in the start method....If that does not meet your needs, create a custom iterable object that sorts as you need.....
    – Eric
    Commented Sep 21, 2015 at 0:52
  • Would you like me to add it as an answer to close the question out?
    – Eric
    Commented Sep 22, 2015 at 0:27
  • @Eric , yes please.
    – Aren
    Commented Sep 22, 2015 at 1:07

2 Answers 2

1

From comment #1:

You still have to deal with CPU limits in the execute method and the amount of data you are dealing with is causing you to hit it. One way to get around this is, instead of storing the data in a map, write it to a custom object. Then in the finish method, execute another batch that processes the records and with each scope of the batch adds the data to a saved document, then in that batch's finish method send the file and clear out the custom object. Basically you need to find a way to bath the compilation of the final data as well if you want it to be scalable.

From comment#2

When you run the second batch, use Order By with two fields in the SOQL query in the start method....If that does not meet your needs, create a custom iterable object that sorts as you need.....

0

I had faced a similar issue with one of my batches. The only way out I know is what @aren has suggested. This batch needs to be broken down into multiple sequential batches. Or, if you could find a way around to alter the logic in such a way to reduce the processing time of Finish method.

You must log in to answer this question.

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