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
sales4cesales4ce 

Apex Trigger Coverage

Hi,

 

i have a trigger on Campaign member object that would add tasks to them.

Now the task  subject should be assigned dynamically.

 

what i mean by dynamically means, subject of task should be in the following way:

Contact/Lead name, Account Name/Company, Quarter, Year

 

Assume a campaign member of type Contact is added. Contact name is Peter Ray and is associated with a Account salesforce.

 

Subject should be : Peter ray, Salesforce, Q2, 2010.

how to get quarter is , dividing a year into 4 quarters starting from january.

 

I have the trigger running fine, but with a test class i am unable to cover some lines. the lines are highlighted in Red below.

 

Code:

 

trigger add_task on CampaignMember (before Insert) 
{
    Campaign c = [select Id,Special_Campaign__c from Campaign where Id = :Trigger.new[0].CampaignId];
    Date Present_Day=Date.today();
    DateTime Present_Day_Time=System.Now();
    integer Year=Present_Day.Year();
    integer Month=Present_Day.Month();
    Public String getQuarter()
    {
        If(Month==1 || Month==2 || Month==3)
            return 'Q1';
        else if(Month==4 || Month==5 || Month==6)
            return 'Q2';
        else if (Month==7 || Month==8 || Month==9)
            return 'Q3';
        else
            return 'Q4';
    }
    If(c.Special_Campaign__c==True)
    {
    List<Id> Contact_Lead_Ids= new List<Id>();
    
    for(CampaignMember cm : Trigger.New)
    {
        Contact_Lead_Ids.add(cm.ContactId);
        Contact_Lead_Ids.add(cm.LeadId);
    }
    
    List<Contact> contacts=[Select Id,OwnerId,Account.Name,FirstName,LastName from Contact where Id in :Contact_Lead_Ids];
    Map<Id,Contact> Cid_to_Sobject= new Map<Id,Contact>();
    
    List<Lead> leads=[select Id,Name,OwnerId,Company from Lead where Id IN :Contact_Lead_Ids];
    Map<Id,Lead> Lid_to_Sobject=new Map<Id,Lead>();
    
    for(Contact ct : contacts)
        Cid_to_Sobject.Put(ct.Id,ct);
    
    for(Lead l : leads)
        Lid_to_Sobject.Put(l.Id,l);
    
    List<Task> insert_Tasks=new List<Task>();
    for(CampaignMember cm2 : Trigger.New)
    {
        if(cm2.ContactId!=NULL)
        {
            Contact ct2=Cid_to_Sobject.get(cm2.ContactId);
            Task myTask=new Task(Whoid=ct2.Id,
                                 Subject=ct2.Account.Name +','+' '+ ct2.FirstName +' '+ct2.LastName + ','+' '+'CCD'+','+' '+getQuarter()+','+Year,
                                 ActivityDate=Present_Day,
                                 Priority='Medium',
                                 Type='Make a Call',
                                 Status='Started',                                 
                                 OwnerId=ct2.OwnerId);
            insert_Tasks.add(myTask);
            System.Debug('Task inserted to=='+ct2.Id);
        }
        else
        {
            Lead l2=Lid_to_Sobject.get(cm2.LeadId);
            Task myTask=new Task(WhoId=l2.Id,
                                 Subject=l2.Company + ','+' '+l2.Name+','+'CCD'+','+getQuarter()+','+Year,
                                 ActivityDate=Present_Day,
                                 Priority='Medium',
                                 Type='Make a Call',
                                 Status='Started',                                 
                                 OwnerId=l2.OwnerId);
            insert_Tasks.add(myTask);
            System.Debug('Task Inserted to Lead=='+l2.Id);
        }
    }
    Insert insert_Tasks;
   
}
}

Test class:

Public Class trigger_Testcls
{
    Static testMethod Void test_CM_Contacts()
    {
        Campaign camp=new Campaign(Name='Test Campaign',Special_Campaign__c=True,IsActive=True);
        Insert camp;
        Account acct= New Account(Name='Testing Account');
        Insert acct;
        List<Contact> ct= new List<contact>();
      
        
        for(Integer i=0;i<200;i++)
        {
            Contact c=New Contact(FirstName='Test',LastName='Contact',AccountId=acct.Id);
            ct.add(c);
           
        }
        Insert ct;
        
        
        List<CampaignMember> cm=New List<CampaignMember>();
        
        for(Integer i=0;i<200;i++)
        {
            CampaignMember Cts=New CampaignMember(CampaignId=camp.Id, ContactId=ct[i].Id);
            cm.add(Cts);
           
            
        }
        
        
        Test.StartTest();
        Insert cm;
        Test.StopTest();
        
        List<Task> inserted_Tasks= [Select Subject,Status from Task where Whoid IN : ct];
        for(Task t : inserted_Tasks)
        {
            System.assertEquals('Started',t.Status);
        }
        
    }
    
    Static testMethod Void test_CM_leads()
    {
        Campaign Camp=new Campaign(Name='Lead Campaign',Special_Campaign__c=True,isActive=True);
        Insert Camp;
       
        List<Lead> l= New List<Lead>();
        for(Integer i=0;i<200;i++)
        {
            Lead ld=New Lead(LastName='Testing Lead',Company='Leads',ownerId='005A0000000h2hh');
            l.add(ld);
        }
        Insert l;
        
        List<CampaignMember> cm= New List<CampaignMember>();
        for(Integer i=0;i<200;i++)
        {
            CampaignMember c= New CampaignMember(CampaignId=camp.Id, LeadId=l[i].Id);
            cm.add(c);
        }
        Test.StartTest();
        Insert cm;
        Test.StopTest();
        List<Task> inserted_Tasks= [Select Subject,Status from Task where Whoid IN : l];
        for(Task t : inserted_Tasks)
        {
            System.assertEquals('Started',t.Status);
        }
    }
}

