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
hanifa fatimahanifa fatima 

How to write a test class for updating the child record?

trigger UpdateContactOnAcc2 on Account (after update) {


  for(Account a :Trigger.new)
  {
     list <contact> con = [select lastname, LeadSource  from contact where contact.accountid=:a.id];

     if(trigger.isUpdate)
      {

          
           for(Contact c: con)
           {
             c.LeadSource='Web';
             c.accountid=a.id;
             c.OtherPhone=a.Phone;
           update c;
           } 
      } 
 

  }
  }
//We Update the child record for that account whenever any field (say phone is changed) the same changes should be reflected in the child record
  //updating child record without repeating the query in the loop and running the query only once


This is the Test Class i've written
@isTest
public class TestClassUpdateAcc {
 static testmethod void updateAcc()
   {
     Account acc = new Account();
      acc.Name = 'Loader';
       insert acc;
       acc=[select name,phone from account];
       acc.Phone='98765';
       update acc;
      // acc=[select name, phone from account];
       // System.assertEquals('555', acc.Phone);

       
      Contact con = new Contact();
      con.LastName = 'xxxx';
      con.AccountId =acc.Id;
      con.OtherPhone=acc.Phone;
       con.LeadSource='email';
      insert con;
        con=[select LastName, AccountID,otherphone from contact];
        con.LeadSource='web';
         update con;
     //   System.assertEquals('Web', con.LeadSource);
       
       
 
               
   }
}

 
TSMETSME
I would create first your account and contact outside your test method, with the contact lead source as email. Then within the test method you just do an update on the account created before and test if the leadsource field on the contact changed. I'll try to create a quick example.
TSMETSME
@isTest
public class TestClassUpdateAcc {

	@testSetup static void setup() {
		Account acc = new Account();
		acc.Name = 'Loader';
		insert acc;
		
		Contact con = new Contact();
		con.LastName = 'xxxx';
		con.AccountId =acc.Id;
		con.OtherPhone=acc.Phone;
		con.LeadSource='email';
		insert con;
	}

	@isTest static void updateAcc() {
		Account acc = [SELECT Name, Phone FROM Account WHERE Name='Loader'];
		acc.Phone = '98765';
		update acc;
		Contact con=[select LeadSource from contact where AccountId =: acc.ID];
		System.assertEquals('Web', con.LeadSource);
	}
}

Something like this (quickly written, not tested, might have small typo's or errors, but you should get the point on how to test your trigger
Steven NsubugaSteven Nsubuga
I have bulkified your trigger, so that it does not fail if you update more than 100 records at a go.
trigger UpdateContactOnAcc2 on Account (after update) {
    
    if(trigger.isUpdate)
    {       
        list <contact> con = new List<contact>();
        for(Contact c: [select lastname, LeadSource, OtherPhone, Account.Phone from contact where accountid IN:trigger.NewMap.keyset()])
        {
            con.add(new contact (id = c.id, LeadSource='Web', OtherPhone=c.Account.Phone));
        }
        update con;
    }
}
Test class
@isTest
public class TestClassUpdateAcc {
    static testmethod void updateAcc()
    {
        Account acc = new Account();
        acc.Name = 'Loader';
        insert acc;
        
        Contact con = new Contact();
        con.LastName = 'xxxx';
        con.AccountId =acc.Id;
        insert con;
        
        acc=[select name,phone from account Limit 1];
        acc.Phone='98765';
        update acc;
      
        con=[select LastName, LeadSource, otherphone from contact Limit 1];
        System.assertEquals('Web', con.LeadSource);
        System.assertEquals('98765', con.otherphone);      
                      
   }
}


 
TSMETSME
Good point on the bulk update in the trigger! Missed that part, just focussed on test class example
Akshay_DhimanAkshay_Dhiman
Hi Pradeep Musthi,
 
@isTest
public class TestAccount {

 @testSetup static void setup() {
  Account acc = new Account();
  acc.Name = 'Test Acc';
  insert acc;
  
  Contact con = new Contact();
  con.LastName = 'child Contact';
  con.AccountId =acc.Id;
  con.OtherPhone=acc.Phone;
  con.LeadSource='web';
  insert con;
 }

 @isTest static void accUpdate() {
  Account acc = [SELECT Name, Phone FROM Account WHERE Name='Test Acc'];
  acc.Phone = '8755666321';
  update acc;
  Contact con=[select LeadSource from contact where AccountId =: acc.ID];
  
 }
}


  if you found this answer helpful then please mark it as best answer so it can help others.   
  
  Thanks 
  Akshay