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
Melanie Miller 11Melanie Miller 11 

Code Coverage failure on trigger deployment

I have a trigger and associated test class that I am trying to deploy in production. Everything tests fine in the sandbox, and it says my trigger has 100% coverage there as well. But when I try to deploy the change set in production, I get a code coverage failure message of "The following triggers have 0% code coverage. Each trigger must have at least 1% code coverage." It then lists the new trigger as the only item with the problem.

My question is, why does my trigger have 100% coverage in the sandbox, but when I bring it into production is says it has 0%? I'm pretty new to this stuff, so this is really confusing. Any help would be very appreciated!
Ankit SehgalAnkit Sehgal

Did you  include the test class in your ChangeSet while deploying the trigger ? It is necessary to include the test class.

Also while deploying the trigger select the "run specified tests" option and write the name of your test class in the box provided below.

Melanie Miller 11Melanie Miller 11
Yep, the test class for the trigger was included in the change set. I've tried deploying it a few different ways (run specified tests, run local tests) and I keep getting the same error.
Ankit SehgalAnkit Sehgal

I'm not sure what might be causing the issue but just go through the steps mention in this link. 

https://help.salesforce.com/apex/HTViewSolution?urlname=Code-coverage-steps-and-considerations-prior-to-deployments

Melanie Miller 11Melanie Miller 11
I tried the steps in the link, and all I accomplished was re-verifying the overall coverage for my production org (90%). Would you mind taking a look at my code? It works just fine in the sandbox, but maybe (probably) I'm missing something obvious.

What I'm trying to accomplish is saving the Opportunity stage value (when a related Opp exists) into a custom field I've created on the event object, at the time a new event is created.

Trigger
trigger UpdateSalesStage on Event (before insert) {
 for(Event e: trigger.new){

          List<Opportunity> opp = [select id,name,StageName from Opportunity where id =: e.whatID];

          if(opp.size() > 0){

             for(Opportunity o : opp){

                    e.Stage__c = o.StageName;
                }
           }
     }
}
Test Class
@isTest
private class TestUpdateSalesStage {

public static List<Opportunity> opp;
public static String id;
public static String size;

        static testMethod void testOppSize() {
        Test.startTest();
        
        Event e = new Event();        
        id = '006G000000nOnpD';
        size = '1';

system.assertEquals(1,1);

        Test.stopTest();        
    }    
    }

 
Ankit SehgalAnkit Sehgal

It looks like the test class is causing the code coverage failiure as you have used static id on line number 12.

Also you need to modify your trigger as we should avoid SOQL inside for loops. Try using this modified trigger:

Trigger

trigger UpdateSalesStage on Event (before insert) 
{
		List<Event> evt =[Select Id,WhatId FROM Event Where Id IN: Trigger.new];
		List<Opportunity> opp=[Select id,name,StageName FROM Opportunity];
		for(Event e:evt)
		{
			for(Opportunity o :opp)
			{
				if(e.whatId==o.Id)
				{
					e.Stage__c = o.StageName;
				}
			}
		}
		update evt;
}