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
Mahmoud Coudsi 1Mahmoud Coudsi 1 

Help needed - Only 71% code coverage and can't figure out why..

Hello,

I'm new to Apex and I'm trying to write this trigger that evaluates multiple conditions on the Accounts object (through if statements). If the conditions are met then the trigger creates a task related to the Account. 

Here is the code for the trigger:
 
//This Trigger get fired daily at 7 Am in the morning. It checks for patients who are due for BIAs and set tasks to Nicole so she can get a BIA exam for patient
Trigger AddTaskForVitalsCheck on Account(before update,after update) {

//Variable that returns a date; 28 days from today.
Date DateMonthAgo = date.today().addDays(-28); 
    
//Create a task list that includes all the tasks
    List <Task> TaskList = new List<Task>();
    
    Set<Id> AccIds=new set<Id>();
    for(Account a:trigger.new)
    {
    AccIds.add(a.Id);
    }
  
//Create a tasklist that includes all the tasks that are created today
   List<Task> TodaysTask =new List<Task>([SELECT Id,whatId from Task Where whatId in :AccIds AND CreatedDate=TODAY AND subject like '%vitals%' ]);
    
//Get the related Task for the accounts in this trigger
//Map <Id,Account> AcctsWithTask = new Map <Id,Account>([SELECT Id,(SELECT Id FROM Tasks) FROM Account WHERE Id IN :AccIds]);
    
// Add an Task for each account if account meets crietria and it doesn't already have a task.
// Iterate through each account.
    for(Account a : Trigger.New) {
    
        if (a.Next_appointment_date__c == date.today() && a.Latest_BIA_date__c < DateMonthAgo&&TodaysTask.size() < 1) {
            //If it doesn't, add a task
            TaskList.add(new Task(Subject= 'Nicole: Patient is due their monthly vitals check',
                                  Description= 'Nicole, It has been more than a month since vitals were taken for this patient. Please get patient\'s waist circumference, hip circumference, blood pressure and BIA exam prior to the appointment.',
                                  ActivityDate= Date.today().addDays(1),
                                  WhatID = a.Id));
        }           
    }
    
        insert TaskList;
        
        }

Here is the test class: 
 
@isTest(SeeAllData=true)
Private class VitalsAndLabsAutomation_TestClass {

    @isTest static void InsertAppointmentsAndLabs() {

Account accnts = new Account();
Appointments__c appt = new Appointments__c();
Appointments__c SecondAppt = new appointments__c();
Labs__c lb = new labs__c();

RecordType rt = [SELECT ID,Name FROM RecordType WHERE SobjectType='Account' and Name='Patients' Limit 1];
        
        accnts.name = 'Fares';
        accnts.RecordTypeID = rt.id;
        accnts.Next_Appointment__c = date.today();
        
        insert accnts;
        
        appt.weight__c = 200;
        appt.scheduled_time__c = date.today().AddDays(-30);
        appt.patient__c = accnts.id;
        appt.status__c = 'Completed';
        appt.Inbody_PBF__c = 300;
        appt.Inbody_SMM__c = 300;
        
        insert appt;
       
        lb.value__c = 100;
        lb.Specimen_Received__c = date.today().AddDays(-90);
        lb.patient__c = accnts.id;
        lb.Observation_Description__c = 'Trigl';
        
        
        insert lb;
        
        SecondAppt.scheduled_time__c = date.today();
        SecondAppt.patient__c = accnts.id;
        
        insert SecondAppt;
        
        }
        
         //Update existing patient's appointment 
         @isTest static void updateAppointmentsAndLabs() {
        
Account accnts = new Account();
Appointments__c appt = new Appointments__c();
Appointments__c SecondAppt = new appointments__c();
Labs__c lb = new labs__c();
        
        accnts.ID = '00161000013cLIg';
        accnts.Next_Appointment__c = date.today();
        
        Update accnts;
        
        appt.weight__c = 320;
        appt.patient__c = '00161000013cLIg';
        appt.ID = 'a0A6100000USumJ';
        appt.status__c = 'Completed';
        appt.Inbody_PBF__c = 300;
        appt.Inbody_SMM__c = 300;
        
        Update appt;
        
        Secondappt.weight__c = 300;
        Secondappt.scheduled_time__c = date.today();
        Secondappt.patient__c = '00161000013cLIg';
        Secondappt.ID = 'a0A6100000JkgIj';       
        
        Update secondappt;
        
        }
}

 
Amit Chaudhary 8Amit Chaudhary 8
Please add accnts.Latest_BIA_date__c in your test class.


Please try below code
@isTest
Private class VitalsAndLabsAutomation_TestClass 
{
    @isTest 
	static void InsertAppointmentsAndLabs() 
	{

		Account accnts = new Account();
		Appointments__c appt = new Appointments__c();
		Appointments__c SecondAppt = new appointments__c();
		
		Labs__c lb = new labs__c();
		RecordType rt = [SELECT ID,Name FROM RecordType WHERE SobjectType='Account' and Name='Patients' Limit 1];
        
        accnts.name = 'Fares';
        accnts.RecordTypeID = rt.id;
        accnts.Next_Appointment__c = date.today();
        
        insert accnts;
        
        appt.weight__c = 200;
        appt.scheduled_time__c = date.today().AddDays(-30);
        appt.patient__c = accnts.id;
        appt.status__c = 'Completed';
        appt.Inbody_PBF__c = 300;
        appt.Inbody_SMM__c = 300;
        insert appt;
       
        lb.value__c = 100;
        lb.Specimen_Received__c = date.today().AddDays(-90);
        lb.patient__c = accnts.id;
        lb.Observation_Description__c = 'Trigl';
        
        
        insert lb;
        
        SecondAppt.scheduled_time__c = date.today();
        SecondAppt.patient__c = accnts.id;
        
        insert SecondAppt;
		
		accnts.Latest_BIA_date__c = date.today().addDays(-30);
		update accnts;
        
    }
        
}

