2

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.

1

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

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

  • Thanks!!!! Man - always making things way more complex than they need to be... Well, I have a working trigger now. It turned out to be a great educational journey into writing triggers. Fun. – Doodereeno66 Apr 20 '14 at 16:38

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.