How do i get coverage for the function that calculates quarter information.Any help/idea is highly appreciated.

 

Thanks,

Sales4ce

Best Answer chosen by Admin (Salesforce Developers) 
gtindugtindu

Currently your code is hard coded for present day values... there's not a way to test all entries into that code...

global class DateUtils
{
    public static string getQuarter(Integer month)
    {
        If(month >= 1 && month <= 3)
            return 'Q1';
        else if(month>=4 && month<=6)
            return 'Q2';
        else if (month>=7 && month<=9)
            return 'Q3';
        else
            return 'Q4';
    }
}
// Which you can test with the following lines
/* I add a test for 0, since it should return Q4 based on code... */
// System.assertEquals(DateUtils.getQuarter(0), 'Q4');
// System.assertEquals(DateUtils.getQuarter(1), 'Q1');
// System.assertEquals(DateUtils.getQuarter(2), 'Q1');
// System.assertEquals(DateUtils.getQuarter(3), 'Q1');
// System.assertEquals(DateUtils.getQuarter(4), 'Q2');
// System.assertEquals(DateUtils.getQuarter(5), 'Q2');
// System.assertEquals(DateUtils.getQuarter(6), 'Q2');
// System.assertEquals(DateUtils.getQuarter(7), 'Q3');
// System.assertEquals(DateUtils.getQuarter(8), 'Q3');
// System.assertEquals(DateUtils.getQuarter(9), 'Q3');
// System.assertEquals(DateUtils.getQuarter(10), 'Q4');
// System.assertEquals(DateUtils.getQuarter(11), 'Q4');
// System.assertEquals(DateUtils.getQuarter(12), 'Q4');
/* I add a test for 13, since it should return Q4 based on code... */
// System.assertEquals(DateUtils.getQuarter(13), 'Q4');



 

 

All Answers

gtindugtindu

You should look to moving the getQuarter into a helper class - and have it accept a variable for Month.  This will allow you to test it by passing in different values.

sales4cesales4ce

Thanks for your reply!

I haven't actually understood what you replied.Can you give me an example?

Thanks for your time.

 

Thanks,

Sales4ce

gtindugtindu

Currently your code is hard coded for present day values... there's not a way to test all entries into that code...

global class DateUtils
{
    public static string getQuarter(Integer month)
    {
        If(month >= 1 && month <= 3)
            return 'Q1';
        else if(month>=4 && month<=6)
            return 'Q2';
        else if (month>=7 && month<=9)
            return 'Q3';
        else
            return 'Q4';
    }
}
// Which you can test with the following lines
/* I add a test for 0, since it should return Q4 based on code... */
// System.assertEquals(DateUtils.getQuarter(0), 'Q4');
// System.assertEquals(DateUtils.getQuarter(1), 'Q1');
// System.assertEquals(DateUtils.getQuarter(2), 'Q1');
// System.assertEquals(DateUtils.getQuarter(3), 'Q1');
// System.assertEquals(DateUtils.getQuarter(4), 'Q2');
// System.assertEquals(DateUtils.getQuarter(5), 'Q2');
// System.assertEquals(DateUtils.getQuarter(6), 'Q2');
// System.assertEquals(DateUtils.getQuarter(7), 'Q3');
// System.assertEquals(DateUtils.getQuarter(8), 'Q3');
// System.assertEquals(DateUtils.getQuarter(9), 'Q3');
// System.assertEquals(DateUtils.getQuarter(10), 'Q4');
// System.assertEquals(DateUtils.getQuarter(11), 'Q4');
// System.assertEquals(DateUtils.getQuarter(12), 'Q4');
/* I add a test for 13, since it should return Q4 based on code... */
// System.assertEquals(DateUtils.getQuarter(13), 'Q4');



 

 

This was selected as the best answer
sales4cesales4ce

Thanks gtindu! It worked for me.

It now gave me a better understanding in writing test cases.

 

Thanks,

Sales4ce