[SalesForce] Apex CPU Time limit exceeded while generating a page/PDF with a LOT of data

There are quite a number of posts I went through that talk about 'Apex CPU Time limit exceeded' issue. I am looking for some advice on how to work around this problem with my situation in mind.

We have a custom VF page with a custom controller that does a LOT of computations and displays a page/PDF with 6 pages of complex data based on ONE record/entity. This part works fine.

Now I need to generate ONE PDF for multiple records (stacked). So, for example, if the user chooses 5 records, the PDF must contain 30 pages of data.

To accomplish this, all I did was to write a for-loop on the method that works on one for as many records as selected. This is where I am hitting 'Apex CPU Time limit exceeded' issue.

The code is fairly complex (5 SOQL statements but a lot of loops, 7000 lines) and although we put in effort to optimize it, re-optimizing it could be a herculean task which won't be approved. Even if we were successful with a little more optimization, there is no guarantee that something that works for 5 records (= 30 pages) now wouldn't fail for 7 (= 42 pages).

Salesforce support won't increase our limits.

I understand that the CPU limit on asynchronous transactions is 6 times higher than synchronous. So I followed this blog post but it still doesn't work for us. Reason: Although I have a REST Consumer method that is declared as @future (CPU limit = 60,000 ms); it calls a global RESTful web service setup in Salesforce (@HttpPost) that is responsible for generating the PDF, and it appears like the latter is still slapped with 10,000 ms of CPU time! Neither the @future methods nor the Batch jobs support getContentAsPDF() method.

So: I would like to utilize the 60,000 ms CPU time in an asynchronous transaction to generate a massive VF PDF and email it out to a user. What are the possible ways to accomplish that? I am really out of ideas.

Thanks in advance.

Best Answer

So we have a PDF that generates 30-60 PDF's with statistical analysis and graphs. What we did was:

  1. Create a custom object to hold the static html data (you may not need to do this but we wanted a way to change the text without modifying the VF page
  2. A object to hold the calculated data and items needed to piece it all together
  3. Created a controller that pulled it all together.

It will take a bit of rethinking but if you can leverage an object to hold the calculations and break up the processing a bit it may help.

As for creating a PDF from a batch, check out BatchPDF on the app exchange.

  • You could also check into leveraging heroku to generate your pdf and return it as well.

Without reviewing you code / org / requirements it will be hard to give a more precise answer and you may want to reach out to a consulting partner...

Related Topic