-
Notifications
You must be signed in to change notification settings - Fork 11
/
Asynchronous apex-Control Processes with Queueable Apex
72 lines (60 loc) · 3.13 KB
/
Asynchronous apex-Control Processes with Queueable Apex
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
Challenge 9:
Create an Queueable Apex class that inserts Contacts for Accounts.
Create a Queueable Apex class that inserts the same Contact for each Account for a specific state. Write unit tests that achieve 100% code coverage for the class.
Create an Apex class called 'AddPrimaryContact' that implements the Queueable interface.
Create a constructor for the class that accepts as its first argument a Contact sObject and a second argument as a string for the State abbreviation.
The execute method must query for a maximum of 200 Accounts with the BillingState specified by the State abbreviation passed into the constructor and insert the Contact sObject record associated to each Account. Look at the sObject clone() method.
Create an Apex test class called 'AddPrimaryContactTest'.
In the test class, insert 50 Account records for BillingState "NY" and 50 Account records for BillingState "CA". Create an instance of the AddPrimaryContact class, enqueue the job and assert that a Contact record was inserted for each of the 50 Accounts with the BillingState of "CA".
The unit tests must cover all lines of code included in the AddPrimaryContact 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.AddPrimaryContact.Apxc
public class AddPrimaryContact implements Queueable {
public contact c;
public String state;
public AddPrimaryContact(Contact c, String state) {
this.c = c;
this.state = state;
}
public void execute(QueueableContext qc) {
system.debug('this.c = '+this.c+' this.state = '+this.state);
List<Account> acc_lst = new List<account>([select id, name, BillingState from account where account.BillingState = :this.state limit 200]);
List<contact> c_lst = new List<contact>();
for(account a: acc_lst) {
contact c = new contact();
c = this.c.clone(false, false, false, false);
c.AccountId = a.Id;
c_lst.add(c);
}
insert c_lst;
}
}
2.AddPrimaryContactTest.apxc
@isTest
public class AddPrimaryContactTest {
@testSetup
public static void setup(){
List<account> acc_lst = new List<account>();
for (Integer i=0; i<50;i++) {
account a = new account(name=string.valueOf(i),billingstate='NY');
system.debug('account a = '+a);
acc_lst.add(a);
}
for (Integer i=0; i<50;i++) {
account a = new account(name=string.valueOf(50+i),billingstate='CA');
system.debug('account a = '+a);
acc_lst.add(a);
}
insert acc_lst;
}
public static testMethod void TestQueueable(){
List<Account> ac_ca=[select id from Account where billingstate='CA'];
contact c = new contact(lastname='bhau');
AddPrimaryContact apc = new AddPrimaryContact(c,'CA');
Test.startTest();
System.enqueueJob(apc);
Test.stopTest();
system.assertEquals(50, [select count() from contact where AccountId IN :ac_ca]);
}
}