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
Micah Perry 18Micah Perry 18 

Pass values to Lead in a Web-to-Lead Visualforce page

I'm new to developing. I'm creating a public visualforce web-to-lead site. I want to pass values to Company, Lead Source, and probably a few others. I tried following a few examples (the /script portions of my VF page is where I tried to pass a value but it's a complete failure). My guess is that I need to establish components and then assign them somehow to the Lead fields.

Any info on how I can prepopulate those values would be great.

My Extension
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;
     }
}

My VF page 
<apex:page standardController="Lead"
           extensions="myWeb2LeadExtension"
           title="Contact Us" showHeader="false"
           standardStylesheets="true">
 <apex:composition template="{!$Site.Template}">
  <apex:define name="body">
   <apex:form >
   <apex:inputtext id="mpCompany"/>
        <script>
            var mpCompany = "{!$Component.mpCompany}";
        </script>
    <apex:messages id="error"
                   styleClass="errorMsg"
                   layout="table"
                   style="margin-top:1em;"/>
      <apex:pageBlock title="" mode="edit">
        <apex:pageBlockButtons >
           <apex:commandButton value="Save"
                               action="{!saveLead}"/>
        </apex:pageBlockButtons>
        <apex:pageBlockSection title="Contact Us"
                               collapsible="false"
                               columns="1">
         <apex:inputField value="{!Lead.Salutation}"/>
         <apex:inputField value="{!Lead.FirstName}"/>
         <apex:inputField value="{!Lead.LastName}"/>
         <apex:inputField value="{!Lead.Email}"/>
         <apex:inputField value="{!Lead.Phone}"/>
         <apex:inputField value="{!Lead.Street}"/>
         <apex:inputField value="{!Lead.City}"/>
         <apex:inputField value="{!Lead.State}"/>
         <apex:inputField value="{!Lead.PostalCode}"/>
         <apex:inputField value="{!Lead.Country}"/>
         <apex:inputField value="{!Lead.Company}"/>
        </apex:pageBlockSection>
     </apex:pageBlock>
   </apex:form>
   <script>
        window.onload=function()
        {

         document.getElementById(mpCompany).self = "{!$CurrentPage.parameters.Company}";
        };
    </script>
  </apex:define> 
 </apex:composition>
</apex:page>

 
Best Answer chosen by Micah Perry 18
kaustav goswamikaustav goswami
Try this and let me know in case of any issues.
 
public class myWeb2LeadExtension {

    public Lead weblead {get; set;}

    public myWeb2LeadExtension(ApexPages.StandardController
                                stdController) {
        //weblead = (Lead)stdController.getRecord();
		weblead = new Lead();
		webLead.Company = 'Self';
    }

     public PageReference saveLead() {
       try {		   
		   webLead.LeadSource = 'Web';
		   insert weblead;
       }
       catch(System.Exception e) {
           ApexPages.addMessages(e);
           return null;
       }
       PageReference p = Page.ThankYou;
       p.setRedirect(true);
       return p;
	}
}
 
<apex:page standardController="Lead"
           extensions="myWeb2LeadExtension"
           title="Contact Us" showHeader="false"
           standardStylesheets="true">
 <apex:composition template="{!$Site.Template}">
  <apex:define name="body">
   <apex:form >
    <apex:messages id="error"
                   styleClass="errorMsg"
                   layout="table"
                   style="margin-top:1em;"/>
      <apex:pageBlock title="" mode="edit">
        <apex:pageBlockButtons >
           <apex:commandButton value="Save"
                               action="{!saveLead}"/>
        </apex:pageBlockButtons>
        <apex:pageBlockSection title="Contact Us"
                               collapsible="false"
                               columns="1">
		 <apex:inputText value="{!weblead.Company}" disabled="true" />
         <apex:inputField value="{!weblead.Salutation}"/>
         <apex:inputField value="{!weblead.FirstName}"/>
         <apex:inputField value="{!weblead.LastName}"/>
         <apex:inputField value="{!weblead.Email}"/>
         <apex:inputField value="{!weblead.Phone}"/>
         <apex:inputField value="{!weblead.Street}"/>
         <apex:inputField value="{!weblead.City}"/>
         <apex:inputField value="{!weblead.State}"/>
         <apex:inputField value="{!weblead.PostalCode}"/>
         <apex:inputField value="{!weblead.Country}"/>
         <apex:inputField value="{!weblead.Company}"/>
        </apex:pageBlockSection>
     </apex:pageBlock>
   </apex:form>
  </apex:define> 
 </apex:composition>
</apex:page>

Thanks,
Kaustav

All Answers

kaustav goswamikaustav goswami
Are you trying to implement a functionality where the Company field's value will be auto-populated when the page loads?

Can you please explain what you are trying to achieve in  more details.

BTW - you do not need to declare the Lead variable as private final. Declaring a variable as private will result in the variable not being accessible to the VF page.

Thabnks,
Kaustav
Micah Perry 18Micah Perry 18
I don't need Company for this web-to-lead form. But Company needs a value for a lead to be submitted. Rather than telling the person to fill out Company with "self," I want to hide Company and have it automatically have "self." I also want to set the lead source, I don't want the person to have to pick the lead source because they shouldn't have to.
kaustav goswamikaustav goswami
Please try this out and let me know if there are any errors.
 
