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 

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%
 
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;
        
        }
     
}
Best Answer chosen by Mahmoud Coudsi 1
LBKLBK
Try this class.
 
@isTest(SeeAllData=true)
Private class InsertWeightToAppointment {

    @isTest static void InsertAppointment() {
        Account accnts = new Account();
        Appointments__c appt = new Appointments__c();
        Labs__c lb = new labs__c();
        
        accnts.name = 'afagas';
        accnts.RecordType = 'Patient';
        insert accnts;
        
        appt.weight__c = 200;
        appt.scheduled_time__c = date.today();
        appt.patient__c = accnts.id;
        appt.status__c = 'Completed';
		
        
        insert appt;
        
        lb.value__c = 100;
        lb.Specimen_Received__c = date.today();
        lb.patient__c = accnts.id;
        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

LBKLBK
You need to insert an Account first before inserting Appointment__c and Lab__c. Otherwise, as per your first condition (with account ids null), all the records will be removed before executing the for logic.

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 ?
Mahmoud Coudsi 1Mahmoud Coudsi 1
@LBK thanks for the fast response!  Dr_Chrono_appointments__r is the relationship name between the accounts object (Parent) and the Appointments object (Child).

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..)
 
@isTest
Private class InsertWeightToAppointment {

    @isTest static void InsertAppointment() {
        Account accnts = new Account();
        Appointments__c appt = new Appointments__c();
        Labs__c lb = new labs__c();
        
        accnts.name = 'afagas';
        accnts.RecordType = 'Patient';
        insert accnts;
        
        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;
        
        }
     
}

Labs trigger:
 
Trigger GetLatestLDLValue on labs__c(after insert, after update) {
    Set<Id> AccountIds = new Set<Id>();

    for (labs__c lb : trigger.new) {
        accountIds.add(lb.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 Specimen_Received__c, Value__c, Observation_description__c
                    From labs__r
                    WHERE Value__c != NULL AND Observation_description__c LIKE '%LDL%'
                    Order by Specimen_Received__c DESC
                    Limit 1
                )
            From Account
            Where Id in :accountIds
        ]) {
           
            //Declare a decimal variable to store initial labs value 
            Decimal LLDL = NULL;
            
            // Get(0) to return the first element in the list value
            if (!account.Labs__r.isEmpty()) {
                LLDL = account.labs__r.get(0).Value__c;
                
            }

            accountsToUpdate.add(new Account(
                Id = account.Id,
                Latest_LDL_labs_value__c = LLDL
            ));
            
        }   
        
        Update accountsToUpdate;
        
        }
      }

 
LBKLBK
Try this class.
 
@isTest(SeeAllData=true)
Private class InsertWeightToAppointment {

    @isTest static void InsertAppointment() {
        Account accnts = new Account();
        Appointments__c appt = new Appointments__c();
        Labs__c lb = new labs__c();
        
        accnts.name = 'afagas';
        accnts.RecordType = 'Patient';
        insert accnts;
        
        appt.weight__c = 200;
        appt.scheduled_time__c = date.today();
        appt.patient__c = accnts.id;
        appt.status__c = 'Completed';
		
        
        insert appt;
        
        lb.value__c = 100;
        lb.Specimen_Received__c = date.today();
        lb.patient__c = accnts.id;
        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.
 
This was selected as the best answer
Mahmoud Coudsi 1Mahmoud Coudsi 1
@LBK thanks alot for the follow up. I finally got 100% code coverage on the sandbox! However, when I got to deploying the class and the triggers in the production. I got %69 code coverage..

What do you think might have gone wrong? and who I do fix it be over 75%

User-added image

Note I added
accnts.RecordTypeID = '01261000000kaLi';
to the code in line 10 because the Test class wasn't saving without including the account record type.
 
@isTest(SeeAllData=true)
Private class InsertWeightToAppointment {

    @isTest static void InsertAppointment() {
        Account accnts = new Account();
        Appointments__c appt = new Appointments__c();
        Labs__c lb = new labs__c();
        
        accnts.name = 'afagas';
        accnts.RecordTypeID = '01261000000kaLi';
        
        insert accnts;
        
        appt.weight__c = 200;
        appt.scheduled_time__c = date.today();
        appt.patient__c = accnts.id;
        appt.status__c = 'Completed';
        
        
        insert appt;
        
        lb.value__c = 100;
        lb.Specimen_Received__c = date.today();
        lb.patient__c = accnts.id;
        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;
        
        }
     
}

 

 
LBKLBK
Are you using change sets to deploy the code in PROD? Can you try to push only this class file and test class in a change set?