4

Still new to all this, thank you in advance for your responses!

I have the following trigger which inserts a custom object record on Campaign Member update:

trigger Insert_Registration_on_member_status_change on CampaignMember (after update) {
List<miiEventV1__Registration__c> reg = new List<miiEventV1__Registration__c>();
    for (CampaignMember cm : Trigger.new)
         if (CampaignMember.Status != null){
                 reg.add (new miiEventV1__Registration__c(
                     miiEventV1__Client__c = cm.Account_ID__c,
                     miiEventV1__Presentation__c = cm.Related_Presentation_Id__c,
                     miiEventV1__Presentation_Ticket_Type__c =  cm.Create_Ticket_Id__c,
                     miiEventV1__Status__c = 'Registered',
                     miiEventV1__Active__c = TRUE));   
         }
   insert reg;
}

I am now stuck on writing the test class. Thanks to Thomas Taylor, this is as far as I got now:

@isTest
private class CampaignMemberInsertTest{
    private static testmethod void testUpdateCM() {
        Account acct = new Account (Name = 'Acme, Inc.');
        insert acct;
        string acctId = acct.id;



        Contact co1 = new Contact(
        co1.FirstName = 'First',
        co1.LastName = 'Last',
        co1.AccountId = acct.Id);

        insert  co1;
        string co1Id = co1.id;

        Campaign ca1 = new Campaign(
            Name = 'Testcampaign',
            IsActive = TRUE);

        insert ( ca1 );
        string ca1Id = ca1.id;

        CampaignMember m1 = new CampaignMember();
            m1.Contact.Id = co1.Id; 
            m1.Campaign.Id = ca1.Id;

        insert ( m1 ) ;

        m1.Status = 'Facilitator';
        update m1;

        //assert that data is as we expect it after trigger has run
        List<miiEventV1__Registration__c> regs = [SELECT
             miiEventV1__Client__c, miiEventV1__Status__c, miiEventV1__Active__c 
             FROM miiEventV1__Registration__c
             WHERE miiEventV1__Client__c = acct.Id AND miiEventV1__Active__c = TRUE AND miiEventV1__Status__c = 'Registered']
         System.assert(!regs.isEmpty());

        }
}


Error: Compile Error: unexpected token: 'acct.Id' at line 38 column 43  

As you can see, I am now getting the error "Error: Compile Error: unexpected token: 'acct.Id' at line 38 column 43"

Questions:

1) Our org is running PersonAccounts, does this change the code?

2) If not, how could I fix the test class with the correctly formatted account id for my new registration model?

Thank you very much for your help,

Robin

4

Unless you have a specific need to and include the SeeAllData annotation, your existing data won't be available to your test. Additionally, since your test may need to run in a variety of environments (full SB, Dev SB, Production), hard coding record ids is pretty much guaranteed to fail anyway.

So you need to create your Account, Contact and Campaign as well, and then check to see if the trigger did what it's supposed to. The below has not been tested, and I'm guessing a bit at a couple of things, but should give you the idea.:

@isTest
private class CampaignMemberInsertTest {
    private static testmethod void testUpdateCM() {
        //create & insert test data 
        //make sure to include all required fields and that it will pass any active Validation Rules
        Account acct = new Account (Name = 'Acme, Inc.');
        insert acct;

        Contact con = new Contact(
                          FirstName = 'Robin',
                          LastName = 'Koehler'
                          AccountId = acct.Id);
        insert con;   

        Campaign camp = new Campaign(
                            Name = 'Test',
                            IsActive = TRUE);            
        insert camp;

        CampaignMember m1 = new CampaignMember(
            Contact = con.Id,
            Campaign = camp.Id,
            Account_ID__c = acct.Id );
        insert ( m1 );

        // perform action that runs trigger
        m1.Status = 'Facilitator';
        update m1;

        //assert that data is as we expect it after trigger has run
        List<miiEventV1__Registration__c> regs = [SELECT
             miiEventV1__Client__c, miiEventV1__Status__c, miiEventV1__Active__c 
             FROM miiEventV1__Registration__c
             WHERE miiEventV1__Client__c = acct.Id AND miiEventV1__Active__c = TRUE AND miiEventV1__Status__c = 'Registered']
         System.assert(!regs.isEmpty());
     }
 }
| improve this answer | |
  • Thomas, thanks a Billion for this! I get it now and modified the trigger accordingly. Question updated with new error message. Very educational, much appreciated!!! – Robin Koehler Feb 3 '14 at 21:13
2

I ended up working it out with a lot of trial and error and a bit more research :) The problems were:

1) Since we are using PersonAccounts, the fields and values for the Account insert had to be modified

2) To insert a campaign member, the fields CampaignId and ContactId have to be populated, which are different from Campaign.Id and Contact.Id

3) ContactId on campaign members did not accept the acct.PersonContact.Id (it would come up as NULL), that is why it had to be inserted via an intermediate step "Account a = [Select PersonContactId From Account Where Id = :acctId];" .

Here is the final, working test class which delivers 100% code coverage:

@isTest
private class CampaignMemberUpdateTest{
    private static testmethod void testUpdateCM() {
        Account acct = new Account (
        LastName = 'PersonLast',
        RecordTypeId = '01290000000EBHR');

        insert acct;
        string acctId = acct.id;


        Campaign ca1 = new Campaign(
            Name = 'Testcampaign',
            IsActive = TRUE,
            Related_Presentation_Ticket_Type_2__c = 'a0W90000005ldXs',
            Related_Presentation__c = 'a0Y90000001E2im');

        insert ( ca1 );
        string ca1Id = ca1.id;

        Account a = [Select PersonContactId From Account Where Id = :acctId];

        CampaignMember m1 = new CampaignMember(
            ContactId = a.PersonContactId,
            CampaignId = ca1.Id);

        insert ( m1 ) ;

        m1.Status = 'Facilitator';
        update m1;


        //assert that data is as we expect it after trigger has run
        List<miiEventV1__Registration__c> regs = [SELECT
             miiEventV1__Client__c, miiEventV1__Active__c, miiEventV1__Status__c 
             FROM miiEventV1__Registration__c
             WHERE miiEventV1__Client__c = :acct.Id AND miiEventV1__Active__c = TRUE AND miiEventV1__Status__c = 'Registered'];
         System.assert(!regs.isEmpty());

        }
}
| improve this answer | |
  • Bravo! Nice work figuring all that out. – Thomas Taylor Feb 4 '14 at 4:09
  • Thanks again, Thomas :) I'd still be scratching my head without your pointers that specifically applied to my problem – Robin Koehler Feb 5 '14 at 6:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.