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
BobBob 

Help with Apex test method for contacts

I have a trigger that i need a test class for but I am limited in my apex test writting. Can someone help me with a test method for the fllowing code below? I would really appreciate it.

 

trigger Trigger_ContactBouncedEmailBeforeUpdate on Contact (before update)
{
    
 //Assign the context before and after the change into a Map
    Map<Id,Contact> newContactMap = Trigger.newMap;
    Map<Id,Contact> oldContactMap = Trigger.oldMap;
    
    //Loop through the map
    for(Id contactId:newContactMap.keySet()){
        Contact myNewContact = newContactMap.get(contactId);
        Contact myOldContact = oldContactMap.get(contactId);
        if (myNewContact.EmailBouncedReason <> myOldContact.EmailBouncedReason){
            //if Salesforce is modifying these fields we want to reset are
            myNewContact.IsEmailBounced__c = false;
            myNewContact.EmailBouncedReason__c = null;
            myNewContact.EmailBouncedDate__c = null;
        }
        if (myNewContact.IsEmailBounced__c <> myOldContact.IsEmailBounced__c){
            if(myNewContact.IsEmailBounced__c == true)
            {
                myNewContact.EmailBouncedDate = myNewContact.EmailBouncedDate__c;
                myNewContact.EmailBouncedReason = myNewContact.EmailBouncedReason__c ;
            }
            if(myNewContact.IsEmailBounced__c == false)
            {
                myNewContact.EmailBouncedDate = null;
                myNewContact.EmailBouncedReason = null;
                myNewContact.EmailBouncedReason__c = null;
                myNewContact.EmailBouncedDate__c = null;
            }
        }
    }

}

Best Answer chosen by Admin (Salesforce Developers) 
Naidu PothiniNaidu Pothini
@isTest
public class ContactTriggerTest
{
  static testMethod void CTTest1()
  {
    test.startTest();

      Account acc = new Account(---- required fields ----);
      insert acc;

      Contact con = new Contact(LastName = 'CL Name', AccountId = acc.Id, -- required fields----);
      insert con;

      con.IsEmailBounced__c = true;
      con.EmailBouncedReason = 'test';
      update con;

      con.IsEmailBounced__c = false;
      update con.

    test.stopTest();
  }
}

 try this.