You need to sign in to do that
Don't have an account?

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:
Here is the test class:
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; } }
Please try below code
Let us know if this will help you
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.
Please let me know if you can spot anything else..
update accnts;
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
Trigger code again: