We are facing issue with the Maximum CPU Time in our Sandbox and Production.

Here is the scenario:

We have a visualforce page which allows to send email to contacts(working fine for less than 500 records), for every email the set activity history is true, which creates task against each contact which is a salesforce Standard feature.There's no DML in sendemail logic, nor we have any trigger on Task. Still when we see the debug log, the Maximum CPU Time exceeds 10000* limit.

Our code is all in managed package, and all queries, statement scripts time should have been calculated in the Managed package Limits, but it seems that it is being calculated and show in Default limit.


  1. Salesforce task creation seems NOT bulk as I see N invocations if there are N contacts (I created sample task trigger to verify this and trigger was called N times for N contacts) ?
  2. Does salesforce creating task which executes its own backend code also counted in CPU Time?
  3. Why email send is eating so much CPU time?

Please advise on what needs to be done to handle and overcome CPU timeout issue.

Thank you!

  • Is this your managed package that you're creating or someone else's? – crmprogdev May 23 '14 at 14:00
  • Yes its managed package that we are creating and observing CPU timeout in installed customer org. – Chirag Mehta May 24 '14 at 4:23
  • Are you sending the emails using salesforce's email methods or using REST, or some other means to send them to an external Server for processing? I think you're going to need to look at the most efficient means of performing each item in your list above in bulk. VF alone is perhaps fine for entry, but not likely very efficient in terms of optimization of code. – crmprogdev May 24 '14 at 5:42
  • sending using salesforce email methods, and the code is bulkified calling sendemail once with list of emails to be sent out. – Chirag Mehta May 24 '14 at 11:01
  • The only thing I can think of is to look at your view state as the number of contacts increases. SF is going to require CPU time to move the data for each field from where its stored in memory relative to view state to put assign it to the email fields. I'm thinking in terms of Assembly language constructs here which I programmed in many years ago. Just because the data is in memory, doesn't mean it still won't require CPU time to reassign it from VF view state to Apex code. – crmprogdev May 26 '14 at 16:50

Your Answer

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

Browse other questions tagged or ask your own question.