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

Apex Trigger For Updating and Adding Campaign Members

I have a web-to-lead (or web-to-anything) form created. Upon submission, I'd like to ideally do the following:


1) If they aren't already in SFDC, create them as a lead and update various fields;

2) If they aren't a member of a specific campaign, add them to the campaign and update their status;

3) If they are a member of the campaign, update their status.


The basic web-to-lead form is causing me issues so I think this might have to be done with an Apex Trigger. When the current form is submitted, it works ok for someone already in SFDC. It does create a lead even though they are already in the system as either a contact or lead. But their campaign member status is updated correctly and if they aren't a member of the campaign already then they are added to it.


But if they are not in SFDC it doesn't work as I hoped. They are added as a lead (which is perfect)but it generates an error to the user when trying to add them to a campaign. My guess is that the way the form is setup, it's doing a lookup of the contact/lead's ID via the email address field and adding them to the campaign that way. And when the person is not already in SFDC (even though they are being added as a lead during the form submission) it can't retrieve their contact or lead ID and therefore it fails.


So, is this a problem best met with an Apex Trigger? 


What's the error message?


Might want to turn on the Critical Update for campaign members if you haven't already.  That is needed to run any campaign member triggers, workflow, and validation rules when creating members from web to lead.


Just saw this post - please only post questions once.


Sounds like your 3rd party provider has bad code.  For clarity, anything using the API is not "web to lead", but what we call custom forms.


You can use a trigger to fix hwo things work and resolve the dupes, but it sounds like the webform provider you're using might be able to bake that in as well.


Yes. After I posted on the other forum I decided that it was beyond the scope and probably required some customization wtih Apex. Sorry for the duplicate.


The error message upon submission is:


ERROR: There was an error submitting the form. Salesforce error: Attempted to add a campaign member where either the member id 'null' or the campaign id '701A0000000LaIy' is null. 


It looks to me that it's trying to add the person to the campaign before they have been added as a lead. Thus they are unable to obtain the contact or lead ID.


I don't see any critical updates for campaigns.


How would I know if it's a problem with the 3rd party (FormSpring) code? And if it is, what's the best way of troubleshooting?


Critical updates are located:

 Setup-->App Setup-->Critical Updates (last option)


It sounds like you aren't getting the prompt, so you probably have it on.


You can try to run a debug log which I believe will show what their code is doing, but that may be obfuscated by the IP protection of managed packages:


Setup-->Admin Setup-->Monitoring-->Debug Log-->New 


Can you send them a note or submit a case?  


I ran the debug log but as you suspected it didn't display what was going on during the actual submission. I can see other processes running like deduplication but not the actual insertion of data.


I'll hit them up but so far I haven't found them to be particularly helpful when it comes to their SFDC integration.

Dave SullivanDave Sullivan

I'm doing somethign similar and it might be easiest to setup an email service on salesforce, write an apex class to read in an email, parse the data into an array, then create the objects and place the data in the right place and update any existing/new records and campaign status.  Basically seperate the process of collecting the information and putting it into salesforce a little bit more.