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
Mariam Ibrahim 10Mariam Ibrahim 10 

Test Class to a update parent object record based on a child object record

Hi guys,
I have written the following test class to for the apex code below, but the code coverage is only 50%. Apparently the parent object record is not updated based on the code.
Below is the  apex code and the test class:
trigger SumTotalMonths on TargetX_SRMb__Family_Relationship__c (after insert,after update,after delete,after undelete) 
{
Map<ID,TargetX_SRMb__Application__c> applicationMap = new Map<ID,TargetX_SRMb__Application__c>();
    List<ID> appID = new List<ID>();
    
    //check for the DML operation, for Insert,update and undelete
    if(Trigger.IsInsert || Trigger.IsUpdate || Trigger.IsUndelete){
        for(TargetX_SRMb__Family_Relationship__c Rel1 : Trigger.new) {
            
            //Add all new  AccountID in the list
            if(Rel1.TargetX_SRMb__Application__c != NULL && Rel1.Total_Months_in_this_Position__c != NULL)
                appID.add(Rel1.TargetX_SRMb__Application__c);
        }
    }
     // check for DML operation for update and delete
    if(Trigger.IsDelete) {
        for(TargetX_SRMb__Family_Relationship__c Rel2 : Trigger.old)
        {
            if(Rel2.TargetX_SRMb__Application__c != NULL && Rel2.Total_Months_in_this_Position__c != NULL)
              appID.add(Rel2.TargetX_SRMb__Application__c);
      }
    }  
   // check if application list is not empty

    if(appID != NULL && appID.size()>0){
        // Add all the applications in the map to map IDs with total months employed
        for(ID ApplicationID : appID) {
           applicationMap.put(ApplicationID, 
          new TargetX_SRMb__Application__c(ID=ApplicationID,Total_Months_Employed__c= 0) ) ;
      } }
   // Calculate the total months employed based on value in the total months in this position
    for(TargetX_SRMb__Family_Relationship__c Rel : [ SELECT ID,TargetX_SRMb__Application__c , Total_Months_in_this_Position__c
                                                   FROM  TargetX_SRMb__Family_Relationship__c WHERE TargetX_SRMb__Application__c  IN :appID ]) 
     
     {applicationMap.get(Rel.TargetX_SRMb__Application__c).Total_Months_Employed__c += Rel.Total_Months_in_this_Position__c;
                          }
    
    //commit to the database
    Database.update(applicationMap.values());
}

Test Class:
@isTest(seealldata=false)
public class GetTotalMonthsWorked {
    static testMethod void CalcMonths(){
        
     //create new Account
     Account acc = new Account(Name= 'Unassigned Contacts', Industry = 'Education'); 
     insert acc;
     
   // Create new contact
    Contact c = new Contact(FirstName= 'Sophie',LastName= 'Test', Accountid = acc.id, Gender__c  = 'F', Email= 'Testset@test.com');
        insert c;
   // Create new application
   TargetX_SRMb__Application__c app = new TargetX_SRMb__Application__c(TargetX_SRMb__Contact__c = c.id,TargetX_SRMb__Stage__c = 'In Progress',
                                                                      TargetX_SRMb__Status__c= 'Incomplete');
    insert app;
        
   // Create list of relationships
   List<TargetX_SRMb__Family_Relationship__c> RelList = new List <TargetX_SRMb__Family_Relationship__c>();
        
        for( integer i=0; i<200; i++)
        {
            
         RelList.add(new TargetX_SRMb__Family_Relationship__c (Name = 'TestRel' + i, TargetX_SRMb__Contact__c = c.id,
                 End_Date__c = date.ValueOf('2018-03-02'), Start_Date__c = date.ValueOf('2016-03-02'))); 
            
        }       
   insert RelList;
        //confirm if the total months worked is updated.
        List<TargetX_SRMb__Family_Relationship__c> RelMonths =new List<TargetX_SRMb__Family_Relationship__c>
            ([SELECT Total_Months_in_this_Position__c  FROM TargetX_SRMb__Family_Relationship__c
              WHERE Id= :app.id limit 1]);
        system.assertEquals(200, RelMonths[0].Total_Months_in_this_Position__c);
        
        //list all the application ids
        List<TargetX_SRMb__Application__c> IDs= new List<TargetX_SRMb__Application__c>
            ([SELECT Total_Months_Employed__c FROM TargetX_SRMb__Application__c 
             WHERE id = :app.id limit 1]);
        
    
    // update relationship record
    List<TargetX_SRMb__Family_Relationship__c> RelList2 = new List<TargetX_SRMb__Family_Relationship__c>();
        for ( integer i=0; i<10; i++)
       
        {
           TargetX_SRMb__Family_Relationship__c RelList3 = new TargetX_SRMb__Family_Relationship__c();
           RelList3.End_Date__c= date.ValueOf('2019-03-02');
           RelList3.Start_Date__c= date.ValueOf('2015-03-02');
           RelList3.id = RelList[i].id;
           RelList2.add(RelList3);
            
        }
        
        Update RelList2;
        
        //delete relationship from a list of relationships
       // TargetX_SRMb__Family_Relationship__c deletedRel = [ SELECT Name, IsDeleted FROM TargetX_SRMb__Family_Relationship__c 
                                                         // WHERE Name = :RelList ALL ROWS];
       // System.assertEquals(deletedTargetX_SRMb__Family_Relationship__c.IsDeleted,true);
    
   }

}

Thanks,
Mariam