+ Start a Discussion
BobBob 

Code Coverage Failure Update_Opportunity_Amount

I have a quote trigger called Update_Opportunity_Amount. I am getting the following error when i try to deploy it to my production org.


The following triggers have 0% code coverage. Each trigger must have at least 1% code coverage.
Update_Opportunity_Amount

I do have a test class, but the error states I have no code coverage. I would appreciate if anyone can tell me what is wrong with my test class that it is not testing my trigger.

Trigger

Update_Opportunity_Amount
trigger Update_Opportunity_Amount on Quote (before insert, before update) {

    List<Opportunity> opps = new List<Opportunity>();
    List<Quote> quotes = new List<Quote>();
    Map<Id, Double> quoteNR = new Map<Id, Double>();
    Set<Id> oppIds = new Set<Id>();
    Set<Id> qteIds = new Set<Id>();

    for (Quote q : Trigger.new) {
        if (q.Primary_Quote__c == true) {
            quotes.add(q);
            qteIds.add(q.Id);
            if (!quoteNr.isEmpty() && quoteNr.containsKey(q.OpportunityId)) {
                quoteNR.clear();
                q.addError('You are attempting to create multiple Primary Quotes into the one Opportunity');
            }
            else {
                quoteNR.put(q.OpportunityId, q.Net_Revenue__c);
            }
        }
    }
    
    if (!quoteNR.isEmpty()) {
        
        // Check if any Quotes tied to the same Opportunity as this Quote is listed as a Primary Quote
        for (Quote q : [SELECT Id, Primary_Quote__c, OpportunityId FROM Quote WHERE OpportunityId IN :quoteNR.keySet() AND Primary_Quote__c = true]) {
            if (qteIds.isEmpty() || !qteIds.contains(q.Id)) {
                oppIds.add(q.OpportunityId);
            }
        }

        if (!oppIds.isEmpty()) {
            for (Quote q : quotes) {
                if (oppIds.contains(q.OpportunityId)) {
                    q.addError('The Opportunity tied to this Quote already contains a Primary Quote. This quote cannot be set as a Primary Quote');
                }
            }
        }
            
        // Update the Opportunity with the Net Revenue from the Quote 
        for (Opportunity o : [SELECT Id, Amount FROM Opportunity WHERE Id IN :quoteNR.keySet()]) {
            o.Amount = quoteNR.get(o.Id);
            opps.add(o);
        }
    }
    
    if (!opps.isEmpty()) {
        update(opps);
    }
}

Test Class
@isTest
private class Update_Opportunity_Amount_TestClass {

