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 

Test class for Trigger-Help

Hi,

 

I have a trigger (before Insert) on Campaign Member that would insert tasks to contact/Leads on a campaign whenever a Contact/Lead is added to it.

I am unable to figure out how i can i write a test class to cover that trigger.

 

I am assuming that we need to insert Campaign Member's which would then fire off the trigger.

But how do i insert a campaign member using apex?

 

Can any one help me out on writing this test method.

 

Thanks in advance,

Sales4ce

 

Best Answer chosen by Admin (Salesforce Developers) 
jkucerajkucera

To insert a campaign member, just give it a leadId or contactID + a campaign Id

 

http://www.salesforce.com/us/developer/docs/api/Content/sforce_api_objects_campaignmember.htm#kanchor119

 

To test it, create the objects and verify the result w/ a query and system.assert

All Answers

jkucerajkucera

To insert a campaign member, just give it a leadId or contactID + a campaign Id

 

http://www.salesforce.com/us/developer/docs/api/Content/sforce_api_objects_campaignmember.htm#kanchor119

 

To test it, create the objects and verify the result w/ a query and system.assert

This was selected as the best answer
sales4cesales4ce

Hi John,

 

Thanks for your reply!

I tried to do the same but somehow unable to compile it.

Can you let me know what i am doing wrong.

 

Here is what i am upto.

 

Public Class trigger_Testcls
{
    Static testMethod Void test_CM_Trigger()
    {
        Campaign camp=new Campaign(Name='Test Campaign',Special_Campaign__c=True,IsActive=True);
        List<Contact> ct= new List<contact>();
        List<Lead> ld=new List<lead>();
        
        for(Integer i=0;i<100;i++)
        {
            Contact c=New Contact(FirstName='Test',LastName='Contact');
            ct.add(c);
            Lead l= New Lead(LastName='Test',Status='Open - Not Contacted',Company='Leads');
            ld.add(l);
        }
        Insert ct;
        Insert ld;
        
        List<CampaignMember> cm=New List<CampaignMember>();
        
        for(Integer i=0;i<100;i++)
        {
            CampaignMember Cl=New CampaignMember(CampaignId=camp.Id,contact=ct[i].Id);
            cm.add(Cl);
        }
        Test.StartTest();
        Insert cm;
        Test.StopTest();
    }
}
jkucerajkucera

I believe you got the solution in another thread - you need to use ContactID, not Contact, for the campaign member creation.

 

            CampaignMember Cl=New CampaignMember(C ampaignId=camp.Id,contactId=ct[i].Id);
sales4cesales4ce

Yes John, helpful as always!

Thanks for your help! I had to duplicate the post, sorry for that as i was in hurry to get it done.

I would let you know once i am done with the test case.

 

sales4cesales4ce

Hi John,

 

Here is my Test Class. It works fine and i get 100% code coverage.

But i wanted to know how we could use System.assertequals() in my situation.

 

My Trigger creates tasks to contacts/leads that are added to a campaign. So how would i use system.assertequals() to see if the task is created properly or not.

 

Here is my test method:

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;
        List<Contact> ct= new List<contact>();
      
        
        for(Integer i=0;i<200;i++)
        {
            Contact c=New Contact(FirstName='Test',LastName='Contact');
            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();
        
        
    }
    
    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');
            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();
    }
}

Thanks for all your help on this.

 

sales4ce.

jkucerajkucera

Check out this post for test best practices:

http://wiki.developerforce.com/index.php/An_Introduction_to_Apex_Code_Test_Methods

 

You want to use system.assertequals() to confirm that what you want to happen actually happened.  In your case it seems you want to ensure that the task was created, so after inserting the campaign member, you could query for the task, and assert that the # of tasks created = 1.

sales4cesales4ce

Hi John,

 

Thanks for the help.

I figured it out.

 

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;
        List<Contact> ct= new List<contact>();
      
        
        for(Integer i=0;i<200;i++)
        {
            Contact c=New Contact(FirstName='Test',LastName='Contact');
            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 from Task where Whoid IN : ct];
        for(Task t : inserted_Tasks)
        {
            System.assertEquals('Testing contact',t.Subject);
        }
        
    }
    
    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');
            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 from Task where Whoid IN : l];
        for(Task t : inserted_Tasks)
        {
            System.assertEquals('Testing Lead',t.Subject);
        }
    }
}

 

Thanks,

Sales4ce