Step 1: Create a custom field on the lead --> Campaign_Name__cStep 2: Create an Apex class to populate this^ field with Campaign Name
public class CampmemTriggerhelper{
public static void CampmemTrigger(list<CampaignMember> lstcmp){
set<Id> leadIds=new set<Id>();
list<Lead> lstleads=new list<Lead>();
map<Id,string> mapLeadIdByCampaign=new map<Id,string>();
for(CampaignMember c:[Select Id,LeadId,Campaign.Name from CampaignMember where ID IN:lstcmp]){
leadIds.add(c.LeadId);
mapLeadIdByCampaign.put(c.LeadId,c.Campaign.Name);
}
for(Lead l:[Select Id,Campaign_Name__c from Lead where Id in :leadIds]){
if(l.Campaign_Name__c==null){
if(mapLeadIdByCampaign.get(l.id)!=null){
l.Campaign_Name__c=mapLeadIdByCampaign.get(l.id);
}
lstleads.add(l);
}
}
update lstleads;
}
}
Step 3: Create an Apex Trigger on Campaigns to call that^ class
Step 4: Now that you have the Campaign Name field on Lead populating Campaign Name, use this field to map the field on Opportunity.trigger CampmemTrigger on CampaignMember(after insert){
CampmemTriggerhelper.CampmemTrigger(Trigger.new);
}
Replace
CampmemTriggerhelper.C_CampaignMemberHandler(Trigger.new);
with
CampmemTriggerhelper.CampmemTrigger(Trigger.new);
in you're Apex Trigger. Thank you !
error
Close
9 answers