Skip to content

Instantly share code, notes, and snippets.

@mannharleen
Created November 2, 2016 15:46
Show Gist options
  • Save mannharleen/8c2df70927777d1d5a27b64dd4d4a61b to your computer and use it in GitHub Desktop.
Save mannharleen/8c2df70927777d1d5a27b64dd4d4a61b to your computer and use it in GitHub Desktop.
Salesforce trailhead - Asynchronous Apex Controlling Processes with Queueable Apex
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;
}
}
@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);
}
}
Copy link

ghost commented Oct 16, 2018

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?

@orlandotv
Copy link

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.

@max-goldfarb
Copy link

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);

@Amanhas2492
Copy link

Amanhas2492 commented Jun 6, 2020

He's is asserting the number of contacts with the billing state of CA.
AddPrimaryContact apc = new AddPrimaryContact(c,'CA'); -> While instantiating the AddPrimaryContact Class he's not considering the accounts with the billing state NY.

@sushilatrahe2010
Copy link

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);
}

@Shoubhik14
Copy link

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!

@varunjanne
Copy link

The 'AddPrimaryContactTest' test class doesn't appear to be using the 'AddPrimaryContact' class. iam getting this as error

@Ravichandran2004
Copy link

(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