+ Start a Discussion
JN22JN22 

Increase Test Coverage for Campaign Trigger

Hello,

I have a trigger that fires when a campaign is created and changes the default Member Status of Responded on the campaign to Registered, and also adds another status of Attended.  The trigger works well, but my test class is not covering line 19-26.  Does anyone know how I can get the test to cover these lines?

Trigger:
trigger CampaignMemberStatus on Campaign (after insert) {

if(checkRecursiveAI.runOnceAI())
{
   
    List<Campaign> newCamps = [select Id from Campaign where Id IN :trigger.new AND ParentID = Null];
    List<CampaignMemberStatus> cms = new List<CampaignMemberStatus>();
    Set<Id> camps = new Set<Id>();
    List<CampaignMemberStatus> cms2Delete = new List<CampaignMemberStatus>();
    List<CampaignMemberStatus> cms2Insert = new List<CampaignMemberStatus>();
   
    for(Campaign camp : newCamps){
      
            camps.add(camp.Id);
    }  
   
   
   for (CampaignMemberStatus cm: [Select Id, Label, CampaignID  FROM CampaignMemberStatus WHERE CampaignID IN :camps]){
      if(cm.Label == 'Responded' ){
            CampaignMemberStatus cms1 = new CampaignMemberStatus(CampaignId=cm.CampaignID, Label='Registered', HasResponded=false, IsDefault = False, SortOrder=4);          
            System.debug(cms1);
            cms2Delete.add(cm);
            cms2Insert.add(cms1);
           
            CampaignMemberStatus cms3 = new CampaignMemberStatus(CampaignId = cm.CampaignId, HasResponded=true, Label = 'Attended', SortOrder = 5);
            cms2Insert.add(cms3);

      }

    }
    //perform insert before delete because system requires at least one CMS for a Campaign
    insert cms2Insert;
    delete cms2Delete;

}

}

Test Class:

@isTest private class CampaignMembStatus{

    @isTest private static void test_Memb_Status() {
    
        Campaign camp=new Campaign(Name='Test Campaign',IsActive=True);
        insert camp;

    Test.StartTest();
    Test.StopTest();
        
    }
}


Pavan Kumar KajaPavan Kumar Kaja
JN22,

To cover those lines insert one more  Campaignmember with Status = 'Responded'

Campaign camp1=new Campaign(Name='Test Campaign1',IsActive=True,Status = 'Responded');
	insert camp1;




JN22JN22
Hi Ashi,

Thanks.  I inserted that but it did not do anything to cover the lines.  It remains at 64%.  Any other ideas?  Thanks.