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
jayesh pagarejayesh pagare 

Unable to cover test coverage its only 33% for trigger after update contact

here is the trigger which update contact phone details on opportunity record aftter update.Please help me to write test class.
Apex Trigger:
trigger ContactAfterUpdate on Contact (after update) {
  // Update information on opportunity when Contact info is updaed.
  Set<Id> ContactIds = new Set<Id>();
  for(Contact c: Trigger.new){
    Contact oldC = Trigger.oldMap.get(c.Id);
    if(c.Fax!=oldC.Fax ||c.Phone!=oldC.Phone||c.MobilePhone!=oldC.MobilePhone){
      ContactIds.add(c.Id);
    }
  }

  if(ContactIds.size()>0){
    List<Opportunity> opps = [select Contact_Phone__c,Contact_Fax__c,Contact_Mobile__c,Contact_Name__c from Opportunity where Contact_Name__c IN :ContactIds];
    if(opps.size()>0){
      for(Opportunity o: opps){
        Contact adj = Trigger.newMap.get(o.Contact_Name__c);
        o.Contact_Phone__c = adj.Phone;
        o.Contact_Fax__c = adj.Fax;
        o.Contact_Mobile__c = adj.MobilePhone;
      } 
      update opps;
    }
  }
}

Test Class:
@isTest
private class AfterUpdateContactTest{
public static testMethod void AfterUpdateContactStaticTestMethod()
{
Account a = new Account();
a.Name = 'Test ';

insert a;
 
  Contact aa =new Contact (LastName='TestAss',Title='ASSID' ,FirstName='AssFName',MobilePhone='0919182320',Phone='02002020',Fax='xyz');
  aa.accountid=a.id;
  insert aa;

  List<Opportunity>oppList = new List<Opportunity> {new Opportunity(Name='TestUser',Contact_Name__c =aa.id,AccountId= a.id  )};   
  insert oppList;

  List<Opportunity> oppList1 = new List<Opportunity>{ [select id,Contact_Phone__c,Contact_Fax__c,Contact_Mobile__c,Contact_Name__c from Opportunity where id in :oppList ]};
  for(Opportunity ppdd:oppList1)  
    ppdd.Contact_Phone__c = aa.Phone;    

    
  update oppList1;  
  update aa;
  }
}
Best Answer chosen by jayesh pagare
Amit Chaudhary 8Amit Chaudhary 8
Pleasse update your test class like below
@isTest
private class AfterUpdateContactTest{
	public static testMethod void AfterUpdateContactStaticTestMethod()
	{
		Account a = new Account();
		a.Name = 'Test ';
		insert a;

		Contact aa =new Contact (LastName='TestAss',Title='ASSID' ,FirstName='AssFName',MobilePhone='0919182320',Phone='02002020',Fax='xyz');
		aa.accountid=a.id;
		insert aa;

		List<Opportunity>oppList = new List<Opportunity> {new Opportunity(Name='TestUser',Contact_Name__c =aa.id,AccountId= a.id  )};   
		insert oppList;

		aa.Phone='01002020'
		update aa;
		
	}
}
Let us know if this will help you
 

All Answers

Amit Chaudhary 8Amit Chaudhary 8
Pleasse update your test class like below
@isTest
private class AfterUpdateContactTest{
	public static testMethod void AfterUpdateContactStaticTestMethod()
	{
		Account a = new Account();
		a.Name = 'Test ';
		insert a;

		Contact aa =new Contact (LastName='TestAss',Title='ASSID' ,FirstName='AssFName',MobilePhone='0919182320',Phone='02002020',Fax='xyz');
		aa.accountid=a.id;
		insert aa;

		List<Opportunity>oppList = new List<Opportunity> {new Opportunity(Name='TestUser',Contact_Name__c =aa.id,AccountId= a.id  )};   
		insert oppList;

		aa.Phone='01002020'
		update aa;
		
	}
}
Let us know if this will help you
 
This was selected as the best answer
jayesh pagarejayesh pagare
Thanks a lot Amit for quick reply..it worked :)