function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Romeo Ngo 1Romeo Ngo 1 

Campaign field in Lead

When we use Web to Lead, we can provide a CampaignID to associate the Lead with specific Campaign.

I just recently found out that we can't use Lead.Campaign field since it is not available through the API.

Now how can one capture this fields on insert Trigger? IE, if a lead is added VIA web-to-lead and we want to get the campaign associate with it but we capture this on the insert.  We can't query the database for it because the lead and the campaign member is not added yet.



 
Vivek DeshmaneVivek Deshmane
Hi Romeo,
Lead and Campaign are not directly related to each other. There is a middle object CampaignMember in between them. Here is an example how to relate Lead with Campaign.
Campaign c = [Select id from Campaign limit 1];
Lead l = new Lead(lastname='Vivek', company='Viveks Pizza');
insert l;
CampaignMember mem = new CampaignMember (campaignid=c.id, leadid=l.id);
insert mem;

Please let me know if this helps you.

Best Regards,
-Vivek
Romeo Ngo 1Romeo Ngo 1
Thanks for the reply Vivek.

I'm well awared of that.

The problem is with Web-To-Lead which has a campaign field but we want to get the CampaignID with an before insert trigger.

IE Above.

if a lead is added VIA web-to-lead and we want to get the campaign associate with it but we capture this on the insert.  We can't query the database for it because the lead and the campaign member is not added yet.