I created a map of , following is the query to create map.
map <string, Campaign> ExpenseCampaignMap = new map <string, Campaign>([select Id,
UtmCampaign_ID__c
from Campaign
where UtmCampaign_ID__c in :UTMCampaignId]);
Where UTMCmapaignId is a set of string, where values are added from trigger.new
for(Contact con: trigger.new)
{
CampaignExternalId.add(con.Source_Campaign__c);
UTMCampaignId.add(con.utmcampaign__c);
}
I am trying to create a campaing member for the campaigns in the map, using UTMCampaign_ID__c, which is an external ID on campaign.
Following is the code to create campaign members
list<CampaignMember> newCampaignMembersExpCamp = new list<CampaignMember>();
Campaign UTMCampaign;
for(Contact con: trigger.new)
{
system.debug('Contact UTM Campaign id ' + con.utmcampaign__c);
UTMCampaign = ExpenseCampaignMap.get(con.utmcampaign__c);
system.debug('UTM Campaign id ' + UTMCampaign);
CampaignMember cm= New CampaignMember(CampaignId=UTMCampaign.Id, ContactId= con.Id ,LeadId= null, Status='Opt-In');
newCampaignMembersExpCamp.add(cm);
}
insert newCampaignMembersExpCamp;
However, the get() satement for map is not returning anyvalue. UTMCmapign is null. I am not sure how to get value of the campaign from map based on the string key.