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
KevlangdoKevlangdo 

Trigger Code Coverage

I have a simple trigger with 100% code coverage. In Sandbox all tests are Success. However deploying to prod, I am getting a No Cod eCoverage Error "The following triggers have 0% code coverage. Each trigger must have at least 1% code coverage.
UpdateOwnerCompany
"

I can see where my error is....what is missing

Here is Trigger Code:
 
trigger UpdateOwnerCompany on Opportunity (before update) {
    for(Opportunity o :Trigger.new){
        User op_owner = [Select u.id, u.CompanyName, u.Company_Code__c from User u where Id =: o.OwnerId];
       
        o.OwnerCompany__c = op_owner.CompanyName;
      
    }
}
Here is the Unit test
 
@IsTest(seeAllData=false)
public class UpdateOwnerCompanyTest {
    public static Opportunity opt;
    public static testMethod void testOwner(){
   
        test.startTest();
        Profile p = [SELECT Id FROM Profile WHERE Name='Standard Ontario User']; 
        User testuser = new User(
        Alias = 'cowner', Email='ontariouser@testorg.com', 
            EmailEncodingKey='UTF-8', LastName='Cowner', LanguageLocaleKey='en_US', 
            LocaleSidKey='en_US', 
            ProfileId = p.Id, 
            CompanyName = 'TestOntario',
            TimeZoneSidKey='America/New York', UserName='ontariouser@testorg.com'
        );
        insert testuser;
         Account testAcct = new Account (Name = 'Test Account');
        Date testCloseDate = Date.newInstance(2016, 4, 17);
        insert testAcct;
        opt = new Opportunity(
            Name = 'BuzzCo',
            AccountId = testAcct.ID,
            Opportunity_Type__c = 'Existing - SOW',
            LeadSource = 'Partner',
            Product_Family__c = 'Test',
            opportunitie_brands__c = 'Test',
            StageName = 'Qualification',
            CloseDate = testCloseDate ,
            Expected_First_Sale_Date__c = testCloseDate.addDays(7),
            next_step_date__c = testCloseDate
        );    
        insert opt;
              
       
        List<Opportunity> insertedOpps = [SELECT OwnerCompany__c FROM Opportunity WHERE name = 'BuzzCo'];
        for(Opportunity ops : insertedOpps){
             User op_owner = [Select u.id, u.CompanyName, u.Company_Code__c from User u where Id =: testuser.Id];
       
        	ops.OwnerCompany__c = op_owner.CompanyName;
            system.debug(ops.OwnerCompany__c);
          System.assertEquals(ops.OwnerCompany__c, 'TestOntario');
        }
        test.stopTest();
    }
   
}


 
Best Answer chosen by Kevlangdo
Amit Chaudhary 8Amit Chaudhary 8
What i see you are not using update in your test class. Update your code like below
@IsTest
public class UpdateOwnerCompanyTest 
{
    public static testMethod void testOwner()
	{
		Profile p = [SELECT Id FROM Profile WHERE Name='Standard Ontario User']; 
		User testuser = new User(
		Alias = 'cowner', Email='ontariouser@testorg.com', 
			EmailEncodingKey='UTF-8', LastName='Cowner', LanguageLocaleKey='en_US', 
			LocaleSidKey='en_US', 
			ProfileId = p.Id, 
			CompanyName = 'TestOntario',
			TimeZoneSidKey='America/New York', UserName='ontariouser@testorg.com'
		);
		insert testuser;

		System.RunAs(testuser)
		{
			test.startTest();
			
				Account testAcct = new Account (Name = 'Test Account');
				Date testCloseDate = Date.newInstance(2016, 4, 17);
				insert testAcct;
				
				Opportunity opt = new Opportunity(
					Name = 'BuzzCo',
					AccountId = testAcct.ID,
					Opportunity_Type__c = 'Existing - SOW',
					LeadSource = 'Partner',
					Product_Family__c = 'Test',
					opportunitie_brands__c = 'Test',
					StageName = 'Qualification',
					CloseDate = testCloseDate ,
					Expected_First_Sale_Date__c = testCloseDate.addDays(7),
					next_step_date__c = testCloseDate
				);    
				insert opt;
					  
				update opt;
			test.stopTest();
		}	
    }
   
}


