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
Ivan WinzerIvan Winzer 

scheduled apex class getting Too many DML rows: 10001 error

So with the help of a few forums i found out you can schedule classes. So i created one that will check a box on a custom object at 5am daily. But when i went to check this morning on the job it failed due to the error: Too many DML rows: 10001

Below is the code used, which probably needs to be bulk-i-fied but each way i try it errors out. Any help is greatly appreciated.
 
global class WineClubCronJob implements Schedulable{ 


    global void execute(SchedulableContext SC) { 
     List <Wineclub_Membership__c> acc = [Select Id, Tenure_Cron_Job_Schedule__c From Wineclub_Membership__c Where Active__c=True];
     
              

    for(Wineclub_Membership__c a: acc ){ 
        if(a.Tenure_Cron_Job_Schedule__c = False ) 
            a.Tenure_Cron_Job_Schedule__c = True; 
            

    } 


    update(acc); 


    }
}

 
Amit Chaudhary 8Amit Chaudhary 8
Please try below code :-
global class WineClubCronJob implements Schedulable{ 


    global void execute(SchedulableContext SC) 
	{ 
     List <Wineclub_Membership__c> acc = [Select Id, Tenure_Cron_Job_Schedule__c From Wineclub_Membership__c Where Active__c=True limit 10000];
		for(Wineclub_Membership__c a: acc )
		{ 
			if(a.Tenure_Cron_Job_Schedule__c = False ) 
            a.Tenure_Cron_Job_Schedule__c = True; 
		} 
		update(acc); 
    }
}

NOTE :- At a time you can do DML on 10K Record only.

Please mark this as solution if this will resolve you issue.

Thanks
Amit Chaudhary
amit.salesforce21@gmail.com
 
Ivan WinzerIvan Winzer
Thanks Amit i will try this. One question i do have though is will it continute to run (restart) after the first 10000 records are hit. We have over 35000 records and will continue to grow them and need to to handle all daily and not just the first 10000
Amit Chaudhary 8Amit Chaudhary 8
If you want to handle more then 10K record then please create Batch job :-
global class WineClubCronJobBatch implements Database.Batchable<sObject>
{

   global Database.QueryLocator start(Database.BatchableContext BC)
   {
		String query ='Select Id, Tenure_Cron_Job_Schedule__c From Wineclub_Membership__c Where Active__c=True';
		return Database.getQueryLocator(query);
   }

    global void execute(Database.BatchableContext BC, List<sObject> scope)
    {
		List<Wineclub_Membership__c> listToUpdate = new List<Wineclub_Membership__c>();
		for( Wineclub_Membership__c a: (List<Wineclub_Membership__c> ) scope)
		{ 
			if(a.Tenure_Cron_Job_Schedule__c = False )
			{	
				a.Tenure_Cron_Job_Schedule__c = True; 
				listToUpdate.add(a);
			}	
		}
		if(listToUpdate.size() > 0)
		{
			update(listToUpdate); 
		}
    }

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

After that you can create on scheduler class to schedule above batch job.
global class WineClubCronJob implements Schedulable 
{
   global void execute(SchedulableContext sc)
   {
      WineClubCronJobBatch Obj= new WineClubCronJobBatch();
      database.executeBatch(Obj);              
   }
}

Now above code can handler numbers of records. 
Please check below post for more information on batch job
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_batch_interface.htm

1) If the start method of the batch class returns a QueryLocator, the optional scope parameter of Database.executeBatch can have a maximum value of 2,000. If set to a higher value, Salesforce chunks the records returned by the QueryLocator into smaller batches of up to 2,000 records. If the start method of the batch class returns an iterable, the scope parameter value has no upper limit; however, if you use a very high number, you may run into other limits.
2) If no size is specified with the optional scope parameter of Database.executeBatch, Salesforce chunks the records returned by the start method into batches of 200, and then passes each batch to the execute method. Apex governor limits are reset for each execution of execute.

Please mark this as solution if this will help you.

Thanks
Amit Chaudhary