I wrote a trigger that creates some CampaignMemberStatus records against a Campaign automatically, and noticed that when I tried to test my logic any queries that I run for CampaignMemberStatus against Campaigns in a unit test seem to return empty collections unless I turn on seeAllData. Is there a reason for this that I am missing?
I'm essentially running this query and returning results (which are empty without seeAllData). I played with the runAs user being some different profiles as well.
SELECT Label, CampaignId FROM CampaignMemberStatus WHERE CampaignId =: c.Id
EDIT: Added simple unit test that fails just trying to run a query. When I execute this with seeAllData enabled it works, and when I run actual code it works fine (the two default Status values, Sent and Responded, always show up when you query for a new Campaign's CampaignMemberStatus children).
Profile p = [Select Id FROM Profile WHERE Name = 'Std Access'];
User user1 = new User(FirstName = 'Test',
Username = '[email protected]',
LastName = 'User 1',
Email = '[email protected]',
Alias = 'test1',
CommunityNickname = 'test1',
EmailEncodingKey = 'UTF-8',
LanguageLocaleKey = 'en_US',
LocalesIdKey = 'en_US',
TimeZonesIdKey = 'America/Los_Angeles',
ProfileId = p.Id,
UserPermissionsMarketingUser = true);
insert user1;
System.runAs(user1)
{
Campaign c = new Campaign();
c.Name = 'Test Campaign';
insert c;
List<Campaign> campaigns = new List<Campaign>();
campaigns.add(c);
Map<String, Set<String>> existingStatus = CampaignTriggerUtil.getExistingCampaignMemberStatusLabelsMap(new List<Campaign>(campaigns));
System.assertEquals(1, existingStatus.size());
}
In this case, the implementation of that custom method is below (simple enough I think):
public static Map<String, Set<String>> getExistingCampaignMemberStatusLabelsMap(List<Campaign> campaigns)
{
Map<String, Set<String>> existingStatus = new Map<String, Set<String>>();
for (CampaignMemberStatus status : [SELECT Label, CampaignId FROM CampaignMemberStatus WHERE CampaignId =: campaigns])
{
Set<String> tempSet = new Set<String>();
if (existingStatus.containsKey(status.CampaignId))
{
tempSet = existingStatus.get(status.CampaignId);
}
tempSet.add(status.Label);
existingStatus.put(status.CampaignId, tempSet);
}
return existingStatus;
}
runAs
user enabled as a marketing user?SortOrder
field? Maybe add your test code to the question.