I am trying to find the Campaign Member Created Date based on the Contact Id and Campaign Id on an Opportunity record. I am newer to apex and struggling to find the record based on the combination of the Contact Id and Campaign Id.
Below is my first pass of a trigger that will run after insert and after update. It looks to find the full list of Campaign Members that are currently linked to Contacts. It then runs through the Opportunities in trigger.new with criteria to only evaluate if they have the ContactId and CampaignId populated. If it meets those criteria, I want it to run through the list of Campaign Members to find the Created Date based on the combination of the ContactId and CampaignId. Once it finds the Id, it would then add the Opportunity Id and Campaign Member Created Date to a list to run an update on.
Thanks for any advise!
trigger addDLRSCampaignInfo on Opportunity (after insert, after update) {
List<CampaignMember> campMemberList = [SELECT Id, CampaignId, ContactId, CreatedDate
FROM CampaignMember
WHERE ContactId != NULL];
Map<Id, Opportunity> oppMap = new Map<Id, Opportunity>();
for(Opportunity opp : Trigger.new){
if(opp.ContactId != NULL && opp.CampaignId != NULL){
//find the campaign member record from the campMemberList based on the combination of
//ContactId and CampaignId
//then add the Opportunity Id and Campaign Member Created Date into a list to be updated on
//the Opportunity record
}
}
}