+ Start a Discussion
Martha VMartha V 

Test class recording Status of Campaign Members wrong

Hi, I am testing a controller that sends emails to Campaign Members. My problem is that when I insert the Campaign Members, they get inserted with the wrong Status (not the one I set up in the test). I have even tried to retrieve the records inserted and change the status again and then do an update, but again the status during the test gets recorded with the wrong value.

I thought maybe it was a trigger of some sort (I am using the NSPS package), but when I add the campaign member through the UI it is not happening. It is only happening during the test.

Does anyone knows why this is happening?

Here is some of my test code, where I insert the records
        List<contact> contacts = new List<Contact>();
        for (Integer x = 1; x < 9; x++)
            Contact cont = new Contact(FirstName = 'test'+x,
                                       LastName = 'test ' + x,
                                       email = 'marthacv@gmail.com');
            if (x==8) cont.email = '';
        insert contacts;
        List<Campaign> campaigns = new List<Campaign>();
        for (integer x = 0; x<4; x++)
            Campaign camp = new Campaign(Name = 'Campaign ' + x,
                                         Type = campaignTypes[x],
                                         StartDate = campaignDates[x],
                                         IsActive = True,
                                         Status = 'Planned',
                                         EndDate = campaignDates[x],
                                         Description = 'testing emails',
                                         RecordTypeId = '012o0000000xwhO',
                                         Fee_for_Activity__c = fee.id);
        Database.SaveResult[] srList = Database.insert(campaigns, false);
       participant = new CampaignMember(ContactId = contacts[1].id,
                                         Status = 'Signed Up',
                                         CampaignId = campaigns[4].id);
        insert participants;

... after the inserts I run the test with several campaigns

        List<String> returnPages = new List<String>();
        List<PageReference> pgref = new List<PageReference>();        
        ApexPages.StandardController std1 =
            new ApexPages.StandardController(campaigns[0]);
        CMSendEmails firstTest = new CMSendEmails(std1);