+ Start a Discussion
Travis DvorakTravis Dvorak 

No coverage on test class

I have a working trigger that I'm starting to build my test class for.  I've done a few lines and ran a test to make sure it's working as it should and everything seems okay, except the trigger itself is 0% coverage.  Can someone help me figure out why?  I'm an amature with this stuff, so please go easy on me if it's something very simple.

Trigger:
trigger PartnerLeadAssignment on Lead (before insert) {
    
    List<Lead> leadList = new List<Lead>();
    for(Lead l : trigger.new){
        leadList.add(l);
        if(l.Company == null){
            l.Company = l.FirstName + ' ' + l.LastName;
        
            if(l.LeadSource == 'Website' && (l.Lead_Subtype__c == 'Free Resource' || l.Lead_Subtype__c == 'Contact Requested' || l.Lead_Subtype__c == 'Connect with Advisor' || l.Lead_Subtype__c == 'WMC Footer') && l.Referring_Brand_Id__c == '0010H00002SuHKtQAN'){
                l.OwnerId = '0050H0000090O6HQAU';
                }

            if(l.LeadSource == 'Website' && (l.Lead_Subtype__c == 'Free Resource' || l.Lead_Subtype__c == 'Contact Requested' || l.Lead_Subtype__c == 'Connect with Advisor' || l.Lead_Subtype__c == 'WMC Footer') && l.Referring_Brand_Id__c == '0010H00002Vv9gqQAB'){
                l.OwnerId = '0050H00000BwRULQA3'; 
                }    
            if(l.LeadSource == 'Website' && (l.Lead_Subtype__c == 'Free Resource' || l.Lead_Subtype__c == 'Contact Requested' || l.Lead_Subtype__c == 'Connect with Advisor' || l.Lead_Subtype__c == 'WMC Footer') && l.Referring_Brand_Id__c == '0010H00002PZz8zQAD'){
                l.OwnerId = '0050H000009LmB3QAK';
                }
            if(l.LeadSource == 'Website' && (l.Lead_Subtype__c == 'Free Resource' || l.Lead_Subtype__c == 'Contact Requested' || l.Lead_Subtype__c == 'Connect with Advisor' || l.Lead_Subtype__c == 'WMC Footer') && l.Referring_Brand_Id__c == '001i000001siaFLAAY'){
                l.OwnerId = '005i0000007xZy2AAE';
                }
            if(l.LeadSource == 'Website' && (l.Lead_Subtype__c == 'Free Resource' || l.Lead_Subtype__c == 'Contact Requested' || l.Lead_Subtype__c == 'Connect with Advisor' || l.Lead_Subtype__c == 'WMC Footer') && l.Referring_Brand_Id__c == '001i000001R92ADAAZ'){
                l.OwnerId = '005i0000004jpjbAAA';  
                }
            if(l.LeadSource == 'Website' && (l.Lead_Subtype__c == 'Free Resource' || l.Lead_Subtype__c == 'Contact Requested' || l.Lead_Subtype__c == 'Connect with Advisor' || l.Lead_Subtype__c == 'WMC Footer') && l.Referring_Brand_Id__c == '0010H00002LTwKnQAL'){
                l.OwnerId = '0050H0000090Mz0QAE';  
                }
            if(l.LeadSource == 'Website' && (l.Lead_Subtype__c == 'Free Resource' || l.Lead_Subtype__c == 'Contact Requested' || l.Lead_Subtype__c == 'Connect with Advisor' || l.Lead_Subtype__c == 'WMC Footer') && l.Referring_Brand_Id__c == '001i000000Rr2XRAAZ'){
                l.OwnerId = '005i0000006ozUbAAI'; 
                }
            if(l.LeadSource == 'Website' && (l.Lead_Subtype__c == 'Free Resource' || l.Lead_Subtype__c == 'Contact Requested' || l.Lead_Subtype__c == 'Connect with Advisor' || l.Lead_Subtype__c == 'WMC Footer') && l.Referring_Brand_Id__c == '001i000001lbahkAAA'){
                l.OwnerId = '005i0000007CtxhAAC';  
                }
            if(l.LeadSource == 'Website' && (l.Lead_Subtype__c == 'Free Resource' || l.Lead_Subtype__c == 'Contact Requested' || l.Lead_Subtype__c == 'Connect with Advisor' || l.Lead_Subtype__c == 'WMC Footer') && l.Referring_Brand_Id__c == '0010H00002WhyBJQAZ'){
                l.OwnerId = '0050H00000BNF5XQAX';  
                }
            if(l.LeadSource == 'Website' && (l.Lead_Subtype__c == 'Free Resource' || l.Lead_Subtype__c == 'Contact Requested' || l.Lead_Subtype__c == 'Connect with Advisor' || l.Lead_Subtype__c == 'WMC Footer') && l.Referring_Brand_Id__c == '0010H00002Gbu8mQAB'){
                l.OwnerId = '0050H00000A9mp3QAB';  
                }
            if(l.LeadSource == 'Website' && (l.Lead_Subtype__c == 'Free Resource' || l.Lead_Subtype__c == 'Contact Requested' || l.Lead_Subtype__c == 'Connect with Advisor' || l.Lead_Subtype__c == 'WMC Footer') && l.Referring_Brand_Id__c == '0010H00002QeX7JQAV'){
                l.OwnerId = '0050H0000090IR1QAM';  
                } 
            if(l.LeadSource == 'Website' && (l.Lead_Subtype__c == 'Free Resource' || l.Lead_Subtype__c == 'Contact Requested' || l.Lead_Subtype__c == 'Connect with Advisor' || l.Lead_Subtype__c == 'WMC Footer') && l.Referring_Brand_Id__c == '0010H00002Wk18hQAB'){
                l.OwnerId = '0050H00000BwWykQAF';  
                }
            if(l.LeadSource == 'Website' && (l.Lead_Subtype__c == 'Free Resource' || l.Lead_Subtype__c == 'Contact Requested' || l.Lead_Subtype__c == 'Connect with Advisor' || l.Lead_Subtype__c == 'WMC Footer') && l.Referring_Brand_Id__c == '0010H00002QeXBVQA3'){
                l.OwnerId = '0050H0000090HG2QAM';  
                }
            if(l.LeadSource == 'Website' && (l.Lead_Subtype__c == 'Free Resource' || l.Lead_Subtype__c == 'Contact Requested' || l.Lead_Subtype__c == 'Connect with Advisor' || l.Lead_Subtype__c == 'WMC Footer') && l.Referring_Brand_Id__c == '0010H00002JJNojQAH'){
                l.OwnerId = '0050H000009LjrVQAS';  
                }
            if(l.LeadSource == 'Website' && (l.Lead_Subtype__c == 'Free Resource' || l.Lead_Subtype__c == 'Contact Requested' || l.Lead_Subtype__c == 'Connect with Advisor' || l.Lead_Subtype__c == 'WMC Footer') && l.Referring_Brand_Id__c == '0010H00002WhxgEQAR'){
                l.OwnerId = '0050H00000BpsLdQAJ';  
                }
            if(l.LeadSource == 'Website' && (l.Lead_Subtype__c == 'Free Resource' || l.Lead_Subtype__c == 'Contact Requested' || l.Lead_Subtype__c == 'Connect with Advisor' || l.Lead_Subtype__c == 'WMC Footer') && l.Referring_Brand_Id__c == '001i000001p21CnAAI'){
                l.OwnerId = '005i0000007E1poAAC';  
                }
            if(l.LeadSource == 'Website' && (l.Lead_Subtype__c == 'Free Resource' || l.Lead_Subtype__c == 'Contact Requested' || l.Lead_Subtype__c == 'Connect with Advisor' || l.Lead_Subtype__c == 'WMC Footer') && l.Referring_Brand_Id__c == '001i000000N2CBbAAN'){
                l.OwnerId = '005i00000058XOlAAM'; 
                }
            if(l.LeadSource == 'Website' && (l.Lead_Subtype__c == 'Free Resource' || l.Lead_Subtype__c == 'Contact Requested' || l.Lead_Subtype__c == 'Connect with Advisor' || l.Lead_Subtype__c == 'WMC Footer') && l.Referring_Brand_Id__c == '001i000000N2CJ8AAN'){
                l.OwnerId = '005i0000007CVm0AAG'; 
                }
            if(l.LeadSource == 'Website' && (l.Lead_Subtype__c == 'Free Resource' || l.Lead_Subtype__c == 'Contact Requested' || l.Lead_Subtype__c == 'Connect with Advisor' || l.Lead_Subtype__c == 'WMC Footer') && l.Referring_Brand_Id__c == '0010H00002QectbQAB'){
                l.OwnerId = '0050H0000090OXcQAM';  
                }
}
}

Test Class:
 
@isTest
public class PartnerLeadAssignmentTest {
    static testMethod void insertLead(){
        
        List<Lead> leadList = new List<Lead>();
        Lead l = New Lead(Company = '', FirstName = 'First', LastName = 'Last', leadSource = 'Website', lead_SubType__c = 'Contact Requested');
        leadList.add(l);
        if(l.Referring_Brand_Id__c == '0010H00002SuHKtQAN'){  
        
 	       insert l;
        
    	    l=[Select Company, FirstName, LastName, Referring_Brand_Id__c, LeadSource, OwnerId, RecordTypeId FROM Lead Where Id =:l.Id];
        	System.assertEquals('0050H0000090O6HQAU', l.OwnerId);
           }
        if(l.Referring_Brand_Id__c == '0010H00002Vv9gqQAB'){ 
            
            insert l;
            
            l=[Select Company, FirstName, LastName, Referring_Brand_Id__c, LeadSource, OwnerId, RecordTypeId FROM Lead Where Id =:l.Id];
        	System.assertEquals('0050H00000BwRULQA3', l.OwnerId); 
        }
        
        
    }
}

 
Best Answer chosen by Travis Dvorak
Andrew GAndrew G
Hi
Ok, first , I greatly dislike the idea of putting hard coded Ids in triggers.  It makes them quite limited.  If I could, I would do some sort of query to get the details.
Perhaps have some sort config document that contains the connection between Referring Brand and the Lead Owner and do a query to that record to do the mapping.  That would remove the hard coding in the trigger.
But, we have hard coded values for the minutes so working with those..... first thing I notice is the repitition of the IF statement values, so lets look at fixing that so it a bit easier to read: Lets use a Switch statement - (note this is uncompiled code so may have errors)
trigger PartnerLeadAssignment on Lead (before insert) {
    
    List<Lead> leadList = new List<Lead>();
    for(Lead l : trigger.new){
        leadList.add(l);   //why are we adding to a list we don't do anything with?????
        if(l.Company == null){
            l.Company = l.FirstName + ' ' + l.LastName;
        
            if(l.LeadSource == 'Website' && (l.Lead_Subtype__c == 'Free Resource' || l.Lead_Subtype__c == 'Contact Requested' || l.Lead_Subtype__c == 'Connect with Advisor' || l.Lead_Subtype__c == 'WMC Footer')) {

		switch on l.Referring_Brand_Id__c {
			when '0010H00002SuHKtQAN' { l.OwnerId = '0050H0000090O6HQAU'; }
			when '0010H00002Vv9gqQAB' { l.OwnerId = '0050H00000BwRULQA3'; }
			when '0010H00002PZz8zQAD' { l.OwnerId = '0050H000009LmB3QAK'; }
			when '001i000001siaFLAAY' { l.OwnerId = '005i0000007xZy2AAE'; }
			when '001i000001R92ADAAZ' { l.OwnerId = '005i0000004jpjbAAA'; }
			when '0010H00002LTwKnQAL' { l.OwnerId = '0050H0000090Mz0QAE'; }
			when '001i000000Rr2XRAAZ' { l.OwnerId = '005i0000006ozUbAAI'; }
			when '001i000001lbahkAAA' { l.OwnerId = '005i0000007CtxhAAC'; }
			when '0010H00002WhyBJQAZ' { l.OwnerId = '0050H00000BNF5XQAX'; }
			when '0010H00002Gbu8mQAB' { l.OwnerId = '0050H00000A9mp3QAB'; }
			when '0010H00002QeX7JQAV' { l.OwnerId = '0050H0000090IR1QAM'; } 
			when '0010H00002Wk18hQAB' { l.OwnerId = '0050H00000BwWykQAF'; }
			when '0010H00002QeXBVQA3' { l.OwnerId = '0050H0000090HG2QAM'; }
			when '0010H00002JJNojQAH' { l.OwnerId = '0050H000009LjrVQAS'; }
			when '0010H00002WhxgEQAR' { l.OwnerId = '0050H00000BpsLdQAJ'; }
			when '001i000001p21CnAAI' { l.OwnerId = '005i0000007E1poAAC'; }
			when '001i000000N2CBbAAN' { l.OwnerId = '005i00000058XOlAAM'; }
            when '001i000000N2CJ8AAN' { l.OwnerId = '005i0000007CVm0AAG'; }
			when '0010H00002QectbQAB' { l.OwnerId = '0050H0000090OXcQAM'; }
		} //end switch
		} //end if leadsource website
	}//end if company null
	}//end loop
}//end trigger

Now that we have simplified the trigger (althought we still have hard coded values). A test class could work as below (again, uncompiled code).
@isTest
public class PartnerLeadAssignmentTest {
    static testMethod void insertLead_one(){
        
        List<Lead> leadList = new List<Lead>();
        Lead l = New Lead(FirstName = 'First', LastName = 'Last', leadSource = 'Website', lead_SubType__c = 'Free Resource');
	l.Referring_Brand_Id__c == '0010H00002SuHKtQAN';
        insert l;
        
    	insertedLead =[Select Company, Referring_Brand_Id__c, OwnerId FROM Lead Where Id =:l.Id];
	System.assertEquals('First Last', Company);
	System.assertEquals('0050H0000090O6HQAU', insertedLead.OwnerId);
       
    } //end test one

    static testMethod void insertLead_two(){
        List<Lead> leadList = new List<Lead>();
        Lead l = New Lead(FirstName = 'First', LastName = 'Last', leadSource = 'Website', lead_SubType__c = 'Contact Requested');
	l.Referring_Brand_Id__c == '0010H00002Vv9gqQAB' ;
        insert l;
        
    	insertedLead =[Select Company, Referring_Brand_Id__c, OwnerId FROM Lead Where Id =:l.Id];
//	System.assertEquals('First Last', Company); - we have tested this once, so don't really need to test it again.
	System.assertEquals'0050H00000BwRULQA3'; insertedLead.OwnerId);
       
   } //end test two

	// rinse / repeat as required - also checking the different sub types.

}


