3

If I create a new Campaign through the Salesforce UI, the Campaign Member Statuses of Sent and Responded are created automatically.

But when I insert a Campaign in a test class no Campaign Member Statuses are automatically created. (I am doing this in a new scratch org with no existing flows/triggers on Campaign)

@isTest 
private static void campaignInsertTest() {
    Campaign c = new Campaign(Name='Test Campaign');
    insert c;
    System.debug([SELECT Id,Label FROM CampaignMemberStatus]);//DEBUG|()
}

When I run that same exact code from Execute Anonymous I get a different result.

Campaign c = new Campaign(Name='Test Campaign');
insert c;
System.debug([SELECT Id,Label FROM CampaignMemberStatus]);//DEBUG|(CampaignMemberStatus:{Id=01Y3G000000Z8aGUAS, Label=Sent}, CampaignMemberStatus:{Id=01Y3G000000Z8aHUAS, Label=Responded})

Is there something I need to do in the test class in order for Campaign Member Statuses to be automatically created?

1
  • 1
    Hi @Windam, Did you check using SeeAllData=true. We know that it is not advisable but can you check if it is returning any data for the same. Dec 22, 2022 at 7:39

1 Answer 1

1

CampaignMemberStatus records are even created in test class as well same as your anonymous block code but you won't be able to query them . If you need those queries you have to use SeeAllData=true in your test class to access them.

Generally the best practise is not to use it.

1
  • or, use selector classes and mock the selector for CampaignMemberStatus using dependency injection to avoid the SeeAllData issue
    – cropredy
    Dec 22, 2022 at 17:53

You must log in to answer this question.

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