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..


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 =; 
//Create a task list that includes all the tasks
    List <Task> TaskList = new List<Task>();
    Set<Id> AccIds=new set<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 == && 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.',
                                  WhatID = a.Id));
        insert TaskList;

Here is the code for test class:​
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];
        = 'Fares';
        accnts.RecordTypeID =;
        accnts.Next_Appointment__c =;
        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 =;
        appt.patient__c =;
        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 =;
        lb.patient__c =;
        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 =;
        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;
        = 'a0E4B000002Bzdt';
        Update lb;

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

User-added image