function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
LStraubLStraub 

Help creating a test method for my Trigger

I needed a way to count the number of Contacts on an Account that have ScarbBooklet check box marked. I could not use a Roll-up summary but I did find some Apex code that I modified and it works in my Sandbox.

 

However I am not a developer and dont know how to right code at all, so I have been looking at sample test classes but I can not find one I can decypher enough to modify to as a test for my particular trigger.  

 

I was wondering if any of you devs have a few minutes to look at my trigger and point me to a bit of test code that would be very obvious that i could modify?   Thanks in advance for any help!  --Laura

 

 

/* Provide summary of Number of Scarb Booklet Contacts on Account record */ trigger ContactSumTrigger on Contact (after delete, after insert, after undelete, after update) { Contact[] cons; if (Trigger.isDelete) cons = Trigger.old; else cons = Trigger.new; // get list of accounts Set<ID> acctIds = new Set<ID>(); for (Contact con : cons) { acctIds.add(con.AccountId); } Map<ID, Contact> contactsForAccounts = new Map<ID, Contact>([select Id,AccountId from Contact where AccountId in :acctIds and Scarborough_Booklet__c=True]); Map<ID, Account> acctsToUpdate = new Map<ID, Account>([select Id,Num_of_Scarb_Booklet_Contacts__c from Account where Id in :acctIds]); for (Account acct : acctsToUpdate.values()) { Set<ID> conIds = new Set<ID>(); for (Contact con : contactsForAccounts.values()) { if (con.AccountId == acct.Id) conIds.add(con.Id); } if (acct.Num_of_Scarb_Booklet_Contacts__c != conIds.size()) acct.Num_of_Scarb_Booklet_Contacts__c = conIds.size(); } update acctsToUpdate.values(); }

 

 

 

MiddhaMiddha

Hi Laura,

 

 Test Coverage:

 

 

public class TestClassContact { public static testMethod void testContactSum() { Account a = new Account(Name='Test Account'); insert a; Contact c1 = new Contact(LastName='Last', email='test@testemail.com',AccountId=a.Id,Scarborough_Booklet__c=true); Contact c2 = new Contact(LastName='Last2', email='test2@testemail.com',AccountId=a.Id,Scarborough_Booklet__c=true); Contact c3 = new Contact(LastName='Last3', email='test3@testemail.com',AccountId=a.Id,Scarborough_Booklet__c=false); insert c1; a = [Select Num_of_Scarb_Booklet_Contacts__c from Account where Id=:a.Id]; System.assertEquals(a.Num_of_Scarb_Booklet_Contacts__c, 1); insert c2; a = [Select Num_of_Scarb_Booklet_Contacts__c from Account where Id=:a.Id]; System.assertEquals(a.Num_of_Scarb_Booklet_Contacts__c, 2); insert c3; a = [Select Num_of_Scarb_Booklet_Contacts__c from Account where Id=:a.Id]; System.assertEquals(a.Num_of_Scarb_Booklet_Contacts__c, 2); delete c2; a = [Select Num_of_Scarb_Booklet_Contacts__c from Account where Id=:a.Id]; System.assertEquals(a.Num_of_Scarb_Booklet_Contacts__c, 1); }}

 

 

 

 

Ispita_NavatarIspita_Navatar

Test Methods are written to ensure that the data being created in them traverses each if-else conditions and loops in a given functionality, so that your code is covered. In your simply adding the code for:-


1. Insert of contact with the value of the Scard Booklet to true and false
2. Update of Contact
3. Deletion of Contact


Please refer to the code below:-


@isTestprivate class ClassTesting
 {
  //Added for security review
  public static testMethod void myUnitTestQfp()
 {        // TO DO: implement unit test
Account inst = new Account(RecordTypeId = RecInst,Familiar_Name__c='@isTest1',Name='@isTest1');        insert inst;
 Contact con1 = new Contact(LastName = 'Xyz@isTest' ,AccountId = inst.Id,Scarborough_Booklet__c=True);
  insert con1;
Contact con2 = new Contact(LastName = 'Abc@isTest' ,AccountId = inst.Id,Scarborough_Booklet__c=False);
  insert con1;
con1.Scarborough_Booklet__c=False;
  update con1;
con2.Scarborough_Booklet__c=True;
  update con2;
delete con1;
delete con2;
}

}


Hope this is helpful.