Can anyone please help? My problem is that I am trying to write a trigger on the Lead object that will fire when a new lead is inserted. I want this trigger to look at the campaign that the lead has been assigned to and then use certain values from that campaign to set values of some of the lead record fields.
I have tried doing this in an after insert trigger but I am unable to write to lead record fields in an after insert trigger. Then in the before insert trigger I can't pull back a list of campaign members based on the lead id because the lead doesn't exist in the database yet.
Is there an easier way for me to those this? I think I am making this more complicated that it should be.
Attribution to: Brian Casey
Possible Suggestion/Solution #1
trigger populateCampaignDataToLead on Lead(before insert,before update){
Set<Id> setcmpIds=new Set<Id>();
for( lead l:trigger.new){
if(l.CampaignId != null){
setcmpIds.add(l.CampaignId);
}
}
Map<Id,campaign> mapIdByCampaign=new Map<Id,campaign>([Select Id,Name,Field1,field2 from campaign where Id in:setcmpIds]);
for(lead l:trigger.new){
if(l.field1!=null){
l.field1=mapIdByCampaign.get(l.CampaignId).field1;
}
}
}
Something like above should work right away for you .You dont need to wait for lead insert to happen and in before insert you will have campaign Id
Attribution to: Mohith Shrivastava
This content is remixed from stackoverflow or stackexchange. Please visit https://salesforce.stackexchange.com/questions/30187