You need to sign in to do that
Don't have an account?
KevinSnell
Test Method Help - 37% Coverage
Hi All,
I'm new to coding and am struggling. I have the following code but am onlying getting 37% code coverage :
trigger trg_LeadToCampaign on Lead (after update, after insert) { //Create a new set to hold all the Leads with CampaignId - Set will contain information in <LeadId - CampaignId> format Set<String> existingCampaignMembers = new Set<String>(); //Query CampaignMember object and fetch all the members related to current context Leads for(CampaignMember cM : [Select LeadId, CampaignId from CampaignMember where LeadId IN: Trigger.newMap.keySet()]) { //Add in set existingCampaignMembers.add(cM.LeadId + '-' + cM.campaignId); } //Create a list to hold all the CampaignMember records to be inserted List<CampaignMember> cMembers = new List<CampaignMember>(); //Loop through Trigger.New records and check if member need to be created for(Lead l : Trigger.New) { //Check if campaign is not null if (l.HubSpot_Campaign__c != null ) { //Create string to check the existing leads String uniquenessCriteria = l.Id + '-' + l.HubSpot_Campaign__c; //Check in set if this lead is already associated with any campaign record //and only create the CampaignMember if Lead and Campaign is not found in set if(!existingCampaignMembers.contains(uniquenessCriteria)){ //Create a new Campign member record CampaignMember cm = new CampaignMember(); cm = new CampaignMember(); cm.LeadId = l.Id; cm.CampaignId = l.HubSpot_Campaign__c; cm.Status = 'Responded'; //Add this record in the list of Campaign Members to be inserted cMembers.add(cm); } } //Insert records in database if(cMembers.size() > 0) database.insert( cMembers, false); } }
@isTest private class test_trgr_leadtocampaign { public static testMethod void testleadtocampaign() { System.debug('VistageDebug: entering TriggerTests_LeadtoCampaign'); //Create Campaign Campaign testCampaign = new Campaign(Name='Test', IsActive=True); insert testCampaign; //Create Lead AND Add to Campaign Lead testLead = new Lead(FirstName='Bart', LastName='Simpson', Company='The Simpsons', Status=VistageConstants.LEAD_INITIAL_STATUS, Country='US', PostalCode='92130', Email='scooby@doo.com', List_Assignment__c = 'Reserve', RecordTypeId=VistageConstants.LEAD_RECORDTYPE_UK_Member_Candidate); insert testLead; //Create Campaign Member CampaignMember newcm = new CampaignMember (LeadId=testLead.Id, CampaignId=testCampaign.Id); insert newcm; //Create Lead Lead newLead = new Lead(FirstName='Scooby', LastName='Doo', Company='Mystery, Inc', Status=VistageConstants.LEAD_INITIAL_STATUS, Country='US', PostalCode='92130', Email='scooby@doo.com', List_Assignment__c = 'Reserve', RecordTypeId=VistageConstants.LEAD_RECORDTYPE_UK_Member_Candidate); insert newLead; //Add Campaign Id to Hubspot Campaign Field to fire trigger Lead updateMe = [SELECT Id FROM Lead WHERE Id = :newLead.Id]; updateMe.HubSpot_Campaign__c = testCampaign.Id; update updateMe; //Check Lead is in Campaign CampaignMember afterupdate = [SELECT Lead.id, CampaignId, HasResponded FROM CampaignMember WHERE lead.id= :newlead.id AND campaignid = :testcampaign.id]; System.assert(afterupdate.lead.id == newlead.id); } }
I'm struggling with the following pieces of code:
//Add in set existingCampaignMembers.add(cM.LeadId + '-' + cM.campaignId); }
And
//Create string to check the existing leads String uniquenessCriteria = l.Id + '-' + l.HubSpot_Campaign__c;
If anyone code help it would be really appreciated as I need to get this code deployed asap.
Thanks
Kev
You should not manually create Campaign Member in the test method. Remove the following lines and try again.
Unfortunately I removed the lines of code and it's still not covering.
These are the bits it's not covering with the test code: