-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathAsynchronous Apex-Schedule Jobs Using the Apex Scheduler
49 lines (43 loc) · 2.17 KB
/
Asynchronous Apex-Schedule Jobs Using the Apex Scheduler
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
Challenge 10:
Create an Apex class that uses Scheduled Apex to update Lead records.
Create an Apex class that implements the Schedulable interface to update Lead records with a specific LeadSource. Write unit tests that achieve 100% code coverage for the class. This is very similar to what you did for Batch Apex.
Create an Apex class called 'DailyLeadProcessor' that uses the Schedulable interface.
The execute method must find the first 200 Leads with a blank LeadSource field and update them with the LeadSource value of 'Dreamforce'.
Create an Apex test class called 'DailyLeadProcessorTest'.
In the test class, insert 200 Lead records, schedule the DailyLeadProcessor class to run and test that all Lead records were updated correctly.
The unit tests must cover all lines of code included in the DailyLeadProcessor class, resulting in 100% code coverage.
Run your test class at least once (via 'Run All' tests the Developer Console) before attempting to verify this challenge.
Solution:
1.DailyLeadProcessor.apxc
public class DailyLeadProcessor implements schedulable{
public void execute(schedulableContext sc) {
List<lead> l_lst_new = new List<lead>();
List<lead> l_lst = new List<lead>([select id, leadsource from lead where leadsource = null]);
for(lead l : l_lst) {
l.leadsource = 'Dreamforce';
l_lst_new.add(l);
}
update l_lst_new;
}
}
2.DailyLeadProcessorTest.apxc
@isTest
public class DailyLeadProcessorTest {
@testSetup
static void setup(){
List<Lead> lstOfLead = new List<Lead>();
for(Integer i = 1; i <= 200; i++){
Lead ld = new Lead(Company = 'Comp' + i ,LastName = 'LN'+i, Status = 'Working - Contacted');
lstOfLead.add(ld);
}
Insert lstOfLead;
}
static testmethod void testDailyLeadProcessorScheduledJob(){
String sch = '0 5 12 * * ?';
Test.startTest();
String jobId = System.schedule('ScheduledApexTest', sch, new DailyLeadProcessor());
List<Lead> lstOfLead = [SELECT Id FROM Lead WHERE LeadSource = null LIMIT 200];
System.assertEquals(200, lstOfLead.size());
Test.stopTest();
}
}