3

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.

  • is your issue has resolved @Brian Casey – Pavan tej Feb 13 '17 at 9:27
0
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

  • I tried this, but got an error on line 5: "Invalid field Campaign for SObject Lead". – user20824 May 27 '15 at 23:37

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.