    static testMethod void myUnitTest() {
        
        RecordType naOppType = [SELECT Id FROM RecordType WHERE Name = 'NA Opportunity Record Type' LIMIT 1];
        RecordType naAccTypeCustomer = [SELECT Id FROM RecordType WHERE Name = 'NA Customer Account Record Type - Active Customer - BW' LIMIT 1];
        RecordType naAccTypeProspect = [SELECT Id FROM RecordType WHERE Name = 'NA Customer Account Record Type - Prospect - BW' LIMIT 1];
        List<Account> accs = new List<Account>();
        List<Opportunity> opps = new List<Opportunity>();
        List<Quote> quotes = new List<Quote>();

        // Insert the Accounts to be used for the testing
        Integer j = 0;
        for (Integer i = 0; i < 41; i++) {
            Account a = new Account();
            a.Name = 'Account Test ' + i;
            a.Sales_Channel__c = '160 SPECIAL FORCES';
            a.Marketing_Level__c = 'Developer';
            a.Billing_Region__c = '1';
            a.BillingCity = '1';
            a.BillingCountry = '1';
            a.BillingPostalCode = '1';
            a.BillingState = '1';
            a.BillingStreet = '1';
            a.Shipping_Region__c = '1';
            a.ShippingCity = '1';
            a.ShippingCountry = '1';
            a.ShippingPostalCode = '1';
            a.ShippingState = '1';
            a.ShippingStreet = '1';
            if (j == 0) {
                a.RecordTypeId = naAccTypeProspect.Id;
                j = 1;
            }
            else {
                a.RecordTypeId = naAccTypeCustomer.Id;
                j = 0;          
            }
            accs.add(a);
        }
        insert(accs);
        
        // Insert the Opportunities to be used for the testing
        for (Integer i = 0; i < 41; i++) {
            Opportunity o = new Opportunity();
            o.Name = 'Opportunity Test ' + i;
            o.Amount = 111.11;
            o.StageName = 'H - Hot Prospect';
            o.CloseDate = Date.today() + 365;
            o.AccountId = accs[i].Id;
            o.RecordTypeId = naOppType.Id;
            if (j == 0) {
                o.Interface_Status_del__c = 'D';
                j = 1;
            }
            else {
                j = 0;          
            }           
            opps.add(o);
        }
        insert(opps);
        
        // Create Quoets that are NOT Primary Quotes. Each Quote for its own Opportunity
        for (Integer i = 0; i < 40; i++) {
            Quote q = new Quote();
            q.Name = 'Quote Test ' + i;
            q.Net_Revenue__c = 222.22;
            q.Primary_Quote__c = false;
            q.OpportunityId = opps[i].Id;
            quotes.add(q);
        }
        insert(quotes);
        
        // Validate that the Amount on the Opportunities still equals 111.11
        for (Opportunity o : [SELECT Id, Amount FROM Opportunity WHERE Id IN :opps AND Id != :opps[40].Id]) {
            System.assertEquals(o.Amount, 111.11);
        }
        
        // Create Quoets that are Primary Quotes. Each Quote for its own Opportunity
        quotes.clear(); 
        for (Integer i = 0; i < 40; i++) {
            Quote q = new Quote();
            q.Name = 'Quote Test ' + i;
            q.Net_Revenue__c = 999.99;
            q.Primary_Quote__c = true;
            q.OpportunityId = opps[i].Id;
            quotes.add(q);
        }
        insert(quotes);
        
        // Validate that the Amount on the Opportunities now equals 999.99
        for (Opportunity o : [SELECT Id, Amount FROM Opportunity WHERE Id IN :opps AND Id != :opps[40].Id]) {
            System.assertEquals(o.Amount, 999.99);
        }       
        
        // Update the Quotes with a new Net Revenue
        for (Quote q : quotes) {
            q.Net_Revenue__c = 555.55;
        }
        update(quotes);     
        
        // Validate that the Amount on the Opportunities now equals 555.55
        for (Opportunity o : [SELECT Id, Amount FROM Opportunity WHERE Id IN :opps AND Id != :opps[40].Id]) {
            System.assertEquals(o.Amount, 555.55);
        }       
        
        
        // Insert new Quotes that are also Primary Quotes to the same Opportunities as above. An error should be thrown
        quotes.clear();     
        for (Integer i = 0; i < 40; i++) {
            Quote q = new Quote();
            q.Name = 'Quote Test ' + i;
            q.Net_Revenue__c = 333.33;
            q.Primary_Quote__c = true;
            q.OpportunityId = opps[i].Id;
            quotes.add(q);
        }
        try {
            insert(quotes);
            System.assert(false);
        } catch (DmlException e) {
            System.assert(e.getDmlMessage(0).indexOf('The Opportunity tied to this Quote already contains a Primary Quote. This quote cannot be set as a Primary Quote') > -1);
        }       
        
        // Validate that the Amount on the Opportunities still equals 555.55 and not 333.33 
        for (Opportunity o : [SELECT Id, Amount FROM Opportunity WHERE Id IN :opps AND Id != :opps[40].Id]) {
            System.assertEquals(o.Amount, 555.55);
        }
        
        // Create multiple Quotes for the one Opportunity all quotes being a Primary Quote. An error should be thrown
        quotes.clear();
        for (Integer i = 0; i < 40; i++) {
            Quote q = new Quote();
            q.Name = 'Quote Test ' + i;
            q.Net_Revenue__c = 888.88;
            q.Primary_Quote__c = true;
            q.OpportunityId = opps[40].Id;
            quotes.add(q);
        }
        try {
            insert(quotes);
            System.assert(false);
        } catch (DmlException e) {
            System.assert(e.getDmlMessage(0).indexOf('You are attempting to create multiple Primary Quotes into the one Opportunity') > -1);
        }
        
        // Check that the last Opportunity (used for above test) still has the Amount 111.11
        for (Opportunity o : [SELECT Id, Amount FROM Opportunity WHERE Id = :opps[40].Id]) {
            System.assertEquals(o.Amount, 111.11);
        }
    }
}

 
SFDC RJSFDC RJ
Hi,
Have you run the test class in sandbox and checked what's the code coverage of the trigger?If not then firstly make sure test class is running fine. And
Have you tried sending both trigger and test class to production at the same time ?

Thanks
BobBob
I ran the test and it has zero percent coverage.
SFDC RJSFDC RJ
so i cant be deployed unless the code coverage is 75 % or more than that.
​Thanks