You need to sign in to do that
Don't have an account?
Anonymouse
How to Make Test Class More Efficient With Bulkify
Hello,
I'm currently working on a Trailhead Challenge called: Create an Apex class that uses the @future annotation to update Account records.
I do not have any errors; however, how may I make my test class more efficient with bulkifying? I wish to create anywhere from 1-200 accounts/contacts for these tests. This is my current code:
Sincerely,
Jackie
I'm currently working on a Trailhead Challenge called: Create an Apex class that uses the @future annotation to update Account records.
I do not have any errors; however, how may I make my test class more efficient with bulkifying? I wish to create anywhere from 1-200 accounts/contacts for these tests. This is my current code:
@isTest private class AccountProcessorTest { @isTest private static void testSingleAccountProcessor() { double numOfAcctsCreated = 0; // Create Account(s) Account acct = new Account(Name = 'Smith Hardward Account'); insert acct; // This is not part of test // Create and insert Contact with Account ID Contact cont = new Contact(LastName = 'Smith', AccountId = acct.Id); insert cont; // Create List of Ids to keep track of Account Ids List<Id> listOfAcctIds = new List<Id>(); // Add Account Id from Account just created into List listOfAcctIds.add(acct.Id); // Increment count on Account(s) created numOfAcctsCreated += 1; // Test Test.startTest(); AccountProcessor.countContacts(listOfAcctIds); Test.stopTest(); // Verify results Account queryResults = [SELECT Number_of_Contacts__c FROM Account WHERE id =: acct.Id]; System.assert(queryResults.Number_of_Contacts__c == numOfAcctsCreated, 'Error! Number of Contacts for Account failed. ' + 'queryResults.Number_of_Contacts__c = ' + queryResults.Number_of_Contacts__c + ' and ' + 'numOfAcctsCreated = ' + numOfAcctsCreated); } @isTest private static void testMultipleAccountProcessors() { double numOfAcctsCreated = 0; // Create Account(s) Account acct1 = new Account(Name = 'Smith Hardward Account1'); insert acct1; // This is not part of test Account acct2 = new Account(Name = 'Smith Hardward Account2'); insert acct2; // This is not part of test Account acct3 = new Account(Name = 'Smith Hardward Account3'); insert acct3; // This is not part of test // Create and insert Contact with Account ID Contact cont1 = new Contact(LastName = 'Smith1', AccountId = acct1.Id); insert cont1; Contact cont2 = new Contact(LastName = 'Smith2', AccountId = acct2.Id); insert cont2; Contact cont3 = new Contact(LastName = 'Smith3', AccountId = acct3.Id); insert cont3; // Create List of Ids to keep track of Account Ids List<Id> listOfAcctIds = new List<Id>(); // Add Account Id from Account just created into List listOfAcctIds.add(acct1.Id); listOfAcctIds.add(acct2.Id); listOfAcctIds.add(acct3.Id); // Increment count on Account(s) created numOfAcctsCreated = listOfAcctIds.size(); // Test Test.startTest(); AccountProcessor.countContacts(listOfAcctIds); Test.stopTest(); // Verify results Account queryResults1 = [SELECT Number_of_Contacts__c FROM Account WHERE id =: acct1.Id]; Account queryResults2 = [SELECT Number_of_Contacts__c FROM Account WHERE id =: acct2.Id]; Account queryResults3 = [SELECT Number_of_Contacts__c FROM Account WHERE id =: acct3.Id]; Decimal counter = queryResults1.Number_of_Contacts__c + queryResults2.Number_of_Contacts__c + queryResults3.Number_of_Contacts__c; System.assert(counter == numOfAcctsCreated, 'Error! Number of Contacts for Account failed. ' + 'counter = ' + counter + ' and ' + 'numOfAcctsCreated = ' + numOfAcctsCreated); } }Any help would be greatly appreciated.
Sincerely,
Jackie
All Answers
This is correct code
Thank you!
Sincerely,
Jackie