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
Tom Ford 15Tom Ford 15 

Can't Deploy Triggers despite 91% Coverage

Hi, I'm new to APEX and getting a little out of my depth. I have a few triggers that essentially prevent users from deleting records when they match critera. I've written the test class below and have 91% coverage in Sandbox. 

When i try to deploy into Production i get this error:

Code Coverage Failure
Your code coverage is 25%. You need at least 75% coverage to complete this deployment.

I'm aware it could be down to bad coding but Can anyone help?

@istest
public class TriggerTests{
@istest static void testOLITrigger() {
    System.Test.startTest();
Date ddate = Date.Today();
Account a2 = new Account(Name ='Test Account');
insert a2;
// Get the Standard PriceBook Id.
Id pricebookId = Test.getStandardPricebookId();       
Opportunity opp = new Opportunity(
    Name='Testing Opp',                            
    AccountId= a2.Id,
    StageName = 'Discovery',
    Type = 'New Business', 
    CurrencyIsoCode = 'USD',
    Region__c = 'New York',
    CloseDate = ddate);
insert opp;      
//Create your product
Product2 prod = new Product2(
     Name = 'TW Print',
    Family = 'The Week',
     Brand__c = 'The Week',
Ad_Type__c  = 'Display Advertising', 
    Media_Type__c = 'Print',
     ProductCode = 'TWP',
     isActive = true,
    CanUseRevenueSchedule = true
);
insert prod;
//Create your pricebook entry
PricebookEntry pbEntry = new PricebookEntry(
     Pricebook2Id = pricebookId,
     Product2Id = prod.Id,
     UnitPrice = 100.00,
     IsActive = true
);
insert pbEntry;
    //Create Line Item Details
Line_Item_Details__c LID = new Line_Item_Details__c(
    CurrencyIsoCode = 'USD',
    Print_Ad_Size__c = '1 Full Page',
    Quantity__c = 5,
    Rate__c = 123,
    Total_Net_Cost__c = 123*5,
    OppProductInvoiced__c = False
);
insert LID;       
//Create Opportunity Line Item. 
OpportunityLineItem oli = new OpportunityLineItem(
     OpportunityId = opp.Id,
     UnitPrice = 123,
     Quantity = 5,
     PricebookEntryId = pbEntry.Id,
    ServiceDate = ddate,
    Digital_End_Date__c = ddate +7,     
    Print_Frequency__c  = 'Consecutive Issues',
    Line_Item_Details__c = LID.id,
    Original_Total_Cost__c = 123*5,
    number_of_invoice_lines__c = 1
);
insert oli;  
//Create Test Issue
Issue__c Iss = new Issue__c(
    Name = 'Test Issue',
    Brand__c = 'The Week',
    Product_Code__c = 'TWM',
    On_Sale_Date__c = ddate - 7,
    Issue_counter__c = 2020001    
);
insert Iss;
//Create Insertion
Insertion__c Ins = new Insertion__c(
    Issue_lookup__c = Iss.id,
    Opportunity__c = opp.id,
    Line_Item_Details__C = LID.id
);
insert Ins;
//Create Opportunity Line Item Schedule. 
OpportunityLineItemSchedule olis = new OpportunityLineItemSchedule (
    OpportunityLineItemId = oli.id,
    ScheduleDate = ddate,
    Revenue =  123,
    Type = 'Revenue',
    Invoiced__c = TRUE
);
insert olis;   
  Try{
        olis.Revenue = 1234;
        olis.ScheduleDate = ddate + 2;
        update olis;       
    }
    catch(exception e){
              system.assert(e.getMessage().contains('You can not Amend this Schedule as it has been Invoiced'));                
}
    Try{
        delete olis;
    }
      catch(exception e){
                system.assert(e.getMessage().contains('You can not Delete this Schedule as it has been Invoiced'));                
}
    Try{
        delete oli;
    }
    catch(exception e){
                system.assert(e.getMessage().contains('You can not delete this Product as it has Invoiced Schedules'));  
    }
    Try{
        delete Ins;
    }
    catch(exception e){
                system.assert(e.getMessage().contains('You can not delete this Insertion as it has Closed'));  
    }          
System.Test.stopTest() ; 
}

//Test For Line Item Details Trigger    

@isTest static void testLIDTrigger() {
System.Test.startTest();
    //Create Line Item Details
    Line_Item_Details__c LID = new Line_Item_Details__c(
    CurrencyIsoCode = 'USD',
    Print_Ad_Size__c = '1 Full Page',
    Quantity__c = 5,
    Rate__c = 123,
    Total_Net_Cost__c = 123*5,
    OppProductInvoiced__c = TRUE
);
insert LID;
    
      try
    {
    delete LID;
        }
    catch(Exception e)
    { 
}
}
}

 

 

 

VinayVinay (Salesforce Developers) 
Hi Tom,  

Firstly trigger should have at least 1% of coverage to deploy triggers.

Please try to deploy only specific triggers were your changes having been made rather than running all other classes to production.

Review below links for more information..

https://help.salesforce.com/articleView?id=000335222&type=1&mode=1
https://trailhead.salesforce.com/en/content/learn/modules/apex_testing

Hope above information was helpful.

Please mark as Best Answer so that it can help others in the future.

Thanks,
Vinay Kumar
Tom Ford 15Tom Ford 15
Each trigger has over 90% covereage and 97% coverage in total now but i still get the error :

Your code coverage is 25%. You need at least 75% coverage to complete this deployment.
VinayVinay (Salesforce Developers) 
Hi Tom, 

Please check if you have missed to include test class along with trigger.

Try to validate individual trigger along with test class and check coverage for same.

Thanks,
Vinay Kumar