0

Trying to write a trigger for below need.

  • Have a custom pick field on Lead - (Campaign_Type__c. ----- values - Webinar, Tradeshow, Events). This custom field is replica of Type field on Campagin. 
  • Campaign - Standard pick Field - Type (Values - Webinar, Tradeshow, Events)

Whenever a lead is created a its gets associated to a campaign, i need the Type pickvalue from Campaign to be copied to Campaign_Type__c field on Lead for the associated Campagin.  There can be cases where the campaign is assoicated to multiple campaigns, in such cases the lastest associated campaign Type should be updated on Campaign_Type__C on Lead. 

Here is something i am trying, any help is much appreciated!

trigger updateCampaignTypeonLead on CampaignMember (after insert) {

for(CampaignMember camMember : Trigger.New){
        
    }

 {
        
         set<Id> leadIds = new set<Id>();
              
        list<Lead> leadList = new list<Lead>();
        set<Id> campIds = new set<Id>();
        
        map<Id, CampaignMember> leadCampaign = new map<Id, CampaignMember>();   
        
        for (CampaignMember camMember : System.Trigger.New)
        {
            leadIds.add(camMember.LeadId);
            
            campIds.add(camMember.CampaignId);
            
            leadCampaign.put(camMember.LeadId, camMember);
        
        }

 map<Id,Campaign> campmap = new map <Id,Campaign>([select Id, Type from Campaign where id in: campIds]);
 
 for(Lead thisLead : [Select Id, Campaign_Type__c from Lead where Id in : leadIds and isConverted=false])
        {
          
          Campaign camp = campmap.get(leadCampaign.get(thisLead.Id).CampaignId);    

            thisLead.Campaign_Type__c = camp.Type;
            
                       }

}
 
}

enter image description here

I need the most recent campaign type associated to lead on Lead details - Campaign type custom field(Picklist field, which is replica of Type in Campaign Object)

2
  • Those extra curly braces ({ and }) you have in there are going to mess up your variable scoping (variables defined inside of a pair of curly braces cannot be used outside of those curly braces), and your missing curly braces on the final for loop would also cause an issue (thisLead is only available to use inside of the curly braces for the for loop)
    – Derek F
    Jul 28, 2021 at 17:26
  • Derek, i have updated the code but does not seem to be working
    – Shiva.venu
    Jul 29, 2021 at 4:44

1 Answer 1

0

As mentioned by Derek in comment section, you need to make corrections to the extra curly braces ({ and  }) and for loop at the start of the trigger.Added to this as it is after insert operation , we have to mention the update statement on the record. By changing the code as below it worked for me .

trigger updateCampaignTypeonLead on CampaignMember (after insert) {
 set<Id> leadIds = new set<Id>();
              
        list<Lead> leadList = new list<Lead>();
        set<Id> campIds = new set<Id>();
        
        map<Id, CampaignMember> leadCampaign = new map<Id, CampaignMember>();  
    
        for (CampaignMember camMember : System.Trigger.New)
        {
            leadIds.add(camMember.LeadId);
            
            campIds.add(camMember.CampaignId);
            
            leadCampaign.put(camMember.LeadId, camMember);
        
        }

 map<Id,Campaign> campmap = new map <Id,Campaign>([select Id, Type from Campaign where id in: campIds]);
 
 for(Lead thisLead : [Select Id, Campaign_Type__c from Lead where Id in : leadIds and isConverted=false])
        {
          
          Campaign camp = campmap.get(leadCampaign.get(thisLead.Id).CampaignId);    

            thisLead.Campaign_Type__c = camp.Type;
            Update thisLead; // Added this line of code
            
                       }

}
1
  • Sai, This worked. Thank You!! Noted the corrections..
    – Shiva.venu
    Aug 1, 2021 at 3:14

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .