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
Rachel Zelkina 18Rachel Zelkina 18 

Too Many DML rows:10001

A method in an existing test class is thtrowing this error when I run the test: System.LimitException:
Too many DML rows: 10001
Stack TraceClass.ContractSnapshotCreateDelete.processDelete: line 64, column 1
Class.Lead_Note_test.myUnitTest4: line 251, column 1

This is the failing unit test within the apex class and below that is lines 60-70 of the refernced class. We've just created 1000's of contract and contract snapshot records so I think thats why its failing. Im just not sure how to fix it. Please help!
 
static testMethod void myUnitTest4() {
// ****Contract Snapshot Create/Delete
    account a = new account(Name='Test');
    insert a;
    a.Name='Test4';
    update a;
    contract c = new contract(accountId=a.id, startdate=date.today(), contractterm=1, status='Draft', active__c=true);
    insert c;
    ContractSnapshotCreateDelete controller = new ContractSnapshotCreateDelete();
    contract_snapshot__c cs = new contract_snapshot__c();
    cs.account__c           = a.id;
    cs.contract__c          = c.id;
    cs.YYYYMM__c            = '201706';
    insert cs;
                                 controller = new ContractSnapshotCreateDelete();
    controller.YYYYMM       = '201706';
    controller.create       = true;
    controller.processCreate();
    controller.deletex      = true;
    controller.processDelete();    
    
}

Below are lines 60-76 of ContractSnapshotCreateDelete Class

ContractSnapshotCreate/Delete 
   

public PageReference processDelete() {
        if(deletex){
            list<Contract_Snapshot__c> listContract_Snapshot = [SELECT Id, YYYYMM__c FROM Contract_Snapshot__c Order By YYYYMM__c desc LIMIT 10000];
            if(listContract_Snapshot.size()>0)      
           Delete listContract_Snapshot; 
                if(listContract_Snapshot.size() == 10000){
                    ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.INFO,'First 10000 records deleted, please click delete again');
                    ApexPages.addMessage(myMsg);                  
                } else {
                    ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.INFO, string.ValueOf(listContract_Snapshot.size()) + ' records deleted.');
                    ApexPages.addMessage(myMsg);              
                }
            }
        }        
        deletex = null;
        return null;
    }
Aparna Hegde 1Aparna Hegde 1
Hi Rachel,

Are you able to try the following?

Modify the below code
list<Contract_Snapshot__c> listContract_Snapshot = [SELECT Id, YYYYMM__c FROMContract_Snapshot__c Order By YYYYMM__c desc LIMIT 10000];
if(listContract_Snapshot.size()>0 || listContract_Snapshot.size()<=10000)     
Delete listContract_Snapshot; 
if(listContract_Snapshot.size() == 10000){
ApexPages.Message myMsg = newApexPages.Message(ApexPages.Severity.INFO,'First 10000 records deleted, please click delete again');

If this doesnt solve your problem, then can you please add system.debug() at the lines where its erroring and post the log here?

Cheers,
Aparna
GauravGargGauravGarg
Hi Rachel,

Since you are running test class and failing DML exception 10001 rows i.e. test mthod is also calculating org wide records which shouldn't be the case with test class. 
  1. Always declare test class as Private. 
  2. Always use @TestSetup to create records in test class. 
Private class testClass{
	@TestSetup
	private void createTestData()
	{
		account a = new account(Name='Test');
		insert a;
		
		a.Name='Test4';
		update a;
		
		contract c = new contract(accountId=a.id, startdate=date.today(), contractterm=1, status='Draft', active__c=true);
		insert c;
	}
	
	private static testMethod test4()
	{
		Contract c = [SELECT Id, AccountId FROM Contract LIMIT 1];
		
		ContractSnapshotCreateDelete controller = new ContractSnapshotCreateDelete();
		contract_snapshot__c cs = new contract_snapshot__c();
		cs.account__c           = c.AccountId;
		cs.contract__c          = c.id;
		cs.YYYYMM__c            = '201706';
		insert cs;
		
		controller = new ContractSnapshotCreateDelete();
		controller.YYYYMM       = '201706';
		controller.create       = true;
		
		controller.processCreate();
		controller.deletex      = true;
		controller.processDelete();   
	}

}


Hope this helps!!!

Let me know if you still face some problem.

Thanks,​

Gaurav
Skype: gaurav62990
Support: gauravgarg.nmims@gmail.com