+ Start a Discussion
Shiva VenuShiva Venu 

Trigger on Lead to copy campaign pick value

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){
        campaignMemberIdSet.add(camMember.id);
    }

{
        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>();   
        
        // Iterate through the list of new Campaign Members
        for (CampaignMember camMember : System.Trigger.New)
        {
            // Add Lead to set to be queried
            leadIds.add(camMember.LeadId);
            
            //Add Campaign Ids to the campIds set
            campIds.add(camMember.CampaignId);
            
            // Add Lead and CampaignMember to map
            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.Campagin_Type__c = camp.Type

}
}

```
Suraj Tripathi 47Suraj Tripathi 47
Hi Shiva,

You can take reference from the below code:-
trigger UpdateCampaignTypeonLead on CampaignMember (after insert) {

       list<Lead> leadList = new list<Lead>();
        map<Id, CampaignMember> leadCampaign = new map<Id, CampaignMember>();   
        
        for (CampaignMember camMember : Trigger.New)
        {
           if(camMember.LeadId!=null){
              leadCampaign.put(camMember.LeadId, camMember);
         }
        }
     for(Lead thisLead : [Select Id, Campaign_Type__c from Lead where Id in : 
             leadCampaign.keyset() and isConverted=false]){
           if(leadCampaign.containskey(thisLead.id)){
              thisLead.Campagin_Type__c = (leadCampaign.get(thisLead.id)).Type;
              leadList.add(thisLead);
        }
      }
    if(leadList.size()>0){
     update leadList;
     }
}


In case you find any other issue please mention. 
If you find your Solution then mark this as the best answer. 
Shiva VenuShiva Venu
Hi Suraj, 

Thanks for taking time. For some reason the code is not working.

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)

User-added image