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
iSqFt_ADiSqFt_AD 

What is causing my Cron job to get Too Many DML Rows error?

Here is my scheduled class and following it is the error I have gotten. This has been running for about a week now and I have received this error on two occasions. Basically it looks at a formula field and reassigns Accounts to the Owner's manager if the field states "YES". Can anyone provide a fix? Thank you!

 

global class AccountOwnerReassignCronJob implements Schedulable{
    global void execute(SchedulableContext SC) {
     List <Account> acc = [Select OwnerId, Owner.ManagerId From Account Where Up_for_Reassignment__c ='YES'];         

    for(Account a: acc){
        if(a.Owner.ManagerId != null)
            a.OwnerId = a.Owner.ManagerId;
    }
    update(acc);
} 
}

 

ERROR:

 

Apex script unhandled exception by user/organization: 00560000001L3AU/00D6000000077GT

 

Scheduled job 'Reassign Stagnant Accounts' threw unhandled exception.

 

caused by: System.LimitException: Too many DML rows: 10001

 

Class.AccountOwnerReassignCronJob.execute: line 9, column 1

Best Answer chosen by Admin (Salesforce Developers) 
Naidu PothiniNaidu Pothini
global class AccountOwnerReassignCronJob implements Schedulable
{   
	global void execute(schedulablecontext sc)
	{
		database.executeBatch(new AssignOwner());
	}
}

 This is the schedulable class

global class AssignOwner implements Database.Batchable<SObject>
{
	global Database.querylocator start(Database.BatchableContext bc)
	{
		String query = 'Select OwnerId, Owner.ManagerId From Account Where Up_for_Reassignment__c = \'YES\'';
			return Database.getQueryLocator(query);
	}
		
	global void execute(Database.BatchableContext bc,List<Sobject> scope)
	{
		List<Account> accList = new List<Account>();
			
		for(Sobject s:scope)
		{
			Account acc =(Account)s;
			accList.add(acc);
		}
			
		for(Account a : accList)
		{
			if(a.Owner.ManagerId != null)
				a.OwnerId = a.Owner.ManagerId;
		}
		update(accList);
	}

    global void finish(Database.BatchableContext bc)
    {
    }
}

 

 

 

 This will be your batch class.

 

Schedulable class will call the batch class and batch class will do what you want to do.

All Answers

Naidu PothiniNaidu Pothini

You can process only 10000 Records in a DML Statment.

 

so you might be updating more than 10000 records.

 

iSqFt_ADiSqFt_AD

Does this mean there is nothing I can do about it to allow the processing of more than those records?

Naidu PothiniNaidu Pothini
global class AccountOwnerReassignCronJob implements Schedulable
{   
	global void execute(schedulablecontext sc)
	{
		database.executeBatch(new AssignOwner());
	}
}

 This is the schedulable class

global class AssignOwner implements Database.Batchable<SObject>
{
	global Database.querylocator start(Database.BatchableContext bc)
	{
		String query = 'Select OwnerId, Owner.ManagerId From Account Where Up_for_Reassignment__c = \'YES\'';
			return Database.getQueryLocator(query);
	}
		
	global void execute(Database.BatchableContext bc,List<Sobject> scope)
	{
		List<Account> accList = new List<Account>();
			
		for(Sobject s:scope)
		{
			Account acc =(Account)s;
			accList.add(acc);
		}
			
		for(Account a : accList)
		{
			if(a.Owner.ManagerId != null)
				a.OwnerId = a.Owner.ManagerId;
		}
		update(accList);
	}

    global void finish(Database.BatchableContext bc)
    {
    }
}

 

 

 

 This will be your batch class.

 

Schedulable class will call the batch class and batch class will do what you want to do.

This was selected as the best answer
iSqFt_ADiSqFt_AD

I attempted to save the first class and received this error: Error: Compile Error: Invalid type: AssignOwner at line 5 column 35

 

I attempted to create the second class and received this error: Error: Compile Error: AssignOwner: Class must implement the global interface method: void finish(Database.BatchableContext) from Database.Batchable<SObject> at line 1 column 14

Naidu PothiniNaidu Pothini

I have edited the class. Try it.

 

Try to save the second class  first and then the first class.

iSqFt_ADiSqFt_AD

Code saved successfully and is loaded into Production now. I will know if it truly works in the morning.


Thank you!