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

Urgent help needed - Only 56% code coverage and can't figure out why..
The code coverage for the Trigger below is only 56%, trying to get it to atleast a 75%
Here is the testing class that I used:
Trigger GetInitialWeightValue on Appointments__c(after insert, after update) { Set<Id> accountIds = new Set<Id>(); for (Appointments__c appt : trigger.new) { accountIds.add(appt.Patient__c); } //Elimnate the the accounts that don't have IDs for accountIds.remove(null); //SOQL query that returns that initial weight value if (!accountIds.isEmpty()) { List<Account> accountsToUpdate = new List<Account>(); for (Account account : [ Select Id, ( Select Scheduled_time__c, weight__c From Dr_Chrono_appointments__r WHERE Weight__c != NULL AND Status__c NOT IN ('Canceled', 'Cancelled') Order by Scheduled_time__c ASC Limit 1 ) From Account Where Id in :accountIds ]) { //Declare a decimal variable to store initial weight value Decimal IW = NULL; // Get(0) to return the first element in the list value if (!account.Dr_Chrono_appointments__r.isEmpty()) { IW = account.Dr_Chrono_appointments__r.get(0).weight__c; } accountsToUpdate.add(new Account( Id = account.Id, initial_Weight__c = IW )); } update accountsToUpdate; } }
Here is the testing class that I used:
@isTest Private class InsertWeightToAppointment { @isTest static void InsertAppointment() { Account accnts = new Account(); Appointments__c appt = new Appointments__c(); Labs__c lb = new labs__c(); appt.weight__c = 200; appt.scheduled_time__c = date.today(); appt.patient__c = '00161000013cLIg'; appt.status__c = 'Completed'; insert appt; lb.value__c = 100; lb.Specimen_Received__c = date.today(); lb.patient__c = '00161000013cLIg'; lb.Observation_Description__c = 'LDL'; insert lb; } @isTest static void UpdateAppointment() { Appointments__c appt = new Appointments__c(); Labs__c lb = new labs__c(); appt.id = 'a0A6100000UUBeK'; appt.weight__c = 100; appt.scheduled_time__c = date.today(); appt.patient__c = '00161000013bPD9'; update appt; lb.value__c = 180; lb.Specimen_Received__c = date.today()-30; lb.patient__c = '00161000013cLIg'; lb.Observation_Description__c = 'LDL'; lb.id = 'a0E61000005Pg21'; update lb; } }
Let me know how it goes.
All Answers
Insert Account, then use that account in the insert Appointment__c section. I guess Dr_Chrono_appointments__r is the Account lookup in Appointment__c ?
I inserted an account first as you recommaned but it didn't make any difference. Still 56% coverage..
I attached the v.2 below, please let me know how can I fix it.
P.S:
For the labs-object-based triggers I'm getting 100% coverage eventhough both appointments triggers and labs triggers follow the same logic and being affected by the same test class equally. (I'm going to attach the labs trigger code below at the end, it might help to figure out whats going on..)
Labs trigger:
Let me know how it goes.
What do you think might have gone wrong? and who I do fix it be over 75%
Note I added to the code in line 10 because the Test class wasn't saving without including the account record type.