+ Start a Discussion
Nitin MakwanaNitin Makwana 

Code Coverage Failure When deploying trigger from Sandbox to Production

Hi,

I have created following APEX TRIGGER so that whenever a new job is created on SalesForce, a Task is created on JIRA CORE.

trigger JobTrigger on Contract (after insert, after update) {
    if (Trigger.isInsert && Trigger.isAfter) {
        JCFS.API.createJiraIssue('10000', '10100');
    }
    if (Trigger.isUpdate && Trigger.isAfter) {
        JCFS.API.pushUpdatesToJira();
    }
}

Now when I deploy this trigger from Sandbox to Production, I get following error:

Code Coverage Failure
Your organization's code coverage is 0%. You need at least 75% coverage to complete this deployment. Also, the following triggers have 0% code coverage. Each trigger must have at least 1% code coverage.
- JobTrigger

So can anyone help me how can I resolve this error that I can deploy this trigger to Production from Sandbox without any error?

Thanks,
Nitin.
Best Answer chosen by Nitin Makwana
Boss CoffeeBoss Coffee
If those are your only two classes, then it should be 100% because pushing to production should ignore the @isTest class methods.

Could you try creating an Account first in that test method?
@isTest public static void contractAfterInsertTest(){
        Account acc = new Account(Name='TestAccount');
        insert acc;

        Contract job = new Contract( Name='My Test Job', Status = 'Draft', AccountID = acc.Id )  ;
        Insert job;
}

All Answers

Khan AnasKhan Anas (Salesforce Developers) 
Hi Nitin,

Greetings to you!

As mentioned your code coverage should be greater than 75% to deploy. Did you include the test class while deploying the trigger? It is necessary to include the test class.

Unit tests must cover at least 75% of your Apex code, and all of those tests must complete successfully.
Note the following:
  • When deploying Apex to a production organization, each unit test in your organization namespace is executed by default.
  • Calls to System.debug are not counted as part of Apex code coverage.
  • Test methods and test classes are not counted as part of Apex code coverage.
  • While only 75% of your Apex code must be covered by tests, don’t focus on the percentage of code that is covered. Instead, make sure that every use case of your application is covered, including positive and negative cases, as well as bulk and single records. This approach ensures that 75% or more of your code is covered by unit tests.
  • Every trigger must have some test coverage.
  • All classes and triggers must compile successfully.

Please refer to the below links which might help you further.

https://trailhead.salesforce.com/en/content/learn/modules/apex_testing/apex_testing_triggers

https://medium.com/@krissparks/apex-trigger-test-coverage-e78d801e9c7d

https://help.salesforce.com/articleView?id=000335222&type=1&mode=1 (https://help.salesforce.com/articleView?id=000335222&type=1&mode=1)

I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future. It will help to keep this community clean.

Thanks and Regards,
Khan Anas
Nitin MakwanaNitin Makwana
Yes, I included following test class when deploying.

@isTest public class JobTriggerTest {
    @isTest public static void contractAfterInsertTest(){
        Contract job = new Contract( Name='My Test Job', Status = 'Draft', AccountID = '001g000002ADUj2' )  ;
        Insert job;
    }
}

What else I need to do to fix this error?
 
Boss CoffeeBoss Coffee
Have you tried running that test to see how much code coverage it gives?

For that specific test class, you may want to create an account record in the test, then create the contract record looking up to that account.
Nitin MakwanaNitin Makwana
It gives me overall coverage of 33% and trigger's coverage as 100%

See this screen shot.

User-added image
Ajay K DubediAjay K Dubedi
Hi Nitin,

Try opening Apex classes in your org. And click compile all classes. And secondly, try Run all from the developer console. This should solve your problem.

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Ajay Dubedi
www.ajaydubedi.com
Boss CoffeeBoss Coffee
If those are your only two classes, then it should be 100% because pushing to production should ignore the @isTest class methods.

Could you try creating an Account first in that test method?
@isTest public static void contractAfterInsertTest(){
        Account acc = new Account(Name='TestAccount');
        insert acc;

        Contract job = new Contract( Name='My Test Job', Status = 'Draft', AccountID = acc.Id )  ;
        Insert job;
}
This was selected as the best answer
Nitin MakwanaNitin Makwana
Thanks Boss, your solution worked for.

Also, I would like to thank everyone who helped me to resolve this issue.
 
Boss CoffeeBoss Coffee
No problem, I'm glad it worked for you!