Hope the above helps.
Note that the test class should still work with the trigger in it's original form.

Regards
Andrew

All Answers

Andrew GAndrew G
Hi
Ok, first , I greatly dislike the idea of putting hard coded Ids in triggers.  It makes them quite limited.  If I could, I would do some sort of query to get the details.
Perhaps have some sort config document that contains the connection between Referring Brand and the Lead Owner and do a query to that record to do the mapping.  That would remove the hard coding in the trigger.
But, we have hard coded values for the minutes so working with those..... first thing I notice is the repitition of the IF statement values, so lets look at fixing that so it a bit easier to read: Lets use a Switch statement - (note this is uncompiled code so may have errors)
trigger PartnerLeadAssignment on Lead (before insert) {
    
    List<Lead> leadList = new List<Lead>();
    for(Lead l : trigger.new){
        leadList.add(l);   //why are we adding to a list we don't do anything with?????
        if(l.Company == null){
            l.Company = l.FirstName + ' ' + l.LastName;
        
            if(l.LeadSource == 'Website' && (l.Lead_Subtype__c == 'Free Resource' || l.Lead_Subtype__c == 'Contact Requested' || l.Lead_Subtype__c == 'Connect with Advisor' || l.Lead_Subtype__c == 'WMC Footer')) {

		switch on l.Referring_Brand_Id__c {
			when '0010H00002SuHKtQAN' { l.OwnerId = '0050H0000090O6HQAU'; }
			when '0010H00002Vv9gqQAB' { l.OwnerId = '0050H00000BwRULQA3'; }
			when '0010H00002PZz8zQAD' { l.OwnerId = '0050H000009LmB3QAK'; }
			when '001i000001siaFLAAY' { l.OwnerId = '005i0000007xZy2AAE'; }
			when '001i000001R92ADAAZ' { l.OwnerId = '005i0000004jpjbAAA'; }
			when '0010H00002LTwKnQAL' { l.OwnerId = '0050H0000090Mz0QAE'; }
			when '001i000000Rr2XRAAZ' { l.OwnerId = '005i0000006ozUbAAI'; }
			when '001i000001lbahkAAA' { l.OwnerId = '005i0000007CtxhAAC'; }
			when '0010H00002WhyBJQAZ' { l.OwnerId = '0050H00000BNF5XQAX'; }
			when '0010H00002Gbu8mQAB' { l.OwnerId = '0050H00000A9mp3QAB'; }
			when '0010H00002QeX7JQAV' { l.OwnerId = '0050H0000090IR1QAM'; } 
			when '0010H00002Wk18hQAB' { l.OwnerId = '0050H00000BwWykQAF'; }
			when '0010H00002QeXBVQA3' { l.OwnerId = '0050H0000090HG2QAM'; }
			when '0010H00002JJNojQAH' { l.OwnerId = '0050H000009LjrVQAS'; }
			when '0010H00002WhxgEQAR' { l.OwnerId = '0050H00000BpsLdQAJ'; }
			when '001i000001p21CnAAI' { l.OwnerId = '005i0000007E1poAAC'; }
			when '001i000000N2CBbAAN' { l.OwnerId = '005i00000058XOlAAM'; }
            when '001i000000N2CJ8AAN' { l.OwnerId = '005i0000007CVm0AAG'; }
			when '0010H00002QectbQAB' { l.OwnerId = '0050H0000090OXcQAM'; }
		} //end switch
		} //end if leadsource website
	}//end if company null
	}//end loop
}//end trigger

