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 

71% test coverage. Can't get it to %75..

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 BIA exams and set tasks to assistant 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;
        
        }

Here is the code for test class:​
 
@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;
        
        //I cant edit accnts.Latest_BIA_date__c field since its a roll-up field, so instead I added an appointment record that should autofill accnts.Latest_BIA_date__c with a date that is less than 30 days from today 
        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 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;
        
        lb.id = 'a0E4B000002Bzdt';
                
        Update lb;
        
        }
}

Here is the configuration for Latest_BIA_date__c​ roll-up field:

User-added image