You need to sign in to do that
Don't have an account?
Will Edwards
How do I add a lead to a campaign via Visualforce page?
I'm trying to add a lead to a campaign using VF and Apex. I'd like to add all leads entered via the form to a campaign with status "Responded."
Can you help out? Thanks.
Here's my controller:
And my VF:
Can you help out? Thanks.
Here's my controller:
public class myWeb2LeadExtension { private final Lead weblead; public myWeb2LeadExtension(ApexPages.StandardController stdController) { weblead = (Lead)stdController.getRecord(); } public PageReference saveLead() { try { insert(weblead); } catch(System.DMLException e) { ApexPages.addMessages(e); return null; } PageReference p = Page.ThankYou; p.setRedirect(true); return p; } }
And my VF:
<apex:page standardController="Lead" extensions="myWeb2LeadExtension" title="Register for Digital Accelerator" showHeader="false" standardStylesheets="true"> <head> <script src='https://www.google.com/recaptcha/api.js'></script> </head> <apex:composition template="{!$Site.Template}"> <apex:define name="body"> <apex:form id="theForm"> <apex:messages id="error" styleClass="errorMsg" layout="table" style="margin-top:1em;"/> <apex:pageBlock title="" mode="edit"> <apex:pageBlockButtons > <apex:commandButton value="Submit" action="{!saveLead}"/> </apex:pageBlockButtons> <apex:pageBlockSection title="Register for Digital Accelerator" collapsible="false" columns="1"> <div class = "requiredInput"> <div class = "requiredBlock"></div> <apex:inputField value="{!Lead.FirstName}" required="true"/> <apex:inputField value="{!Lead.LastName}" required="true"/> <apex:inputField value="{!Lead.Nickname__c}" label="Nickname" required="False"/> <apex:inputField value="{!Lead.Title}" required="true"/> <apex:inputField value="{!Lead.Email}" required="true"/> <apex:inputField value="{!Lead.Company}" required="true"/> <apex:inputField value="{!Lead.Phone}" required="true"/> <apex:inputField value="{!Lead.Mailing_Address__c}" required="true"/> <apex:inputHidden value="{!Lead.Campaign.Id}" id="hiddenField3"/> <apex:inputHidden value="{!Member_Status}" id="hiddenField4"/> <script> document.getElementById('{!$Component.theForm}').elements['{!$Component.hiddenField3}'].value = '7013B000000MAma'; </script> <script> document.getElementById('{!$Component.theForm}').elements['{!$Component.hiddenField4}'].value = 'Responded'; </script> </div> </apex:pageBlockSection> </apex:pageBlock> </apex:form> </apex:define> </apex:composition> </apex:page>
You can check the list of fields for the campaign member object in this link - https://developer.salesforce.com/docs/atlas.en-us.api.meta/api/sforce_api_objects_campaignmember.htm
Below is the sample code for inserting campaign member object