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
Om NarlaOm Narla 

Code Coverage Failure while deployment

HI,

I am trying to deploy code from my sandbox to production but it is failing with the error as below:
Code Coverage Failure
Your code coverage is 0%. You need at least 75% coverage to complete this deployment.


My Sandbox code coverage shows as below:

Code Coverage: 82%
Your overall code coverage is currently 82%. To deploy code to production, you must have at least 75%
.

Can someone please help me what is wrong with the deployment?
Best Answer chosen by Om Narla
Om NarlaOm Narla
SOLVED

I hear many stories and I believe my story would help someone with this issue:

I wrote simple trigger in sanbox
trigger Test_TRG on Account (before insert) {
	for(account a :Trigger.New){
		a.Consultant__c = 'Test';
	}
    
}

and a test class :
@isTest
public class GolarTestProd{
	
	static testmethod void createLead(){
    	Lead ld = new Lead();
    	ld.FID__c = '122333';
    	ld.Facility_Name__c = 'XXXX';
    	ld.Company= 'Test Company';
    	ld.firstname = 'FirstName';
    	ld.lastname = 'lastname';
    	ld.status = 'New';
    	insert ld;
    	ld= [select id, fid__c,name from Lead where fid__c ='122333' ];
    	System.assertEquals('122333', ld.fid__c);
    	
    	
    }
}

Deployed both together through changesets and it deployed successfully. I assume that the test class deployed with the triggers gets executed for validation and deployes the code if the code is covered in that test. So try to deploy small snipped of code along with the Test class to cover that code and it iwll work. Now my code coverage shows 100% after running the tests in Production Org.
User-added image

I hope this helps others with similar problem.

All Answers

Anupama SamantroyAnupama Samantroy
What is the existing code coverage in production?
 
Om NarlaOm Narla
Thank you Anupama for checking my request!

My production code coverage shows as Zero.

Code Coverage: 0%
Your overall code coverage is currently 0%. To deploy code to production, you must have at least 75%

But, i do not have any code in production.. How can i have code coverage if i do not have Apex code in the system? Please advise. Thank you.
Om NarlaOm Narla
I do not have any Apex Class, or triggers, or validation rules in the Org.. I just have a test class as below: all tests pass but no luck with the code coverage.User-added imageTest Execution
@isTest
public class GolarTestProd{
	
	static testmethod void createLead(){
    	Lead ld = new Lead();
    	ld.FID__c = '122333';
    	ld.Facility_Name__c = 'XXXX';
    	ld.Company= 'Test Company';
    	ld.firstname = 'FirstName';
    	ld.lastname = 'lastname';
    	ld.status = 'New';
    	insert ld;
    	ld= [select id, fid__c,name from Lead where fid__c ='122333' ];
    	System.assertEquals('122333', ld.fid__c);
    	
    	
    }
    public static testMethod void runPositiveTestCases() {
        Integer  numAccts = 1;
        Integer  numContactsPerAcct = 1;
        List<Account> accts = new List<Account>();
        List<Account> accts2 = new List<Account>();
        for(Integer i=0;i<numAccts;i++) {
            Account acc1 = new Account(Name='TestAccount' + i);
            acc1.Name='Test Account';
        acc1.BillingCity ='Chennai' ;
        acc1.BillingCountry='india';
        acc1.BillingPostalCode='600075';
        acc1.BillingState='tamil nadu';
        acc1.BillingStreet='water well street';
        
        accts.add(acc1);
        }
        insert accts;
        accts2 = accts;
        accts = [select id, name from account where createddate =: System.today()];
        System.assertNotEquals(null,accts,'Accounts are returned');
        accts = accts2;
        List<Contact> cons = new List<Contact>();
        for (Integer j=0;j<numAccts;j++) {
            Account acct = accts[j];            
            // For each account just inserted, add contacts
            for (Integer k=numContactsPerAcct*j;k<numContactsPerAcct*(j+1);k++) {
                cons.add(new Contact(firstname='Test'+k,
                                     lastname='Test'+k,
                                     AccountId=acct.Id));
            }
        }
        // Insert all contacts for all accounts
        insert cons;
        
        for(Integer i=0;i<numAccts;i++){
        	System.assertEquals(null,accts[i].Company_Contact__c,'Before update Company Contact is null');
        }
        RecordType rt =[SELECT DeveloperName,Id,IsActive,Name,SobjectType FROM RecordType WHERE DeveloperName = 'Company'];
        for(Integer i=0;i<numAccts;i++){
                        accts[i].Client_Contact__c = cons[i].Id;
                accts[i].RecordTypeId = rt.id;
                accts[i].Company_Contact__c = cons[i].Id;
                                
        }
       update accts;
       accts = [select Id,RecordTypeId,Company_Contact__c from account where lastmodifieddate = : System.today() ];
       for(Account a:accts){
        	System.assertNotEquals(null,a.Company_Contact__c,'After update Company Contact is populated');
        }
       
        
    }
    
       
    static testmethod void createOpp(){
    	    Account acc1 = new Account(Name='TestAccount');
            acc1.Name='Test Account';
        acc1.BillingCity ='Chennai' ;
        acc1.BillingCountry='india';
        acc1.BillingPostalCode='600075';
        acc1.BillingState='tamil nadu';
        acc1.BillingStreet='water well street';
        insert acc1;
        Contact ct = new Contact(firstname='Test',
                                     lastname='Test',
                                     email__c= 'mar.dondeti@yahoo.com',
                                     AccountId=acc1.Id);
         insert ct;
         //Opportunity opt= new Opportunity(Name = 'Test',Contact_Person__c= ct.id,StageName= 'Qualification',CloseDate=System.Today());
           Opportunity opt ;//= [select id, name from Opportunity where createddate =: System.today()];
           System.assertEquals(null,opt,'Before insert opportunity does not exist');
         opt= new Opportunity(Name = 'Test',Contact_Person__c= ct.id,StageName= 'Qualification',CloseDate=System.Today());
        
         insert opt;
         opt = [select id, name from Opportunity where Name = 'Test'];
           System.assertNotEquals(null,opt,'After insert opportunity exist');
         
         
         
    }
    
    
}

 
Om NarlaOm Narla
SOLVED

I hear many stories and I believe my story would help someone with this issue:

I wrote simple trigger in sanbox
trigger Test_TRG on Account (before insert) {
	for(account a :Trigger.New){
		a.Consultant__c = 'Test';
	}
    
}

and a test class :
@isTest
public class GolarTestProd{
	
	static testmethod void createLead(){
    	Lead ld = new Lead();
    	ld.FID__c = '122333';
    	ld.Facility_Name__c = 'XXXX';
    	ld.Company= 'Test Company';
    	ld.firstname = 'FirstName';
    	ld.lastname = 'lastname';
    	ld.status = 'New';
    	insert ld;
    	ld= [select id, fid__c,name from Lead where fid__c ='122333' ];
    	System.assertEquals('122333', ld.fid__c);
    	
    	
    }
}

Deployed both together through changesets and it deployed successfully. I assume that the test class deployed with the triggers gets executed for validation and deployes the code if the code is covered in that test. So try to deploy small snipped of code along with the Test class to cover that code and it iwll work. Now my code coverage shows 100% after running the tests in Production Org.
User-added image

I hope this helps others with similar problem.
This was selected as the best answer