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
Will EdwardsWill 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:
public class myWeb2LeadExtension {

    private final Lead weblead;

    public myWeb2LeadExtension(ApexPages.StandardController
                                stdController) {
       weblead = (Lead)stdController.getRecord();

     public PageReference saveLead() {
       try {
       catch(System.DMLException e) {
           return null;
       PageReference p = Page.ThankYou;
       return p;

And my VF:
<apex:page standardController="Lead"
           title="Register for Digital Accelerator" showHeader="false"
           <script src=''></script>
 <apex:composition template="{!$Site.Template}">
  <apex:define name="body">
   <apex:form id="theForm">
    <apex:messages id="error"
      <apex:pageBlock title="" mode="edit">
        <apex:pageBlockButtons >
           <apex:commandButton value="Submit"
        <apex:pageBlockSection title="Register for Digital Accelerator"
        <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"/>

         document.getElementById('{!$Component.theForm}').elements['{!$Component.hiddenField3}'].value = '7013B000000MAma';
         document.getElementById('{!$Component.theForm}').elements['{!$Component.hiddenField4}'].value = 'Responded';


Just insert those records in the 'Campaign Member' object with the Id of the campaign record and Id of the lead record and other field values.
You can check the list of fields for the campaign member object in this link -

Below is the sample code for inserting campaign member object
CampaignMember cpgMember = CampaignMember ();
cpgMember.CampaignId = 'xxxxxxx'; //Id of the Campaign Record
cpgMember.LeadId = 'XXXXX'; //Id of the Lead record
cpg.Status = 'Responded';
insert cpgMember;

Will EdwardsWill Edwards
Karanrajs, is there a way to do that as part of the existing controller, or as an extension to what I already have. I won't have the LeadId until the the Lead is created, correct?