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
Lisa Haigy 53Lisa Haigy 53 

Code Coverage is 57% and will not deploy in Production

I am trying to deploy a trigger in production and despite having 57% code coverage in Sandbox, I get the following error
Code Coverage Failure Your code coverage is 0%. You need at least 75% coverage to complete this deployment. Business_Hours
What am I missing:

My trigger is:

trigger Business_Hours on Lead (before insert) {
    //Selecting default business hours (BH) record
    BusinessHours defaultBH = [SELECT Id FROM BusinessHours WHERE IsDefault = true Limit 1];
    //Making sure BH record exists
    if(defaultBH != NULL){
        for(Lead leadObj : trigger.new ){
            //Making sure that first call made field is populated and is updated
            if(leadObj.Call_Made__c != NULL && Trigger.oldMap.get(leadObj.Id).Call_Made__c != leadObj.Call_Made__c){
               decimal result = BusinessHours.diff(defaultBH.Id, leadObj.CreatedDate, leadObj.Call_Made__c );
            Decimal resultingMinutes = result/(60000);
                //Populating result into custom field & setting number of decimals
                leadObj.Elapsed_Time__c = resultingMinutes.setScale(4); 
            }  
        }    
    } 
}
Test Class:
@isTest

public class Business_Hours_Test
{
    static testMethod void testUnit()
    {
        
        
        Lead led = new Lead();
        led.lastname = 'Test';
        led.firstName ='Test';
        led.company = 'ABC';
        led.Brand__c = 'Rain';
        led.Call_Made__c = System.today();
        led.CreatedDate = System.today();
        led.Elapsed_Time__c = 736;
        // Add all required field
        update led;
        
    }
}
Antoninus AugustusAntoninus Augustus
Hey Lisa,

Your trigger is running on the 'before insert' context and you're updating Leads in your test class. You'd need to insert a Lead record in order to actually test the Business_Hours trigger.
Lisa Haigy 53Lisa Haigy 53
Ohhh Lordy Be...I didn't even notice that!!