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
Kalue HerKalue Her 

Code Coverage Failure - Lead Assignment by Zipcode Trigger

I don't have any developer or coding knowledge. I try to learn as I go, but for the past two days, I have not figured out how to resolve my issue. I hope that the forum will help guide me in the right direction so that I can move forward.

What I've done is created a trigger to assign leads to a particular zipcode based off this suggestion (https://developer.salesforce.com/forums/?id=906F0000000Af9nIAC). It litterally is the same trigger. All I did was substitute the database and sales field name with my custom object fields. It worked great in the sandbox production. So I created two more triggers for the Account and Contact records. Both of those worked as well.

The issue I'm running into now is deployment into production. Validation failed. My org has 0% coverage. I've read as much documentation and examples as I could, and tested what I could understand. Nothing is making much sense at this point.

Appreciate it.
Best Answer chosen by Kalue Her
Veenesh VikramVeenesh Vikram
In this line:
Zip_Code__c zip = Zip_Code__c(Name='123456',Outreach_Specialist__c=UserInfo.getUserId());
I am trying to assign a Name and Outreach_Specialist__c in the Zip_Code__c field. 

Try this:
@isTest 
Private Class LeadAssignByZip{ 
    static testmethod void verifyLeadAssignByZip(){ 
		
		//Insert lead record
		Lead l = new Lead(FirstName='Test', LastName='Lead', Company='ABC Inc.',PostalCode = '123456');
		insert l;
        
    }
}

This code will not give a good coverage, but will at least let you deploy the Trigger.
 

All Answers

Veenesh VikramVeenesh Vikram
Hi,

The issue here is that the trigger you have written has 0% code coverage, and Salesforce as a Standard requires that overall 75% Code coverage should be there for Apex Classes. Triggers need at least 1% Code coverage so that they can be deployed to Production.
So, in a single sentence, you need to write a Test Class for your trigger.

Something like this will help you (for Lead Trigger):
@isTest
Private Class LeadTrigger_Test{
	static testmethod void testScenario1{
		Lead l = new Lead(FirstName='Test', LastName='Lead', Company='ABC Inc.');
		Insert l;
		
		l.Company='XYZ Corp.';
		update l;
	}
}

This code will give your trigger at least 1 % code coverage and you will be able to deploy the Trigger.

Veenesh
Kalue HerKalue Her
Hi Veenesh. Thank you for responding and providing some guidance. I created a Test Class based on your suggestion, but my error message is: "Error: Compile Error: Variables cannot be marked as tests at line 3 column 28"

Below is my lead trigger, and the test class that is created. I am definitely missing something, and am reading up on more documentation. I appreciate the patience and help.
 
trigger LeadAssignByZip on Lead (before insert, before update)
{
    List<Lead> leadsToUpdate = new List<Lead>();

    for (Lead lead : Trigger.new)
    {    
      if (lead.PostalCode != NULL)
      {
          // Find the outreach specialist for the current zip code
          List<Zip_Code__c> zip = [select Outreach_Specialist__c from Zip_Code__c
                                       where Name = :lead.PostalCode limit 1];     
               
          // if you found one
          if (zip.size() > 0)
          {   
              //assign the lead owner to the zip code owner
              lead.OwnerId = zip[0].Outreach_Specialist__c;
         
              leadsToUpdate.add(lead);
         
          }
       }
    }
}
And the test class:
@isTest 
Private Class LeadAssignByZip{ 
    static testmethod void verifyLeadAssignByZip{ 
        Zip_Code__c l = Zip_Code__c(Zipcode='55055', Outreach_Specialist__c=Laura Savat); 
        Insert l; 

        l.Outreach_Specialist__c=Renee Most; 
        update l; 
    }
}

 
Veenesh VikramVeenesh Vikram
Hi,

Can you try this one: 
@isTest 
Private Class LeadAssignByZip{ 
    static testmethod void verifyLeadAssignByZip{ 
		//Insert ZIP
		Zip_Code__c zip = Zip_Code__c(Name='123456', Outreach_Specialist__c=UserInfo.getUserId()); 
        insert zip; 
		
		//Insert lead record
		Lead l = new Lead(FirstName='Test', LastName='Lead', Company='ABC Inc.',PostalCode = '123456');
		insert l;
        
    }
}

Let me know if this helps!

Veenesh
Kalue HerKalue Her
Thank Veenesh. I am still getting the same error message:

"Error: Compile Error: Variables cannot be marked as tests at line 3 column 28"

I'm trying to locate any documentation on this error, but I am not having much luck from the knowledge base. I'll keep looking, but any suggestion is welcome.
Veenesh VikramVeenesh Vikram
Oh the method was not declared correctly:
@isTest 
Private Class LeadAssignByZip{ 
    static testmethod void verifyLeadAssignByZip(){ 
		//Insert ZIP
		Zip_Code__c zip = Zip_Code__c(Name='123456', Outreach_Specialist__c=UserInfo.getUserId()); 
        insert zip; 
		
		//Insert lead record
		Lead l = new Lead(FirstName='Test', LastName='Lead', Company='ABC Inc.',PostalCode = '123456');
		insert l;
        
    }
}

Let me know if it works!

Veenesh
Kalue HerKalue Her
New error message: "Error: Compile Error: Variable does not exist: Name at line 5 column 39"

Does "Name" need to be the field name or does it link it self to the trigger? I'm trying to understand as much as possible.

Thanks Veenesh.
Veenesh VikramVeenesh Vikram
In this line:
Zip_Code__c zip = Zip_Code__c(Name='123456',Outreach_Specialist__c=UserInfo.getUserId());
I am trying to assign a Name and Outreach_Specialist__c in the Zip_Code__c field. 

Try this:
@isTest 
Private Class LeadAssignByZip{ 
    static testmethod void verifyLeadAssignByZip(){ 
		
		//Insert lead record
		Lead l = new Lead(FirstName='Test', LastName='Lead', Company='ABC Inc.',PostalCode = '123456');
		insert l;
        
    }
}

This code will not give a good coverage, but will at least let you deploy the Trigger.
 
This was selected as the best answer
Kalue HerKalue Her
Thank you so much Veenesh. The lead and contact test class was not passing, so I tried to validate in production and realized what the problem was. I need to add email or phone to the test class. I went back and added "phone" to line 6. Test passed for both test class, validate was a success and it is currently deployed. I have to remember that our validation rules definitely plays a role anytime there is customization involved.

Have a wonderful day!