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
lisa.sturycz1.3890322391892622E12lisa.sturycz1.3890322391892622E12 

Test class help

I wrote the following trigger to populate a summary Privileges field on the Contact object if any one or more of the related Affiliation (child) records has the Privileges checkbox checked on it. It appears to be working fine in sandbox. Now I am wondering how to go about creating a test class for it. Is anyone able to assist?

trigger SummarizePrivileges on Affiliation__c (after insert, after update, after delete) {
  // get the contacts
  Set<Id> contactids=new Set<Id>();
  Map<Id, Affiliation__c> AffilMap;

  // figure out which map is populated
  //When adding new affil or updating existing affiliations
  if(trigger.isInsert || trigger.isUpdate){
    for(Affiliation__c a : trigger.new){
      ContactIds.add(a.Provider__c);
    }
  }
 
  //When deleting affiliations
  if(trigger.isDelete){
    for(Affiliation__c a : trigger.old){
      ContactIds.add(a.Provider__c);
    }
  }

  // now retrieve the contacts and all of their associated affiliations, so that we can figure out the correct value for the contact
  List<Contact> toUpdate=new List<Contact>();
  for (Contact cont : [select id, Privileges__c, (select id, Privileges__c from Affiliations__r) from Contact where id in :contactIds])
  {
    // iterate the affiliations looking for privileges values that require the Privileges flag to be set on the contact
    boolean checkPriv=false;
    for (Affiliation__c cAffil : cont.Affiliations__r)
    {
       if (cAffil.Privileges__c==TRUE)
       {
           checkPriv=true;
           break;       // no need to continue with the inner loop as we know we need to check the Privileges box on the contact
       }
    }

    // is the result different to the contact record?
    if (checkPriv!=cont.Privileges__c)
    {
       cont.Privileges__c=checkPriv;
       toUpdate.add(cont);
    }
   
  }

  if (!toUpdate.isEmpty())
  {
    update toUpdate;
  }
}
magicforce9magicforce9
Hi,

Here is the test class......
------------------------------------------------------------------
@isTest
private class Test_SummarizePrivilegesTrigger {
   
    static testMethod void testShouldPass() {

                  //You might also need to populate other fields for Account record if required
                  Account acc = new Account();
                  acc.Name = 'TestAccount';
                  insert acc;
 
     Contact con = new Contact();
     con.FirstName = 'SomeFirstName';
     con.LastName = 'SomeLastName';
     con.AccountId = acc.id;
     //You might also need to populate other fields for contact record if
     //they are required or has a validation rule before you insert the contact
     insert con;

     Affiliation__c a1 = new Affiliation__c();
     a1.Contact__c = con.id;
     //Again You might also need to populate other fields for this object if they are required
     insert a1;
     Affiliation__c a2 = new Affiliation__c();
     a2.Contact__c = con.id;
     a2.Privileges__c = True;

     //By this insert is should execute most of your trigger statements and since we set
     //Privileges__c = True, it should also set the same on contact.
     insert a2;

     //Now Lets check if it has done what it was suppose to do
     Contact c = [Select Privileges__c from Contact where id = :con.id];
     System.assert(c.Privileges__c);

    }
}