You need to sign in to do that
Don't have an account?
hanifa fatima
This is the Test Class i've written
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); } }
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
if you found this answer helpful then please mark it as best answer so it can help others.
Thanks
Akshay