This website uses cookies. By continuing to browse the site, you are agreeing to our use of cookies
Customer Experience
June 18, 2018
Salesforce provides different ways of processing asynchronously and it is typically used in scenarios for callouts to external systems, operations that require higher limits, and code that needs to run at a certain time. Processes can be later run in a separate thread, using Asynchronous Apex. It comes in multiple flavors like future methods, batch apex, queueable apex and scheduled apex. This blog explores how to write schedulable apex class and corresponding test class.
The apex scheduler provides a medium to run apex classes at specific times. This is an ideal way for scheduling maintenance tasks that needs to be executed on a daily or weekly basis. Apex scheduler can be used to schedule any Apex class that implements Schedulable interface. Schedulable interface makes it mandatory to implement execute method – the only method that this interface contains.
Example of such class –
Basic structure of Apex class with Schedulable interface:
Scheduling Apex class can either be done programmatically or from the Apex Scheduler UI.
System.Schedule method allows scheduling Apex class programmatically. Three input parameters are considered in this method: CRON expression for time and date the job schedule, name for the job, and the name of class.
Code to schedule an Apex class programmatically –
It is also possible to schedule an Apex class from the user interface by executing below steps:
For a Scheduled Apex test it is necessary that the scheduled job is finished before testing against the results. To do this, use startTest and stopTest again around the System.schedule method, to ensure that the processing finishes before continuing your test.
Below is the structure of a test class for testing Scheduled Apex. It’s important to note that as soon as stopTest method finishes execution, the job is finished synchronously, to ensure the test results can be validated immediately after stopTest.
// Dummy CRON expression: midnight on Feb 28.
// Because this is a test, job executes
// immediately after Test.stopTest().
public static String CRON_EXP = ‘0 0 0 28 2 ? 2022’;
static testmethod void testScheduledJob() {
// Create Test Data
Test.startTest();
// Schedule the test job
String jobId = System.schedule(‘Account Updates’’,
CRON_EXP,
new AccountUpdates());
// Key thing to Note: Stopping the test will run the job synchronously
Test.stopTest();
// Now that the scheduled job has executed,
//Use System.assertEquals to confirm test success
}
}
From the myriad items available in Scheduled Apex, you should ensure to bear in mind the following information:
Niraj has extensive experience in Enterprise CRM applications, solution design, architecture and system integrations. 3x Salesforce Certified Professional, AWS Certified Cloud Practitioner and playing role of an application architect in his current assignment.
Niraj Wani
For more insights please feel free to connect with us on marketing@hexaware.com.
About the Author
Niraj Wani
Read more
Every outcome starts with a conversation