Now that we have simplified the trigger (althought we still have hard coded values). A test class could work as below (again, uncompiled code).
@isTest
public class PartnerLeadAssignmentTest {
    static testMethod void insertLead_one(){
        
        List<Lead> leadList = new List<Lead>();
        Lead l = New Lead(FirstName = 'First', LastName = 'Last', leadSource = 'Website', lead_SubType__c = 'Free Resource');
	l.Referring_Brand_Id__c == '0010H00002SuHKtQAN';
        insert l;
        
    	insertedLead =[Select Company, Referring_Brand_Id__c, OwnerId FROM Lead Where Id =:l.Id];
	System.assertEquals('First Last', Company);
	System.assertEquals('0050H0000090O6HQAU', insertedLead.OwnerId);
       
    } //end test one

    static testMethod void insertLead_two(){
        List<Lead> leadList = new List<Lead>();
        Lead l = New Lead(FirstName = 'First', LastName = 'Last', leadSource = 'Website', lead_SubType__c = 'Contact Requested');
	l.Referring_Brand_Id__c == '0010H00002Vv9gqQAB' ;
        insert l;
        
    	insertedLead =[Select Company, Referring_Brand_Id__c, OwnerId FROM Lead Where Id =:l.Id];
//	System.assertEquals('First Last', Company); - we have tested this once, so don't really need to test it again.
	System.assertEquals'0050H00000BwRULQA3'; insertedLead.OwnerId);
       
   } //end test two

	// rinse / repeat as required - also checking the different sub types.

}


