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
Prakash DevloperPrakash Devloper 

Too many DML rows: 10001 DML error working with Bathapex


Heere is the code.
public class batchApex implements Database.Batchable<sobject> {
    public static List<account> aclist = new List<Account>();
    public static Database.QueryLocator start(Database.BatchableContext bc){
        return Database.getQueryLocator('select Name From Account');
    }
    public static void execute(Database.BatchableContext bc,List<Account> accList){
        
        for(integer i=0;i<50002;i++){
            Account acc = new Account();
            acc.Name='Test'+i;
            acc.AccountNumber='1234567';
            aclist.add(acc);            
        }
        insert aclist;
    }
    public static void finish(Database.BatchableContext bc){
        
    }
    
}

batchApex obj = new batchApex();
Database.executeBatch(obj,2000);
It show error that Too many DML rows: 10001.
Batch apex can process upto 5million record .Why it showin error.
And
If the first transaction succeeds but the second fails, the database updates made in the first transaction are not rolled back. How it works ?
Vanisha_GuptaVanisha_Gupta
Hi,

As stated in government Limit documentation ( https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_gov_limits.htm )
Total number of records retrieved by SOQL queries: 50,0000
Total number of records retrieved by Database.getQueryLocator: 10,000
SOQL :
return Database.getQueryLocator('select Name From Account');


DML:
for(integer i=0;i<50002;i++){
            Account acc = new Account();
            acc.Name='Test'+i;
            acc.AccountNumber='1234567';
            aclist.add(acc);            
        }
        insert aclist;

Here you're trying to insert aclist (which contains how many accounts?)
If its more than 10K government limit won't allow. That's why error.  You have met DML limit not SOQL limit.

Try inserting less that 10k records. It won't give error.