public class myWeb2LeadExtension {

    public Lead weblead {get; set;}

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

     public PageReference saveLead() {
       try {
		   webLead.Company = 'Self';
		   webLead.LeadSource = 'Web';
		   insert weblead;
       }
       catch(System.Exception e) {
           ApexPages.addMessages(e);
           return null;
       }
       PageReference p = Page.ThankYou;
       p.setRedirect(true);
       return p;
	}
}
 
<apex:page standardController="Lead"
           extensions="myWeb2LeadExtension"
           title="Contact Us" showHeader="false"
           standardStylesheets="true">
 <apex:composition template="{!$Site.Template}">
  <apex:define name="body">
   <apex:form >
    <apex:messages id="error"
                   styleClass="errorMsg"
                   layout="table"
                   style="margin-top:1em;"/>
      <apex:pageBlock title="" mode="edit">
        <apex:pageBlockButtons >
           <apex:commandButton value="Save"
                               action="{!saveLead}"/>
        </apex:pageBlockButtons>
        <apex:pageBlockSection title="Contact Us"
                               collapsible="false"
                               columns="1">
         <apex:inputField value="{!weblead.Salutation}"/>
         <apex:inputField value="{!weblead.FirstName}"/>
         <apex:inputField value="{!weblead.LastName}"/>
         <apex:inputField value="{!weblead.Email}"/>
         <apex:inputField value="{!weblead.Phone}"/>
         <apex:inputField value="{!weblead.Street}"/>
         <apex:inputField value="{!weblead.City}"/>
         <apex:inputField value="{!weblead.State}"/>
         <apex:inputField value="{!weblead.PostalCode}"/>
         <apex:inputField value="{!weblead.Country}"/>
         <apex:inputField value="{!weblead.Company}"/>
        </apex:pageBlockSection>
     </apex:pageBlock>
   </apex:form>
  </apex:define> 
 </apex:composition>
</apex:page>

Thanks,
Kaustav
Micah Perry 18Micah Perry 18
No errors, but the value in Company is still empty.

http://micahsb-micahperry.cs10.force.com/recruiting
Micah Perry 18Micah Perry 18
It does reassign the Company value to 'Self' after the lead is inserted. My hope was the have it prepopulated on the site, and then just set rendered=false.
kaustav goswamikaustav goswami
So you want the Company field to appear on the VF page but in read only mode and value of the field should be Self. Is that correct???
Thanks,
Kaustav
Micah Perry 18Micah Perry 18
That's right, sorry for the confusion.
kaustav goswamikaustav goswami
Try this and let me know in case of any issues.
 
public class myWeb2LeadExtension {

    public Lead weblead {get; set;}

    public myWeb2LeadExtension(ApexPages.StandardController
                                stdController) {
        //weblead = (Lead)stdController.getRecord();
		weblead = new Lead();
		webLead.Company = 'Self';
    }

     public PageReference saveLead() {
       try {		   
		   webLead.LeadSource = 'Web';
		   insert weblead;
       }
       catch(System.Exception e) {
           ApexPages.addMessages(e);
           return null;
       }
       PageReference p = Page.ThankYou;
       p.setRedirect(true);
       return p;
	}
}
 
<apex:page standardController="Lead"
           extensions="myWeb2LeadExtension"
           title="Contact Us" showHeader="false"
           standardStylesheets="true">
 <apex:composition template="{!$Site.Template}">
  <apex:define name="body">
   <apex:form >
    <apex:messages id="error"
                   styleClass="errorMsg"
                   layout="table"
                   style="margin-top:1em;"/>
      <apex:pageBlock title="" mode="edit">
        <apex:pageBlockButtons >
           <apex:commandButton value="Save"
                               action="{!saveLead}"/>
        </apex:pageBlockButtons>
        <apex:pageBlockSection title="Contact Us"
                               collapsible="false"
                               columns="1">
		 <apex:inputText value="{!weblead.Company}" disabled="true" />
         <apex:inputField value="{!weblead.Salutation}"/>
         <apex:inputField value="{!weblead.FirstName}"/>
         <apex:inputField value="{!weblead.LastName}"/>
         <apex:inputField value="{!weblead.Email}"/>
         <apex:inputField value="{!weblead.Phone}"/>
         <apex:inputField value="{!weblead.Street}"/>
         <apex:inputField value="{!weblead.City}"/>
         <apex:inputField value="{!weblead.State}"/>
         <apex:inputField value="{!weblead.PostalCode}"/>
         <apex:inputField value="{!weblead.Country}"/>
         <apex:inputField value="{!weblead.Company}"/>
        </apex:pageBlockSection>
     </apex:pageBlock>
   </apex:form>
  </apex:define> 
 </apex:composition>
</apex:page>

Thanks,
Kaustav
This was selected as the best answer
Micah Perry 18Micah Perry 18
It's there! Thank you thank you thank you, sorry for the confusion earlier.