I have a apex class which uses a many aggregate queries to form a list<Survey__c>.

  1. Apex CPU limit is exceeded when I try to insert this list. I have already divided the list into two parts, but the CPU time is still exceeded. enter image description here

I basically need to divide this into smaller chunks of data to insert (less than 200 or 150 records). Can anyone help me achieve this?

  1. Also, what will happen if the list size is a prime number? (Eg: 5179 records) Will I still be able to divide it by any number?

  2. Is there any other way to handle this error?

Thank you!

  • please don't paste screen shots of code; use the code formatting tool; see How to Ask
    – cropredy
    Aug 19, 2020 at 18:18

1 Answer 1


I believe you have misunderstood the term "batch" here. What you are doing is not batching, but rather simply invoking insert multiple times in a row, synchronously.

Fundamentally you need to avoid inserting all your records in one session; it isn't a matter of reducing the size of each insert DML call but rather of reducing the number of records inserted synchronously (i.e. in the same apex code execution).

You need to go learn about asynchronous apex (future methods would not be appropriate here since you cannot pass complex data to one, whilst Batchable or Queueable may or may not be, depending just what you are doing in the code we cannot see in your question). Start with this trailhead.

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.