Apex Scheduler
In order to schedule your apex class, Salesforce provides a Standard feature named Apex Scheduler.
To take benefits of Apex Scheduler first Write an Apex class that implements the Schedulable interface, and then schedule it for execution on specified time.
global class ClassName implements Schedulabe{
global void execute(SchedulableContext SC){
// here your logic goes
}
}
Schedulable Interface contains only execute method with return type void and takes SchedulableContext Object as an argument.
System.schedule() method takes three arguments, first one is a name for the job, second is CRON Expression that represents date and time on which job is scheduled and last is an instance of apex class. This method returns a job Id of String type.
String jobId = System.schedule('JobName', Cron_Exp, new ClassName());
Here is an example that uses Schedule Apex to update Lead Records.
Apex Class--
global class DailyLeadProcessor implements Schedulable{
global void execute(SchedulableContext SC){
List<Lead> leads = [select Id,LastName,LeadSource From Lead where LeadSource=null LIMIT 200];
List<Lead> listOfUpdates = new List<Lead>();
if(!leads.isEmpty()){
for(Lead l : leads){
l.LeadSource = 'Dreamforce';
listOfUpdates.add(l);
}
update listOfUpdates;
}
System.debug('Total Records are :'+ listOfUpdates.size());
}
}
Test Class--
@isTest
public class DailyLeadProcessorTest{
public static testMethod void positiveScenario(){
String Cron_Exp = '0 0 1 * * ?';
List<Lead> leadList = new List<Lead>();
for(Integer i=0; i<200; i++){
leadList.add(new Lead(LastName = 'Mayank'+i,Company = 'Dell',Status = 'Working'));
}
insert leadList;
Test.startTest();
System.schedule('DailyLeadProcessor',Cron_Exp,new DailyLeadProcessor());
Test.stopTest();
List<Lead> checkList = [select Id,LeadSource from Lead where Id =:leadList];
List<Lead> updatedList = new List<Lead>();
for(Lead l : checkList){
if(l.LeadSource == null){
l.LeadSource = 'Dreamforce';
updatedList.add(l);
}
}
update updatedList;
System.assertEquals(200,checkList.size());
}
}
To take benefits of Apex Scheduler first Write an Apex class that implements the Schedulable interface, and then schedule it for execution on specified time.
Scheule Apex Syntax--
To run at the specific time your Apex class have to implement the Schedulable interface and then schedule an instance of your class to run at the specific time using System.schedule() method.
global void execute(SchedulableContext SC){
// here your logic goes
}
}
Schedulable Interface contains only execute method with return type void and takes SchedulableContext Object as an argument.
System.schedule() method takes three arguments, first one is a name for the job, second is CRON Expression that represents date and time on which job is scheduled and last is an instance of apex class. This method returns a job Id of String type.
String jobId = System.schedule('JobName', Cron_Exp, new ClassName());
Here is an example that uses Schedule Apex to update Lead Records.
Apex Class--
global class DailyLeadProcessor implements Schedulable{
global void execute(SchedulableContext SC){
List<Lead> leads = [select Id,LastName,LeadSource From Lead where LeadSource=null LIMIT 200];
List<Lead> listOfUpdates = new List<Lead>();
if(!leads.isEmpty()){
for(Lead l : leads){
l.LeadSource = 'Dreamforce';
listOfUpdates.add(l);
}
update listOfUpdates;
}
System.debug('Total Records are :'+ listOfUpdates.size());
}
}
Test Class--
@isTest
public class DailyLeadProcessorTest{
public static testMethod void positiveScenario(){
String Cron_Exp = '0 0 1 * * ?';
List<Lead> leadList = new List<Lead>();
for(Integer i=0; i<200; i++){
leadList.add(new Lead(LastName = 'Mayank'+i,Company = 'Dell',Status = 'Working'));
}
insert leadList;
Test.startTest();
System.schedule('DailyLeadProcessor',Cron_Exp,new DailyLeadProcessor());
Test.stopTest();
List<Lead> checkList = [select Id,LeadSource from Lead where Id =:leadList];
List<Lead> updatedList = new List<Lead>();
for(Lead l : checkList){
if(l.LeadSource == null){
l.LeadSource = 'Dreamforce';
updatedList.add(l);
}
}
update updatedList;
System.assertEquals(200,checkList.size());
}
}
Comments
Post a Comment