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
Aafaque Hussain 10Aafaque Hussain 10 

Getting Error "the following triggers have 0% code coverage. Each trigger must have at least 1% code coverage." for trigger on Task Trigger

Can Anyone helps we I am trying to deploy below Task trigger from Sandbox to Production but getting error "the following triggers have 0% code coverage. Each trigger must have at least 1% code coverage."

In the below trigger I am trying to create update lead status to "Intro call" when a task is create with Subject contain "Intro call" this code work fine in sandbox but gives code coverage error while deployment.

I am new to devolopment /Coding 

trigger changeLeadStatus on Task (before insert, before update) {
    String desiredNewLeadStatus = 'Intro Call';
    String myString;

    List<Id> leadIds=new List<Id>();
    for(Task t:trigger.new){
    mystring=t.subject.toUpperCase();
        if(t.Status=='Completed' && myString.contains('INTRO CALL')){
            if(String.valueOf(t.whoId).startsWith('00Q')==TRUE){//check if the task is associated with a lead
                leadIds.add(t.whoId);
            }//if 2
        }//if 1
    }//for
    List<Lead> leadsToUpdate=[SELECT Id, Status FROM Lead WHERE Id IN :leadIds AND IsConverted=FALSE];
    For (Lead l:leadsToUpdate){
        l.Status=desiredNewLeadStatus;
    }//for
    
    try{
        update leadsToUpdate;
    }catch(DMLException e){
        system.debug('Leads were not all properly updated.  Error: '+e);
    }
}//trigger
Jacob Elliott 8Jacob Elliott 8
Hey Aafaque, do you have test classes/methods covering your triggers? To move code from Sandbox to Production your total code coverage has to meet 75%. 

From Salesforce: "Before you can deploy your code or package it for the Lightning Platform​ AppExchange, at least 75% of Apex code must be covered by tests, and all those tests must pass. In addition, each trigger must have some coverage."

Please see the trailhead below to learn about Unit Tests.

https://trailhead.salesforce.com/en/content/learn/modules/apex_testing/apex_testing_intro
 
lnallurilnalluri
Hey @Aafaque, Yes you need code coverage to move apex from test environemt to production.

Sample test class for your scenario something like this.
 
@isTest
public class ChangeLeadStatusTest {
    @isTest
    static void insertTask(){
        Lead  l= new Lead();
        l.LastName='test';
        l.Company='test';
        l.Status='Intro Call';
        insert l;
        
        Task t = new Task();
        t.Subject='Intro Call';
        t.whoId=l.Id;
        t.Status='completed';
        insert t;
    }
}