2

My goal is to configure a trigger on the CampaignMember object in SF that, when fired, will send a marketing cloud email. We are using a Web2Lead form that will pass the campaign value into SF, which should be used to trigger the email to the person that submitted the form.

In configuring the object for triggers in SF, I used this code from the marketing cloud documentation (replacing Object Name with CampaignMember):

trigger Trig_Object Name on Object Name (after insert, after update) {
    et4ae5.triggerUtility.automate('Object Name');
}

Now I need to create the test class. This is the code someone sent me, but I think I have the required fields and values incorrect because I'm getting an error (I replaced 'field1 = 'test1" with 'Campaign=Event_1' and deleted field2 and 3):

@isTest(Seealldata = true)                                    
public class Trig_CampaignMemberTest {  
    static testmethod void InsertCampaignMember() {

    CampaignMember cmp = new CampaignMember('field1' = 'test1', 'field2' = 'test2','field3' = 'test3');
    Insert cmp;

    }
}

I received this error: Compile Error: SObject constructor must use name=value pairs at line 5 column 22

Am I missing something? Any feedback is highly appreciated. I'm new to this!

2
  • Can you show us the actual code you are currently using? Apr 27, 2015 at 21:20
  • Hi Jeremy, Here is my trigger: trigger Trig_CampaignMember on CampaignMember (after insert, after update) { et4ae5.triggerUtility.automate('CampaignMember'); } Here is my test Class (however, it's not giving me any code coverage): @isTest(Seealldata = true) public class Trig_CampaignMemberTest { static testmethod void InsertCampaignMember() { CampaignMember cmp = new CampaignMember(Status = 'test'); Insert cmp;}}
    – JennyBee
    Apr 28, 2015 at 19:34

2 Answers 2

2

When writing a test class, one way to think about it is to build code that does the same thing a User will do in Salesforce themselves.

In the case of a Campaign Member, the typical User process is something like this:

  1. Create an Account
  2. Create a Contact on that Account
  3. Create a Campaign
  4. Create a CampaignMember to add the Contact to the Campaign.

Likewise, in your Apex test code, you need to do all of these things. A CampaignMember is a detail object to Contact and Campaign, so you can't insert one without those other things already being there. Here is the bare minimum you would need to do all of these things:

static testmethod void sampleTest()
{
    Account a = new Account(Name = 'Account');
    insert a;

    Contact c = new Contact(AccountId = a.Id, LastName = 'Contact');
    insert c;

    Campaign camp = new Campaign(Name = 'test campaign');
    insert camp;

    test.startTest();

    CampaignMember testCM = new CampaignMember(CampaignId = camp.Id,                 
            ContactId = c.Id);
    insert testCM;

    test.stopTest();
}

This is not commented, and does not do any actual testing at all, since it doesn't make sure the code performs properly. I suggest you not just copy this into your class, but spend some time understanding how it works. Writing test code is integral to Apex programming.

4
  • Thanks for the help. What does Invalid initial expression type mean? That is what I get now that I removed the single quotes. If you don't mind, and at the risk of asking a dumb question, what exactly is the expression doing? CampaignMember cmp = new CampaignMember(Campaign = 'Event_') Is it creating a CampaignMember for the Event_ campaign? I think part of my problem is not understanding what I'm trying to accomplish with this testClass. :(
    – JennyBee
    Apr 27, 2015 at 19:07
  • The complete error is this:Compile Error: Invalid initial expression type for field Campaign, expecting: Campaign (or single row query result of that type) at line 5 column 52
    – JennyBee
    Apr 27, 2015 at 19:55
  • Sorry that I am burdening anybody.. that is not my intent. I have done research, plenty of it before posting anything here. Again, my apologies.
    – JennyBee
    Apr 28, 2015 at 19:33
  • Jeremy, thank you so much! You have no idea how much this means to me. I've been struggling with this for 2 weeks. As a new Salesforce admin, with no Apex coder resources (or experience for that matter) this is a tremendous help. I did end up getting this to work. I had to also add a Lead object since CampaignMembers can also be leads! Thanks again. Once I'm able, I'll upvote (or whatever you call it here!) you! :)
    – JennyBee
    Apr 30, 2015 at 15:54
1

To answer your question, the syntax is fieldname without quotes, literal value in quotes:

CampaignMember cmp = new CampaignMember(Campaign = 'test1');

Also as an aside, I would advise against using seealldata=true in your test class. You're starting on the correct approach by creating test data within your test, so there shouldn't be any need for you to use seealldata.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .