0

I am getting a DML error in the execute part of the Batch Apex - need a workaround, please

Query:
    select Id,Name from Account

Execute:
    for (Account a: acclist){
       -- need to insert 10 records for each day for next 1 year
       -- i.e 10x365=3650 records
       -- populate a list
    }
    -- insert the list populated above.
finish:
    -- send email

Now the problem is, if the Account query reaches the 3rd account, i get DML Error:10001..(i.e 3 x 3650 exceeds 10001). But I need to do this for over 55 accounts.. i.e 55*10*365 total records.

1
  • do you mean to say you want to insert those many records in one go? Or you want your batch to run each day with less number of records to insert? Jul 21, 2020 at 3:18

1 Answer 1

1

Each invocation of execute() takes place in a single transaction, and is subject to all of the standard limits for an asynchronous context. That includes the DML Rows limit at 10,000 records.

If you must insert this many records per Account, you'll need to reduce your batch size to 1 or 2.

That said, I'd seriously consider reevaluating this data model. Are you meaningfully using 10 records per day per Account? Can you potentially use other types of customization, like a custom UI, to avoid creating any records that may be more like placeholders?

1
  • Thanks for pitching in, David. In our use case, we are creating time based appointment records for each day, hence we need 10 appointments/per day/per account. We are re-using account for a different purpose. Having said that, I got the answer from your first instance. I changed my logic to workaround this. Jul 21, 2020 at 17:09

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.