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
LakshmanLakshman 

How to write an Apex schedulable interface for 10000+ records and write test class for the same?

Hi all,

I want to write a schedulable interface which updates mass records for accounts. The code which is written for this is as below.....

 

 

global class ScheduleAccountUpdate implements Schedulable{
   global void execute(SchedulableContext ctx) {
      List<Account> acc = [SELECT today__c from Account WHERE CloseDate__c AND today__c = false];
        for (Account a : acc) {
                a.Closed_date__c = true;
        }     
      update acc;      
   }
}

-I want to ask that if my number of records are more than 10000 then will there be any problem and also whats the limit for, FOR loop. If my List exceeds 10000 the for loop should work. I had 9000+ records and it was taking some time for do schedulable job.

-Also How will i write a test class for above code.

 

Any help on this will be greatly appreciated.

 

Thanks in advance,

NightFox

 

 

regger118regger118

use the batch interface to update those records, you can then schedule that batch job.

LakshmanLakshman

Thanks for your reply regger.

Do you have any example for such scenario. If yes can you please give along with test case.

I dint find any example for my case.

 

Thank you.

NightFox

harsha__charsha__c
global class BatchApex implements Database.Batchable<sObject>,Schedulable, Database.Stateful
{
	global String Query;
	//Constructor of the class
	global BatchApex(String q)
	{
		Query=q;
	}
	//start method of the batch, which will return the list<sObject> of size=scope to the execute method
	global Database.QueryLocator start(Database.BatchableContext BC)
	{
		return Database.getQueryLocator(Query);
	}
	//Execute method , which will recieve the scope from the start method of the batch
	global void execute(Database.BatchableContext BC, List<Student_Course__c> scope)
	{
        	update scope;
	}
	//Finish method, that will perform required post batch operations...
	global void finish(Database.BatchableContext BC)
	{

	}
	
}

 Make a call to the batch as followed....

Database.executebatch(new BatchApex('SELECT today__c from Account WHERE CloseDate__c AND today__c = false');

 for the test method you can follow the below link......

 

http://mattreyuk2.wordpress.com/2010/08/21/testing-batch-apex/

 

If this gives u solution, mark it as a solution...