Find your content:

Search form

You are here

Trigger for passing picklist value from Campaign to campaign members custom field

 
Share

Brand new to Apex with rudimentary programming skills.

Here's what I'm trying to do: I'm trying to pass the campaign type (email, webinar, etc...) from the Campaign to it's Members. What I'd like to have happen is that, when a Lead is added to a Campaign as a member, the picklist value from Campaign.

I thought I'd set up a trigger to update a custom field (picklist) for the CampaignMember object. Here's what I have so far.

trigger Set_Campaign_Type on CampaignMember (before update, before insert) {
Campaign c = new Campaign();
for(CampaignMember Member : trigger.new){
    Member.Marketing_Campaign_Type__c = c.Type;
}

I'm sure it's WAY off so any help would be appreciated.

Thanks!

*********Edit***********

As James pointed out, this is MUCH easier to do via Text formula. However, I ended up writing a working trigger, so here goes (unformatted and commented):

 trigger Set_Campaign_Type on CampaignMember (before insert, before update) {
       if(checkRecursiveMarketingStatusUpdate.runOnce()){
        List<CampaignMember> cm = Trigger.new;
        List<String> leadid= new List<String>();
        List<Lead> leads = new List<Lead>();

        List<Campaign> cmp = [SELECT Type FROM Campaign WHERE Id = :cm[0].CampaignId];
        String type  = cmp[0].Type;
        String status = cm[0].Status;

        for(CampaignMember c : cm){

           leadid.add(c.LeadId);
           c.Marketing_Campaign_Type__c = type; 
       }

       for(Lead l : [SELECT Id FROM Lead WHERE Id IN : leadid]){    

               l.Campaign_Type__c = type;
               l.Marketing_Response__c = status;
               leads.add(l);     
               system.debug(leads[0].Campaign_Type__c + ' ' + leads[0].Marketing_Response__c);      

            }

        update leads;           } 
}

This updates both a custom field on the CampaignMember and associated Lead to indicate the Campaign Type.


Attribution to: Doodereeno66

Possible Suggestion/Solution #1

You can do this with a formula instead of coding a solution.

Create a text formula on the CampaignMember and specify Campaign.Type


Attribution to: James Loghry
This content is remixed from stackoverflow or stackexchange. Please visit https://salesforce.stackexchange.com/questions/33142

My Block Status

My Block Content