You need to sign in to do that
Don't have an account?
Simon Baird
How to batch a scheduled apex class
Hi all, I am fairly new to apex coding and have created a scheduled class that checks a number of records to fit a criteria then performs a simple update to a checkbox field. The problem is I am getting the Apex CPU time limit exceeded as it is checking through over 5000 records. I believe I need to set this up as a batch to make it work. Can someone help me convert this code to a batch
global class SalesInvoicePaid implements Schedulable {
// Execute method
global void execute(SchedulableContext SC) {
// Code to be executed when the schedule class wakes up
List <c2g__codaInvoice__c> siv = [SELECT ID, Paid__c
FROM c2g__codaInvoice__c
WHERE c2g__PaymentStatus__c = 'Paid' AND Paid__c = False AND c2g__InvoiceDate__c > 2014-07-01];
for(c2g__codaInvoice__c s: siv){
s.Paid__c = true;
}
update(siv);
// this section of code will abort the current schedule job
try {
system.abortJob(sc.getTriggerId());
} catch (exception e) {system.debug('#### schedule job exception while aborting:' + e);}
// reschedule the job
system.debug('#### schedule job executing');
scheduleNow();
}
global static void scheduleNow() {
// this section of code will schedule the next execution 1 minute from now
datetime thisTime = system.now().addHours(24);
integer minute = thisTime.minute();
integer second = thisTime.second();
integer hour = thisTime.hour();
integer year = thisTime.year();
integer month = thisTime.month();
integer day = thisTime.day();
String timeStamp = second + ' ' + minute + ' ' + hour + ' ' + day + ' ' + month + ' ? ' + year;
string jobName = 'SalesInvoicePaid';
SalesInvoicePaid p = new SalesInvoicePaid();
system.schedule(jobName, timeStamp , p);
}
}
global class SalesInvoicePaid implements Schedulable {
// Execute method
global void execute(SchedulableContext SC) {
// Code to be executed when the schedule class wakes up
List <c2g__codaInvoice__c> siv = [SELECT ID, Paid__c
FROM c2g__codaInvoice__c
WHERE c2g__PaymentStatus__c = 'Paid' AND Paid__c = False AND c2g__InvoiceDate__c > 2014-07-01];
for(c2g__codaInvoice__c s: siv){
s.Paid__c = true;
}
update(siv);
// this section of code will abort the current schedule job
try {
system.abortJob(sc.getTriggerId());
} catch (exception e) {system.debug('#### schedule job exception while aborting:' + e);}
// reschedule the job
system.debug('#### schedule job executing');
scheduleNow();
}
global static void scheduleNow() {
// this section of code will schedule the next execution 1 minute from now
datetime thisTime = system.now().addHours(24);
integer minute = thisTime.minute();
integer second = thisTime.second();
integer hour = thisTime.hour();
integer year = thisTime.year();
integer month = thisTime.month();
integer day = thisTime.day();
String timeStamp = second + ' ' + minute + ' ' + hour + ' ' + day + ' ' + month + ' ? ' + year;
string jobName = 'SalesInvoicePaid';
SalesInvoicePaid p = new SalesInvoicePaid();
system.schedule(jobName, timeStamp , p);
}
}
All Answers
If you really need to have this in a batch class, first thing you will have to do is create a bacth class which will execute your operation.
with http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_batch_interface.htm
Second you will have to change your schedule class to execute the batch class, sample code below-
String query = 'SELECT Id,CreatedDate FROM Merchandise__c ' + 'WHERE Id NOT IN (SELECT Merchandise__c FROM Line_Item__c)'; CleanUpRecords c = new CleanUpRecords(query);
Database.executeBatch(c);