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
Igor M.Igor M. 

Apex Trigger unable to find Campaign Members with a specific status

I have a trigger and a test class, neither of which is able to do a soql query to find a campaign member with the status 'registered'
The trigger works, but on UI it completely ignores the status part and just updates the campaign member anyway, while in test class it fails since it doesn't find any with this status. Am I just misunderstanding how the status gets populated?

Here is the query in my trigger:
   List<CampaignMember> cmList = [SELECT Id, PE_Changed__c, ContactId, Status 
                                   FROM CampaignMember 
                                   WHERE Status = 'Registered'];

Here is my setup in the test class:
        CampaignMember cm1 = new CampaignMember ();
        cm1.ContactId = testcon1.Id;
        cm1.CampaignId = testCampaign.id;
        cm1.Status = 'Registered';
        insert cm1;

When I run the test class and look at the debug log, I see this:
08:02:44:449 USER_DEBUG [85]|DEBUG|cm1: CampaignMember:{ContactId=0032a0000066rW1AAI, CampaignId=7012a000000A8kuAAC, Status=Registered, Id=00v2a000000T1yTAAS}

So I know the setup worked and the status is correctly set. The other interesting part is that if I change my trigger to Status = 'Sent' then it works fine.
We are using AAkonsult Campaign Status app to change campaign statuses, but I don't believe that this should cause any issues.

Any help would be appreciated as I'd like to prevent making so many updates on records that don't need to be updated.
devedeve
Hi Igor,

Status field is picklist in CampaignMember object so have you added 'Registered' value in that picklist and activate that.
 
Igor MakarovIgor Makarov
It turns out that the issue is only with my test code. It works fine when I test it in UI. For some reason, my test code thinks it's in sent status no matter what I do. Do I need to map it or something else? I haven't used Map for anything yet so I'm not that familiar with it.