Hope the above helps.
Note that the test class should still work with the trigger in it's original form.

Regards
Andrew
This was selected as the best answer
Andrew GAndrew G
Sorry, also realised I didn't explain why your test code wasn't covering anything.

Your trigger is Insert, so we have to insert records.  If we check your test class, you were creating a Lead with no Brand detail and then trying to test with an IF statement for Brand Id - there fore the code would not go into that branch.  And the other thing was there was no insert.  You were close in that you were attempting to query for the Lead record, and test against that record, but it had not been inserted.

HTH
Regards
Andrew
Travis DvorakTravis Dvorak

Andrew, I can't thank you enough for the detailed explanation to all of this.  Definitely helps my amature self learn a bit more about development!  I have a hard time with the query aspect of the referring_brand_Id__c field.  The way that field is used is the following: 

A prospect fills out a form that generates a lead.  The specific form, depending on which site it stemmed from, has a Referring Brand Id value passed through to the lead record that is created.  That Referring Brand Id field would be the Account Id of the office who the lead owner resides.  Each user has a text field that references the Account Id, which is titled "Brand Id".  It's a mess, but it works how I need it to.  

Travis DvorakTravis Dvorak

I tired to go about doing a query to get the Id, and ran into an issue of "Illegal Assignment:  from List to String"

l.Referring_Brand_Id__c = [Select Id from Account Where Name = 'ACME'];

Then tried:
List<Account> accountList = [SELECT Id from Account where Name = 'ACME'];
accontlist.add(Referring_Brand_Id__c);

But that had a different error.  Sorry for all the questions

Andrew GAndrew G
No worries.
Lets see if we can help.

Lets talk about the query first.  When we do a query we return a list of sObjects, not just the Id that is queried.
If we do 
[SELECT Id FROM Account WHERE Name = 'ACME'];
It will return a list of Account records (objects) but the only detail that is contained in the Object is the Id of the record that matches the WHERE statement. Similarly, if we query
[SELECT Name FROM Account WHERE Name = 'ACME'];  we don't get a single String containing the value ACME.  We receive an object whose only detail is "Name".  
Now, the 2nd piece of code,
List<Account> accountList = [SELECT Id from Account where Name = 'ACME'];
accontlist.add(Referring_Brand_Id__c);
What is happening is that we have a list of Accounts contained in the accountList.
When you do the .add what you are adding is ...well... nothing.  That is a field name.  Using the .add method we need to add an Account record to the list. However, keep in mind that if we add a new record to a queried list, then we need to use the upsert method, if that is what we are trying to do.
So, if I understand what you are trying to do, which is grab the Account record and test the Referring Brand Id field.
 
List<Account> accountList = new List<Account>([SELECT Id,Referring_Brand_Id__c from Account where Name = 'ACME']);

for (Account acct : accountList ) {
    System.debug('##debug loop - account ref Id ' + acct.Referring_Brand_Id__c);
}

//you could also access the list directly
If (accountList.size() == 1 ) {
    System.debug('##debug - one entry in list ' + accountList[0].Referring_Brand_Id__c);
} else if ( accountList.size > 1 ) {
    for (integer i = 0; i < accountList.size(); i ++ ) {
    System.debug('##debug - loop the list ' + accountList[i].Referring_Brand_Id__c);
  }
}

hope that explanation helps

Regards
Andrew

p.s. all code is uncompiled. ;-)