Learn How to Write Salesforce Schedulable Apex Class and Corresponding Test Class
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.
Introduction to Schedulable Apex
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
Scheduling Apex class can either be done programmatically or from the Apex Scheduler UI.
Programmatic Scheduling / Using the System.Schedule Method
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 –
Manual Scheduling via UI
It is also possible to schedule an Apex class from the user interface by executing below steps:
- Navigate to Setup, search for Apex in the Quick Find box, then select Apex Classes.
- Click Schedule Apex.
- Enter the job name, Example – Daily Account Update.
- Click the lookup button to search Apex class and select the class.
- Select Weekly or Monthly for the frequency and set options as needed.
- Select the start and end dates, and also the preferred start time.
- Click Save.
Testing Scheduled Apex
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.
private class AccountUpdatesTest {
// 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:
- At a time, you can have only 100 scheduled Apex jobs. There are maximum number of scheduled Apex executions per a 24-hour period.
- For scheduling a class from a trigger, you should be extremely cautious. The trigger shouldn’t exceed the limit of adding more scheduled jobs.
- Only if your scheduled Apex executes a batch job, callouts are supported from the batch class. Scheduled Apex otherwise does not support Synchronous Web service callouts. For making callouts in this case, make an asynchronous callout by placing the callout in a method annotated with @future(callout=true) and call this method from scheduled Apex.
About the Author:
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
Related Blogs

The Future of Commerce is Composable
- Customer Experience

Customer Journey Transformation in Fitness Industry with Dynamics 365 Marketing and Customer Insights
- Customer Experience

Digital Experience Platforms (DXPs), the Horsepower for Businesses to Thrive
- Customer Experience

Microsoft Dynamics 365 CRM for the Insurance Sector : Unleash the Power of Your Data
- Customer Experience

Consolidating to Microsoft 365 for Increased Collaboration Productivity and Efficiency
- Customer Experience

Reimagine, Redesign and Reinvent with Microsoft Dynamics 365 Cloud Migration
- Customer Experience

An Overview of Microsoft Dynamics 365 Marketing Automation
- Customer Experience

Reimagine Customer Experience with Salesforce Implementation
- Customer Experience

Easy 5-step for successful Microsoft Dynamics 365 implementation
- Customer Experience

Propelling Customer Service to Greater Heights using Artificial Intelligence
- Customer Experience

Ready to Pursue Opportunity?
Every outcome starts with a conversation