You need to sign in to do that
Don't have an account?

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
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
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
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.
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);
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.
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:
Thanks for all your help on this.
sales4ce.
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.
Hi John,
Thanks for the help.
I figured it out.
Thanks,
Sales4ce