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
Ramana123Ramana123 

how to cover the test class for below trigger

 Highlighed part is not covering in the test class--- Helper class 

public  class  TriggerHelperClass {
   public Set <Id> accountIds = new Set <Id>();
   public List <Account> lstAccountsToUpdate = new List<Account>();
    // for insertion and deletion
   public void insertAndUndelete(List<Contact> newAcc)
       {
         for(Contact con:newAcc)
              {
                 accountIds.add(con.accountID);
             }
        this.updatingContactsCount();
    }   
    // for delete operation
   public void deleteOperation(List<Contact> newAcc)
      {
           for(Contact con:newAcc)
                {
                   accountIds.add(con.accountID);
             } 
         this.updatingContactsCount();
      }              

    public void updateOperation(List<Contact> newAcc,Map<Id,Contact>oldMap)
       {
           for(Contact con : newAcc)
               {
                      if(con.AccountId != null)
                           {
                              if(oldMap.get(con.Id).AccountId != con.AccountId)
                                  {
                                    accountIds.add(con.AccountId);     
                               }
                          } 
                     accountIds.add(oldMap.get(con.Id).AccountId);    
               }     
           this.updatingContactsCount();
       }          

    Public void updatingContactsCount()
        {
           for(Account acc:[SELECT Id,Name,Count_Contacts__c,Total_Sum__c,(Select Id,Sum__c from Contacts) from Account where Id IN: accountIds])
                   {
                       acc.Count_Contacts__c = acc.Contacts.size();
                     acc.Total_Sum__c = 0;  
                     for(Contact con : acc.Contacts)
                         {
                            if(con.Sum__c != NULL)
                               {
                                   acc.Total_Sum__c = acc.Total_Sum__c + con.Sum__c;  
                             }   
                       }
                     lstAccountsToUpdate.add(acc);
                      }   
         UPDATE lstAccountsToUpdate;    
      }
}




test class


@isTest
private class UpdatingAccountRelatedContactsCount_Test {
   
 static Testmethod void accountRelatedContacts()
    {
    List<Contact> conList =  new List<Contact>();
    Account Acc = New Account();
    Acc.Name = 'srikanth';
    insert Acc;    
           
    Contact con = new Contact();
    con.LastName = 'Test';
    con.AccountId = Acc.Id;
    con.Sum__c = 100;
    conList.add(con);       
        
    Contact con1 = new Contact();
    con.LastName = 'Test1';
    con1.AccountId = Acc.Id;
    con1.Sum__c = 100;
    conList.add(con1);       
     
    insert conList;
    delete con1;      
    Test.StartTest();
    TriggerHelperClass obj = new TriggerHelperClass(); 
    
    update conList ;
    undelete con1;
    Test.StopTest();
   
    }
}
 
AnudeepAnudeep (Salesforce Developers) 
Hi Srikanth, 

Can you please try adding the following lines to your test class?
 
@isTest
private class UpdatingAccountRelatedContactsCount_Test {
   
 static Testmethod void accountRelatedContacts()
    {
    List<Contact> conList =  new List<Contact>();
    Account Acc = New Account();
    Acc.Name = 'srikanth';
    insert Acc;    
           
    Contact con = new Contact();
    con.LastName = 'Test';
    con.AccountId = Acc.Id;
    con.Sum__c = 100;
    conList.add(con);       
        
    Contact con1 = new Contact();
    con.LastName = 'Test1';
    con1.AccountId = Acc.Id;
    con1.Sum__c = 100;
    conList.add(con1);       
     
    insert conList;
    delete con1;      
    Test.StartTest();


Map<Id,Contact> accMap = new Map<Id,Contact>();
for(Contact con : [Select id from Contact]){
  accMap.add(con.id,con);
}


    TriggerHelperClass obj = new TriggerHelperClass(); 
    
    obj.deleteOperation(conList);
    obj.updateOperation(conList, accMap);
    update conList;
    undelete con1;
    Test.StopTest();
   
    }
}

Let me know if this helps, if it does, please mark this answer as best so that others facing the same issue will find this information useful. Thank you

 
Ramana123Ramana123
Hi Anudeep Thanks for the help. But it is not working .
Ramana123Ramana123
System.DmlException: Insert failed. First exception on row 1; first error: REQUIRED_FIELD_MISSING, Required fields are missing: [LastName]: [LastName]
AnudeepAnudeep (Salesforce Developers) 
Please also query LastName when querying contact
 
for(Contact con : [Select id, LastName from Contact]){

Let me know if this fixes your issue and gives you any coverage?