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
KatherineCKatherineC 

Trigger failed to deploy with 100% code coverage

Hi,

My trigger failed to deploy with 100% code coverage. I got help for creating the trigger and test, don't know why it's only 22% coverage at deployment, please help.

Trigger:

trigger SemiMonthly on Pint__c (after update) {
    List<Pint__c> pints = new List<Pint__c>();
   for (Pint__c p : Trigger.new) {
           if (p.Semi_Monthly__c == true ) {
               Pint__c newPint = new Pint__c();
               newPint.Account__c = p.Account__c;                
               pints.add(newPint);
        }
    }
   if(pints.size() > 0) insert pints;
}

Test:

@isTest
public class TestSemiMonthly
{
    static testMethod void insertNewPint()
    {
        // All these records created in your test method are only valid for the tests
        // and they do not actually get inserted in the database, nor they're available
        // outside this context
        Account account = new Account();
        account.Name = 'Test Account';
        insert account;
        Pint__c PintToCreate = new Pint__c();
        PintToCreate.Account__c = account.Id; // HERE we need an ID, not a Name
        insert PintToCreate;
        Test.StartTest();
        PintToCreate.Semi_Monthly__c = true;
        update PintToCreate;
        Test.StopTest();
    }
}

Error message:

, Details: Average test coverage across all Apex Classes and Triggers is 22%, at least 75% test coverage is required.


Best Answer chosen by KatherineC
Deepak Kumar ShyoranDeepak Kumar Shyoran
Ok Just write the test class for all those Two trigger which have no test class and improve the code coverage which have 40% in Sandbox  then from the Sandbox or using eclipse IDE deploy all these new and Updated component to the production along with trigger. It'll solve your problem.
As during deployment of these new components the production estimates overall code coverage by considering new/updated test class.

All Answers

Deepak Kumar ShyoranDeepak Kumar Shyoran
This is because the overall code coverage for the Org where you deploying the new component is below 75%. Please check it and try improve the code coverage for other component in target Org if they are below 75%.
KatherineCKatherineC
How to improve the code coverage for other component? Can u explain more on that? I'm new in coding, thanks.
Deepak Kumar ShyoranDeepak Kumar Shyoran
If you are deploying only one component i.e. the trigger and its test class then you need to check the overall coverage for the target organization where you are going to deploy your trigger by running all test class over there using the developer console and the then figure out which test class have less code coverage.
Then modify the test class for the corresponding Apex Class and include modified test class in your package/Change set which you want to deploy.
And if you have some other Apex Classes in your Change Set or package then include their test class with > 75 % coverage also along with trigger and its test class.
KatherineCKatherineC
I think I did not explain my situation clearly. I have 100% code coverage when runing test in Sandbox, change set including 1 trigger and 1 apex class uploaded successfully, when deploy to production, in the production environment it shows error message that coverage is only 22%, since I can not create trigger and run test, don't know what's my next step. 
Deepak Kumar ShyoranDeepak Kumar Shyoran
Please check the overall code coverage for your target org (Production) by running all apex classes and using estimate overall test coverage for your org.
KatherineCKatherineC
I have 3 triggers without test class, we were in 30 days trial period then recently paid to join Salesforce, so it became production enviroment, I did not know I can not create triggers and apex class in production, so leaving 3 triggers there, 1 has 40% coverage, 2 have 0% coverage, none of them has test class. I learned from Help & Training that I can inactivate unwanted triggers in sandbox, deploy to production then delete them through eclipse. Unfortunately the 3 inactive triggers in sandbox can not deploy to production due to not 75% coverage. So I'm stuck, with those 3 triggers there seems I can not do anything. Any suggestion what I can do now?
Deepak Kumar ShyoranDeepak Kumar Shyoran
Ok Just write the test class for all those Two trigger which have no test class and improve the code coverage which have 40% in Sandbox  then from the Sandbox or using eclipse IDE deploy all these new and Updated component to the production along with trigger. It'll solve your problem.
As during deployment of these new components the production estimates overall code coverage by considering new/updated test class.
This was selected as the best answer
KatherineCKatherineC
I understand now, thank you very much for your help.
Deepak Kumar ShyoranDeepak Kumar Shyoran
Your welcome.