NOTE:- I also found some issue in your Trigger
1) You are using SOQL inside for loop. Update your trigger like below
trigger UpdateOwnerCompany on Opportunity (before update) 
{
	Set<ID> setOwnerId = new Set<ID>();	
	for(Opportunity o :Trigger.new)
	{
		setOwnerId.add(o.OwnerId);
        o.OwnerCompany__c = op_owner.CompanyName;
      
    }
	if(setOwnerId.size() > 0 )
	{
		Map<Id,User> MaPop_owner = new Map<Id,User>([Select u.id, u.CompanyName, u.Company_Code__c from User u where Id in :setOwnerId]) ;
		for(Opportunity o :Trigger.new)
		{
			if(MaPop_owner.containsKey(o.OwnerId))
			{
				User op_owner = MaPop_owner.get(o.OwnerId);
				o.OwnerCompany__c = op_owner.CompanyName;
			}
		}
	}	
}

Let us know if this will help you

 

All Answers

UC InnovationUC Innovation
Hi Kevin,

My guess is that you dont have the test code in production. You should deploy them both to prod.

Hope this helps!

AM
KevlangdoKevlangdo
User-added image
Both the Trigger and the class are in the deployment package already
UC InnovationUC Innovation
Also as far as I can see, it doesnt look like you are updating the opportunity durring the test. This trigger is on update and I am uncertain whether or not you have other test classes in sandbox that might be updating opportunities. 
UC InnovationUC Innovation
Try running just this test and see the code coverage in sandbox
Prateek Singh SengarPrateek Singh Sengar
Hi Kevin,
There are multiple things that looks missing in your trigger and code.
  • Like UC mentioned you are not updating your opportunity in test class.
  • You trigger is performing SOQL inside for loop, this is a big no, you should ensure that your code is bulkified.
  • Also can you ensure that you have Standard Ontario User profile in your production otherwise your test class will fail during deployment resulting 0 coverage.
Amit Chaudhary 8Amit Chaudhary 8
What i see you are not using update in your test class. Update your code like below
@IsTest
public class UpdateOwnerCompanyTest 
{
    public static testMethod void testOwner()
	{
		Profile p = [SELECT Id FROM Profile WHERE Name='Standard Ontario User']; 
		User testuser = new User(
		Alias = 'cowner', Email='ontariouser@testorg.com', 
			EmailEncodingKey='UTF-8', LastName='Cowner', LanguageLocaleKey='en_US', 
			LocaleSidKey='en_US', 
			ProfileId = p.Id, 
			CompanyName = 'TestOntario',
			TimeZoneSidKey='America/New York', UserName='ontariouser@testorg.com'
		);
		insert testuser;

		System.RunAs(testuser)
		{
			test.startTest();
			
				Account testAcct = new Account (Name = 'Test Account');
				Date testCloseDate = Date.newInstance(2016, 4, 17);
				insert testAcct;
				
				Opportunity opt = new Opportunity(
					Name = 'BuzzCo',
					AccountId = testAcct.ID,
					Opportunity_Type__c = 'Existing - SOW',
					LeadSource = 'Partner',
					Product_Family__c = 'Test',
					opportunitie_brands__c = 'Test',
					StageName = 'Qualification',
					CloseDate = testCloseDate ,
					Expected_First_Sale_Date__c = testCloseDate.addDays(7),
					next_step_date__c = testCloseDate
				);    
				insert opt;
					  
				update opt;
			test.stopTest();
		}	
    }
   
}


NOTE:- I also found some issue in your Trigger
1) You are using SOQL inside for loop. Update your trigger like below
trigger UpdateOwnerCompany on Opportunity (before update) 
{
	Set<ID> setOwnerId = new Set<ID>();	
	for(Opportunity o :Trigger.new)
	{
		setOwnerId.add(o.OwnerId);
        o.OwnerCompany__c = op_owner.CompanyName;
      
    }
	if(setOwnerId.size() > 0 )
	{
		Map<Id,User> MaPop_owner = new Map<Id,User>([Select u.id, u.CompanyName, u.Company_Code__c from User u where Id in :setOwnerId]) ;
		for(Opportunity o :Trigger.new)
		{
			if(MaPop_owner.containsKey(o.OwnerId))
			{
				User op_owner = MaPop_owner.get(o.OwnerId);
				o.OwnerCompany__c = op_owner.CompanyName;
			}
		}
	}	
}

Let us know if this will help you

 
This was selected as the best answer
KevlangdoKevlangdo
Thanks for all you help!

Here is the final code that has allowed me to successfully deploy using your suggestions
 
List<Opportunity> insertedOpps = [SELECT OwnerCompany__c FROM Opportunity WHERE name = 'BuzzLub'];
        for(Opportunity ops : insertedOpps){
             User op_owner = [Select u.id, u.CompanyName, u.Company_Code__c from User u where Id =: testuser.Id];
       
        	ops.OwnerCompany__c = op_owner.CompanyName;
            system.debug(ops.OwnerCompany__c);
          
        }
        update insertedOpps;
        System.assertEquals(insertedOpps[0].OwnerCompany__c, 'TestCo');
        test.stopTest();

Thanks