function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
yvk431yvk431 

Batch Apex from Batch Apex

Hi Friends ,

 

I need some advice on writing a batch apex

 

I am calling a Batch Apex let say to insert contacts for Account SObject

I am having more than 100,000  Account Records, so these are devided in to

several Batch processes let say 200 each .

 

For all the new contact records , iam adding them to a Contact List and upserting them at the end.

 

For one of these batch process , the log is showing "Too many DML rows:15034"

 There may be a chance that few Accounts having large number of contact , and I am getting this error.

Remaining batch process are running with out any problem.

 

I thought the list cannot accomodate more than 10,000  so i used List<List<Contact>>

and for everytime the new record  count increases 2000 i am inserting into a new list.

and upserting each List<Contact> by looping through List<List<Contact>>

Even then I am getting  "Too many DML rows:10030".

 

Please let me know, that there is any certain limit to upsert records fort a single Batch Process?

 

Best Answer chosen by Admin (Salesforce Developers) 
Doug ACFDoug ACF

The governor limits apply to each batch, so you need to make sure that the number of records in that batch won't spawn more than 10,000 DML rows, etc.  You might try decreasing your account batch size using the scope parameter such that the total DML rows for the related contacts is well under 10,000.  Depending on your use case, another approach would be to iterate through batches of contacts instead of accounts.

 

fyi in Spring 10, the 1,000 item limit on collection size is going away.  Although you are still subject to the 2MB heap size limit for giant lists.

All Answers

Doug ACFDoug ACF

The governor limits apply to each batch, so you need to make sure that the number of records in that batch won't spawn more than 10,000 DML rows, etc.  You might try decreasing your account batch size using the scope parameter such that the total DML rows for the related contacts is well under 10,000.  Depending on your use case, another approach would be to iterate through batches of contacts instead of accounts.

 

fyi in Spring 10, the 1,000 item limit on collection size is going away.  Although you are still subject to the 2MB heap size limit for giant lists.

This was selected as the best answer
yvk431yvk431

 Thanks Doug,

 

The scope parameter did the trick for me.