0

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

        }
    }

}
0

First, you need to get familiar with Apex Sets and Maps, how to use sets in SOQL queries, and how to build Maps and reference their values. Check the documentation, Trailhead modules, Salesforce youtube channel for info on these Apex fundamentals, as well as various free and paid tutorial resources.

For your use case, here are the high-level steps you need to follow:

  1. Loop through Trigger.new and collect both CampaignId and ContactId in separate sets. Be sure to exclude null values. **

  2. Do a SOQL query on CampaignMember to get only those records where CampaignId and ContactId are in the ID sets.

  3. Loop through the CampaignMember records. For each record, populate a Map where the key is a concatenation the campaign ID and contact ID into a single string, and the value is the CampaignMember record.

  4. Loop through the Trigger.new again. For each record, concatenate the campaign ID and contact ID (skip if null) and use this to pull the campaign member record from the Map. You can then populate another map with the opp ID and the campaign member created date value.

** Since your trigger is also after update, you'll want to compare Trigger.old values to make sure you're only firing the trigger when the CampaignId or ContactId changes.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.