Let us know if this will help you

 
Mahmoud Coudsi 1Mahmoud Coudsi 1
Hi Amidt,

Thanks for the quick reponse. Latest_BIA_date__c is a roll-up field that returns the date of the last BIA test that was done (Please refer to the attachment below). Since its ineditable field, I wrote line (21 - 27) to autofill it. 

User-added image

Please let me know if you can spot anything else..
Amit Chaudhary 8Amit Chaudhary 8
Then After line 21 -27 try to update the account object to get that value like below

update accnts;

 
Mahmoud Coudsi 1Mahmoud Coudsi 1
Hi Amit,

I added update accnts; to my code however it didn't make any difference. I'm still at 71% code coverage. 

I cleaned my code a bit and added bunch of "update accnts;" after every child-record modification. Let me know what else I can do in order to get to 100% coverage
 
@isTest(SeeAllData=true)
Private class VitalsAndLabsAutomation_TestClass {

    @isTest static void InsertAppointmentsAndLabs() {

        Account accnts = new Account();
        Appointments__c appt = new Appointments__c();
        Labs__c lb = new labs__c();
        
        //Get the recordtype ID associated with the patients record type
        RecordType rt = [SELECT ID,Name FROM RecordType WHERE SobjectType='Account' and Name='Patients' Limit 1];
        
        accnts.name = 'Fares';
        accnts.RecordTypeID = rt.id;
        accnts.Next_Appointment__c = date.today();
        
        insert accnts;
        
        appt.weight__c = 200;
        appt.scheduled_time__c = date.today().AddDays(-30);
        appt.patient__c = accnts.id;
        appt.status__c = 'Completed';
        appt.Inbody_PBF__c = 300;
        appt.Inbody_SMM__c = 300;
        
        insert appt;
        update accnts;
       
        lb.value__c = 100;
        lb.Specimen_Received__c = date.today().AddDays(-90);
        lb.patient__c = accnts.id;
        lb.Observation_Description__c = 'Trigl';
                
        insert lb;
        update accnts;

        }
        
         //Update existing patient's appointment 
        @isTest static void updateAppointmentsAndLabs() {
        
        Account accnts = new Account();
        Appointments__c appt = new Appointments__c();
        Appointments__c SecondAppt = new appointments__c();
        Labs__c lb = new labs__c();
        
        accnts.ID = '00161000013cLIg';
        accnts.Next_Appointment__c = date.today();
        
        Update accnts;
        
        appt.weight__c = 320;
        appt.patient__c = '00161000013cLIg';
        appt.ID = 'a0A6100000USumJ';
        appt.status__c = 'Completed';
        appt.Inbody_PBF__c = 300;
        appt.Inbody_SMM__c = 300;
        
        Update appt;
        Update accnts;
        
        lb.id = 'a0E4B000002Bzdt';
                
        Update lb;
        Update accnts;
        
        }
}

Trigger code again:
 
//This Trigger get fired daily at 7 Am in the morning. It checks for patients who are due for BIA exams and set tasks to the medical assitant to get a BIA exam for patient who are due
Trigger AddTaskForVitalsCheck on Account(before update,after update) {

//Variable that returns a date; 28 days from today.
Date DateMonthAgo = date.today().addDays(-28); 
    
//Create a task list that includes all the tasks
    List <Task> TaskList = new List<Task>();
    
    Set<Id> AccIds=new set<Id>();
    for(Account a:trigger.new)
    {
    AccIds.add(a.Id);
    }
  
//Create a tasklist that includes all the tasks that are created today
List<Task> TodaysTask =new List<Task>([SELECT Id,whatId from Task Where whatId in :AccIds AND CreatedDate=TODAY AND subject like '%vitals%' ]);
    
//Get the related Task for the accounts in this trigger
//Map <Id,Account> AcctsWithTask = new Map <Id,Account>([SELECT Id,(SELECT Id FROM Tasks) FROM Account WHERE Id IN :AccIds]);
    
// Add an Task for each account if account meets crietria and it doesn't already have a task.
// Iterate through each account.
    for(Account a : Trigger.New) {
    
        if (a.Next_appointment_date__c == date.today() && a.Latest_BIA_date__c < DateMonthAgo&&TodaysTask.size() < 1) {
            //If it doesn't, add a task
            TaskList.add(new Task(Subject= 'Nicole: Patient is due their monthly vitals check',
                                  Description= 'Nicole, It has been more than a month since vitals were taken for this patient. Please get patient\'s waist circumference, hip circumference, blood pressure and BIA exam prior to the appointment.',
                                  ActivityDate= Date.today().addDays(1),
                                  WhatID = a.Id));
        }           
    }
    
        insert TaskList;
        
        }