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
sriram k 15sriram k 15 

Can someone help for Test Coverage for the given trigger?

trigger Count_Contact_Records on Contact (after insert,after update,after delete,after undelete) {   
   
 
    set<id> accIds = new set<id>();
    if(trigger.isafter && (trigger.isinsert || trigger.isupdate || trigger.isundelete)){
        for(contact con : Trigger.new){
            if(con.accountid != null){
                accIds.add(con.accountid);
            }
        }
    }
    if(trigger.isafter && (trigger.isupdate || trigger.isdelete)){
        for(contact con : Trigger.old){
            if(con.accountid != null){
                accIds.add(con.accountid);
            }
        }
    }
    if(accIds != null){
      list<Account> lstaccs =[select id,name,Total_child_records__c,(select id, name from contacts) from Account where id in :accIds];
        for(account acc : lstaccs)
        {
            acc.Total_child_records__c = acc.contacts.size();
        }
        update lstaccs;
    
    }



        
}
Best Answer chosen by sriram k 15
CharuDuttCharuDutt
Hii Sriram
Try Below Code 100% Coverage
@isTest
public class NumberOfChildTest {
    @isTest
    public Static Void UnitTest(){
        list<Contact>lstCon = new list<Contact>();
        Account Acc = new account();
        Acc.name ='test';
        insert Acc;
        
        Account Acc2 = new account();
        Acc2.name ='test2';
        insert Acc2;
        
      
        
        Contact Con = new Contact();
        Con.LastName = 'TestCon';
        Con.AccountId = Acc.Id;
        Insert Con;
       
        Con.AccountId = Acc2.Id;
        Update Con;
        Delete Con;
        
        
        
    }
}
Please Mark It As Best Asnwer If It Helps
Thank You!