How to write the Schedule Apex Class with Test Class in Salesforce

This post describes about to create a Schedule Apex class with Test Class, Monitor the Scheduled Jobs, Delete the Scheduled Jobs.

Use Case:

To update the Contact records every hours after 6 minutes (like 8:06, 9:06, 10:06, etc..)

Schedule Apex Class:

global class contactUpdateFromScheduleClass implements Schedulable {

// Execute below code in developer console
// It schedules this class to run every hour after 6 minutes (like 8:06, 9:06, 10:06, etc..)
// This is one minute after the exchange rates are fetched from Open Exchange Rates.
/*
contactUpdateFromScheduleClass contactUpdate = new contactUpdateFromScheduleClass();
// Seconds Minutes Hours Day_of_month Month Day_of_week optional_year
String schedule = '0 6 * * * ?';
String jobID = System.schedule('Contact Update', schedule, contactUpdate);
*/

global void execute(SchedulableContext sc) {

//Fetch to All Contact Records
List listContact = new List([SELECT Id, FirstName, LastName, Email
FROM Contact]);
// Loop through list and update Contact Name
for (Contact con : listContact){
con.Description = con.FirstName + ' - ' + con.LastName +' Update from Schedulable Class';
}

update listContact;
}
}

How to Create Test Class for the Schedulable Apex Class?

@isTest
public class contactUpdateFromScheduleClassTest {

@isTest static void contactUpdateTest_Schedule() {

Contact cont = new Contact(FirstName ='First Name', LastName ='Last Name', Email='sakthi@theblogreaders.com');
insert cont;

// Seconds Minutes Hours Day_of_month Month Day_of_week optional_year
String CRON_EXP = '0 6 * * * ?';

Test.startTest();

String jobId = System.schedule('Update Contacts', CRON_EXP, new scheduleStoneExRates());
CronTrigger ct = [SELECT Id, CronExpression, TimesTriggered, NextFireTime FROM CronTrigger WHERE id = :jobId];
System.assertEquals(CRON_EXP, ct.CronExpression);
System.assertEquals(0, ct.TimesTriggered);

Test.stopTest();

}

}

Run Test Class From Developer Console -> Test -> New Run -> Select the Test Class

 

Schedule the Class using Developer console
Execute the following code into Developer Console, It schedules this class to run every hour after 6 minutes (like 8:06, 9:06, 10:06, etc..)
This is one minute after the exchange rates are fetched from Open Exchange Rates.

contactUpdateFromScheduleClass contactUpdate = new contactUpdateFromScheduleClass();
// Seconds Minutes Hours Day_of_month Month Day_of_week optional_year
String schedule = '0 6 * * * ?';
String jobID = System.schedule('Contact Update', schedule, contactUpdate);

Monitor Scheduled Jobs from Salesforce Setup

Setup -> Jobs -> Scheduled Jobs -> All Scheduled Jobs

 

How to Delete Scheduled Jobs?

There are two ways possible and below are the details to delete Scheduled Apex jobs
1)
Delete from Setup -> Jobs -> Scheduled Jobs -> Delete from “All Scheduled Jobs” List

2)
Delete from Developer Console and execute the (System.abortJob) following code in your salesforce org:

for(CronTrigger deleteCron: [SELECT Id FROM CronTrigger ]) {
System.abortJob(deleteCron.Id);
}

Source:  https://help.salesforce.com/articleView?id=code_schedule_batch_apex.htm&type=5

Main Class

Test Class:

 

Written by 

Salesforce MVP | Lightning Champion | 17x Salesforce Certified | Application Architect | 7x Trailhead Ranger | Techforce Services | Australia

Leave a Reply

Your email address will not be published. Required fields are marked *

*