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
Kramer Hope 1Kramer Hope 1 

Code Coverage Error

I have created an APEX trigger in sandbox and when I try to validate in production I am getting this error. 

Code Coverage Failure
The following triggers have 0% code coverage. Each trigger must have at least 1% code coverage.
 - UpdateLeadswithTask

This does not make sense to me as the tigger in production says I cover 76% of code. See attachtment User-added image

I did not write an APEX class specifically for the trigger, rather just ran tests on all of my existing. Tihs is what got me the 76%. Any ideas?
Best Answer chosen by Kramer Hope 1
pconpcon
It's likely because you have the word 'Attempting' spelled incorrectly.  But also, you should move your lead query after your insert of your task.  Additionally, you should only have your insert of your task inside the startTest/stopTest.  So your test should look like:
 
@isTest
public class TestClassforTrigger {
    static Lead createLead() {
        return new Lead(
            LastName = 'Tulsani',
            Company ='ABC Corp',
            Status='Marketing Engaged'
        );
    }

    static Task createTask(Id userId, Id ldId) {
        return new Task(
            OwnerId = userId,
            Subject = 'Call for test class',
            WhoId = ldId,
            Priority = 'Normal'
        );
    }

    static testMethod void TestInsertTask() {
        Id userId = '005E0000007Oaod'; // This should really be a user created during the test
        Lead createlead = createLead();
        insert createlead;

        Task tk = createTask(userId, createlead.id);
        Test.startTest();

        insert tk;

        Test.stopTest();

        Lead getleadupdated = [
            select Status
            from Lead
            where Id = :createlead.id
        ];

        System.assertEquals(
            'Attempting',
            getleadupdated.Status,
            'The status was not updated'
        );
    }
}
NOTE: This code has not been tested and may contain typographical or logical errors

All Answers

pconpcon
When you did your deployment to production, did you include your test classes?  How did you do the deployment to production?  Also, you really should write tests specifically for this trigger.
Kramer Hope 1Kramer Hope 1
I did not include test classes. I can retry and do that again. I did not write a text class because I took the trigger from someone else and modified it to match what I needed. I am new to developing triggers and have not yet created any classes. [image: headshot] Kramer Hope[image: los angeles] Marketing Operations [image: wdlogo] [image: linkedin] [image: facebook] [image: twitter] [image: instagram] T 310 823 8238 x1182 | M 602 451 8314
Gyanender SinghGyanender Singh
Hi Kramer Hope
This warning comes when you deploy your trigger with the test class then in the production it runs all test classes but in case if you have any code coverage of all test class not more than 75% or if any trigger in the class not have code coverage then its show error because for deployment any trigger in the production then for every trigger you have atleast 1% code coverage.

Thanks
Gyani
Mirketa Software Pvt. Ltd.
http://www.mirketa.com
Kramer Hope 1Kramer Hope 1
Hi Gyani,

I am a little unclear on your answer. Are you saying this is another trigger that is causing the validation to fail? If thats the case can I deply my change set if it only had my one trigger in it that is at 76%?  

Does anyone have a similar trigger they can provide a test class for if thats what i need? 

Thanks!
Kramer Hope 1Kramer Hope 1
@PCON I have created this test trigger, but it is not passing its test. Any suggestions?
 
@isTest(seealldata = false)
public class TestClassforTrigger {

static Lead createLead() {
        Lead ld = new Lead();
        ld.LastName = 'Tulsani';
        ld.Company ='ABC Corp';
       ld.Status='Marketing Engaged';
        return ld;
    }
    
    static Task createTask(Id ldId) {
        Task tk = new Task();
       tk.OwnerId= '005E0000007Oaod' ; //user ID specific to your ORG
        tk.Subject ='Call for test class';
        tk.WhoId = ldid;
        tk.Priority = 'Normal';
        return tk;
    }
    
    static testMethod void TestInsertTask() {
        Test.startTest();
        Lead createlead = createLead();
        insert createlead;
        Lead Getlead = [SELECT Status FROM Lead where ID = : createlead.id];
        Task tk = createTask(Getlead.id);
        Lead Getleadupdated = [SELECT Status FROM Lead where ID = : createlead.id];
       insert tk;

        
        Test.stopTest();
        System.assertEquals('Attemtping', Getleadupdated.Status); 
        
    }
    }

 
pconpcon
It's likely because you have the word 'Attempting' spelled incorrectly.  But also, you should move your lead query after your insert of your task.  Additionally, you should only have your insert of your task inside the startTest/stopTest.  So your test should look like:
 
@isTest
public class TestClassforTrigger {
    static Lead createLead() {
        return new Lead(
            LastName = 'Tulsani',
            Company ='ABC Corp',
            Status='Marketing Engaged'
        );
    }

    static Task createTask(Id userId, Id ldId) {
        return new Task(
            OwnerId = userId,
            Subject = 'Call for test class',
            WhoId = ldId,
            Priority = 'Normal'
        );
    }

    static testMethod void TestInsertTask() {
        Id userId = '005E0000007Oaod'; // This should really be a user created during the test
        Lead createlead = createLead();
        insert createlead;

        Task tk = createTask(userId, createlead.id);
        Test.startTest();

        insert tk;

        Test.stopTest();

        Lead getleadupdated = [
            select Status
            from Lead
            where Id = :createlead.id
        ];

        System.assertEquals(
            'Attempting',
            getleadupdated.Status,
            'The status was not updated'
        );
    }
}
NOTE: This code has not been tested and may contain typographical or logical errors
This was selected as the best answer
Kramer Hope 1Kramer Hope 1
Awesome thank you it passes! I caught the attempting just after I sent to you and still didnt work.  When I am testing that trigger only 61% overall coverage works, but my trigger is 100%. Would that block it from passing validation? The triggers that are getting 0% are installed package triggers. Could I just delete those from sandbox. 
Kramer Hope 1Kramer Hope 1
Got is pushed to production. Thanks for the help!