Created
November 2, 2016 15:46
-
-
Save mannharleen/8c2df70927777d1d5a27b64dd4d4a61b to your computer and use it in GitHub Desktop.
Salesforce trailhead - Asynchronous Apex Controlling Processes with Queueable Apex
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@IsTest | |
public class AddPrimaryContactTest { | |
@IsTest | |
public static void testing() { | |
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; | |
Test.startTest(); | |
contact c = new contact(lastname='alex'); | |
AddPrimaryContact apc = new AddPrimaryContact(c,'CA'); | |
system.debug('apc = '+apc); | |
System.enqueueJob(apc); | |
Test.stopTest(); | |
List<contact> c_lst = new List<contact>([select id from contact]); | |
Integer size = c_lst.size(); | |
system.assertEquals(50, size); | |
} | |
} |
I need to ask you one simple question about this, In your test class, why have you used two looping variables instead of just one?
I think it is because the first loop creates contacts with BillingState with NY and the second one with CA and validates that the assertEquals just count the size of the CA billingState.
But isn't he just asserting that there are 50 contacts? Wouldn't the following be more appropriate?
Integer listSize = [SELECT count() FROM Contact WHERE Contact.Account.BillingState = 'CA']; system.assertEquals(50, listSize);
This worked well!
The 'AddPrimaryContactTest' test class doesn't appear to be using the 'AddPrimaryContact' class. iam getting this as error
(System Code)
Class.AccountProcessorTest.TestAccountProcessorTest: line 36, column 1
Class.AddPrimaryContactTest.testing: line 17, column 1
i am getting this error
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
It can be optimized like this.
for (Integer i=0; i<50;i++) {
account a1 = new account(name='TestCon'+i,billingstate='NY');
acc_lst.add(a1);
account a2 = new account(name='TestCon1'+i,billingstate='CA');
acc_lst.add(a2);
}