18 Jun 2018
3 MINS READ
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
(nirajw@hexaware.com)
For more insights please feel free to connect with us on marketing@hexaware.com.
About the Author
Customer Experience Transformation, Healthcare
30 Mar 2020
Customer Experience Transformation
26 Jul 2019
18 Aug 2018
28 Jun 2018
BI & Analytics
19 Mar 2009
26 Nov 2010
Digital Assurance
02 Jan 2012
17 Feb 2012
Infrastructure Mgmt. Services
02 Mar 2012
03 Jan 2013
04 Feb 2013
06 Feb 2013
Digital Assurance, Enterprise Solutions
14 Feb 2013
18 Feb 2013
21 Feb 2013
27 Feb 2013
Others
01 Mar 2013
04 Mar 2013
Enterprise Solutions
05 Mar 2013
18 Mar 2013
Digital Assurance, Enterprise Solutions, Others
22 Mar 2013
12 Apr 2013
26 Apr 2013
29 Apr 2013
13 May 2013
11 Jun 2013
17 Jun 2013
25 Jun 2013
19 Aug 2013
26 Aug 2013
27 Aug 2013
03 Sep 2013
10 Sep 2013
19 Sep 2013
24 Sep 2013
26 Sep 2013
30 Sep 2013
01 Oct 2013
03 Oct 2013
01 Nov 2013
19 Nov 2013
Enterprise Solutions, Manufacturing and Consumer
28 Nov 2013
03 Dec 2013
20 Dec 2013
03 Jan 2014
22 Jan 2014
27 Jan 2014
31 Jan 2014
12 Feb 2014
13 Feb 2014
20 Mar 2014
24 Mar 2014
17 Apr 2014
11 Jun 2014
Manufacturing and Consumer
26 Jun 2014
30 Jun 2014
10 Jul 2014
15 Jul 2014
16 Jul 2014
18 Jul 2014
28 Oct 2014
13 Jul 2015
06 Aug 2015
26 Aug 2015
28 Sep 2015
07 Oct 2015
26 Oct 2015
07 Mar 2016
22 Mar 2016
13 May 2016
23 May 2016
Application Transformation Mgmt.
11 Jul 2016
25 Aug 2016
03 Sep 2016
14 Sep 2016
15 Nov 2016
22 Nov 2016
25 Nov 2016
Business Process Services
25 Apr 2017
Banking and Financial Services
18 May 2017
30 May 2017
23 Jun 2017
27 Jun 2017
18 Jul 2017
26 Oct 2017
Healthcare, Insurance
28 Nov 2017
11 Dec 2017
25 Jan 2018
21 Feb 2018
( Mandatory field * )
The information you provide will be used in accordance with our terms ofPrivacy Policy
Please Check on "I Agree" to register for the blog.