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
Kevin Chiles 930Kevin Chiles 930 

Code Not Covered by Class

Hello!

I am trying to update a Campaign Member from a lead when a lead interaction occures.  The code works, but the code is not covered by the class for some odd reason.  Can anyone let me know what may be the issue?  I am only seeing 55 percent coverage with the items after the if statements not being covered: IE Lines 16-17  and 23-25
 
trigger UpdateLeadForTrial on CampaignMember (after insert,after update) {

for (CampaignMember cm: Trigger.New){


//Collect associated lead
List<Lead> led =[select Id, In_trial__c from Lead where Id =:cm.LeadId];

For( Lead l: Led){

//Logic for when to fire
if (l.In_Trial__c ==False && CM.Status=='Package Sent'){


//Updating value for In Trial field on Lead
l.In_Trial__c=True;
Update led;
}

//Running criteria for lead that is expired uncheck the In Trial field
if (l.In_Trial__c ==True && CM.Status=='Trial Complete'){

l.In_Trial__c=False;

Update led;
}}


}}

Test Class
@isTest(seeAllData=true)

private class testClass_UpdateLastModifiedDate
{
    public static testmethod void method1()
            {
        
        Lead l= new Lead(RecordTypeid=[select id,name,SobjectType from RecordType where Name ='SMB' and SobjectType='Lead'].id, Company='Test',LastName='test',
        leadsource='web',In_Trial__c=false,email='test@test.com',Install_Phone__c='999-999-9999',Status='0-Lead',OwnerId=[select id,LastName from User where Lastname='Sync' limit 1].id);
        insert l;
        
        task t2 =new Task(OwnerId=l.OwnerId,WhoId=l.Id,Subject='Call',Description='test',Status='In Progress');
        insert t2;
        
        Account Ac = new Account(MegaPath_Department__c='Channel',OwnerId=[select id,LastName from User where LastName='Sync' limit 1].id,
        Name='testAccountName11',Type='Customer');
        insert Ac;
        
        Contact c= new Contact(AccountId=ac.id, LastName='Chiles',LeadSource='Chat');
        Insert c;
        
        Opportunity Opp =New Opportunity(OwnerId=Ac.OwnerId,Name='test',AccountId=Ac.Id,StageName='Prospecting',CloseDate=system.today(),ContactForRole__c=c.Id,Number_of_Sites__c='5',
        LeadSource='360 Communications - T1',NextStep='test',Install_Street__c='test',Install_State__c='CA',Install_City__c='test',Install_Zip__c='94582',
        Install_Phone__c='3157775695');
        Insert Opp;
        
        Task t3 = new Task(  OwnerId=opp.OwnerId,WhoId=c.Id,Description='test', WhatId=opp.ID, Subject='Call',Status='In Progress');
        insert t3;        
        
        Task t1 = new Task(  OwnerId=ac.OwnerId,WhoId=c.Id,Description='test', WhatId=Ac.ID, Subject='Call',Status='In Progress');
        insert t1;
        //Task t2 = [Select Status,ID from Task where ID =:t1.ID];
       // insert t2;
        
        t1.Status='Completed';
        Update t1;
        
          Campaign cp=new Campaign(Name='MP1 Free Trial',IsActive=TRUE,Free_Trial_Campaign__c=TRUE);
        Insert cp;
        
         CampaignMember cm= New CampaignMember(CampaignId=Cp.Id,LeadId=l.Id,Status='Request');
        Insert cm;
       {
       
        
        CM.Status='Package Sent';
        Update CM;
        
        //l.In_Trial__c=True;
        //Update L;
        
        CM.Status='Trial Complete';
        Update CM;
        
        
        }
        l.Status='X - Lost/Nurture';
        l.Lead_Disposition__c='Lost To Competitor';
        update l;
        }
    }

 
Sagar LakhaniSagar Lakhani

Hi Kevin,

 

Please Try below code of Test Class & you get 100% code coverage.......

 

@isTest
public class SFUpdateLeadonCampaignMemberTest {
    static testMethod void testSFUpdateLeadonCampaignMember(){    

        Lead objLead = new Lead();
        objLead.LastName = 'TestLastName';
        objLead.Company = 'TestCompany';
        objLead.Status = 'closed - converted';
        objLead.In_Trial__c = False;
        insert objLead;

        Campaign objCampaign = new Campaign();
        objCampaign.Name = 'TestCamName';
        insert objCampaign;
       
        CampaignMemberStatus cms1 = new CampaignMemberStatus();
        cms1.CampaignId = objCampaign.id;
        cms1.Label = 'Package sent';
        cms1.SortOrder = 4;
        insert cms1;
        
        CampaignMember objCamMem = New CampaignMember();
        objCamMem.CampaignId = objCampaign.id;
        objCamMem.Status = cms1.Label;
        objCamMem.LeadId = objLead.id;   
        insert objCamMem;
		  	
        objLead = [SELECT id,status FROM Lead WHERE id =: objLead.id];
        objLead.In_Trial__c = true;
        update objLead;
        
        Campaign objCampaign2 = new Campaign();
        objCampaign2.Name = 'TestCamName2';
        insert objCampaign2;
                
        CampaignMemberStatus cms2 = new CampaignMemberStatus();
        cms2.CampaignId = objCampaign2.id;
        cms2.Label = 'Trial Complete';
        cms2.SortOrder = 5;
        insert cms2;
        
        CampaignMember objCamMem2 = New CampaignMember();
        objCamMem2.CampaignId = objCampaign2.id;
        objCamMem2.Status = cms2.Label;
        objCamMem2.LeadId = objLead.id;   
        insert objCamMem2;
                     
    }
}



Thanks & Cheers,

 

Sagar Lakhani.