You need to sign in to do that
Don't have an account?

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:
Test Class:
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); } } }
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)
Now that we have simplified the trigger (althought we still have hard coded values). A test class could work as below (again, uncompiled code).
Hope the above helps.
Note that the test class should still work with the trigger in it's original form.
Regards
Andrew
All Answers
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)
Now that we have simplified the trigger (althought we still have hard coded values). A test class could work as below (again, uncompiled code).
Hope the above helps.
Note that the test class should still work with the trigger in it's original form.
Regards
Andrew
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
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.
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
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 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, 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.
hope that explanation helps
Regards
Andrew
p.s. all code is uncompiled. ;-)