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 {
       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>

 
@Karanraj@Karanraj
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 - 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
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?