Recipes by Category

App Distribution (2) Bundle logic, interface and services for distribution. App Logic (37) The Apex programming language, workflow and formulas for logic. Collaboration (5) The Salesforce Chatter collaboration platform. Database (29) Data persistence, reporting and analytics. Integration (33) Web Service APIs and toolkits for integration. Security (9) Platform, application and data security. Tools (4) tooling User Interface (36) Visualforce MVC and metadata-drive user interfaces. Web Sites (12) Public web sites and apps with optional user registration and login.
Beta Feedback
Cookbook Home » Creating a Web-to-Lead Form for Your Site

Creating a Web-to-Lead Form for Your Site

Post by Developer Force  (2010-07-16)

Status: Certified
Level: novice


You want to create a web-to-lead form on your site to capture prospective customers' personal information. You also want to create a custom contact page that will appear when customers submit their information.


Create two Visualforce pages: one to capture a prospect's information and another to display the confirmation. You'll also need to extend the standard controller to redirect public users to a custom contact page when they submit information.

First, create a Visualforce page for the confirmation:
  1. Click Setup | Develop | Pages.
  2. For the Label, enter Thank You Page and for the Name, enter ThankYou.
  3. Replace the existing Visualforce markup with the following:
    <apex:page title="Job Application" 
               showHeader="false" standardStylesheets="true">
      <apex:composition template="{!$Site.Template}">
        <apex:define name="body"> 
            <apex:outputText value="Your information is saved.  Thank you for your interest!"/>
    Next, you'll create an create an extension for the standard controller. By default in, a standard controller directs the user to the standard detail page after saving the related record. However, standard detail pages can't be made public via sites. To direct users to a custom Visualforce page, you must create an extension for the standard controller. The controller extension creates a new lead record and redirects users to the “Thank You” page. To create the extension:Click Setup | Develop | Apex Classes.Click New.In the editor, add the following content:
    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;
Now create a “Contact Us” Visualforce page:
  1. Click Setup | Develop | Pages.
  2. For the Label, enter Contact Us Page and for the Name, enter ContactUs.
  3. Replace the existing Visualforce markup with the following:
    <apex:page standardController="Lead"
               title="Contact Us" showHeader="false"
     <apex:composition template="{!$Site.Template}">
      <apex:define name="body">
        <apex:messages id="error"
          <apex:pageBlock title="" mode="edit">
               <apex:commandButton value="Save"
            <apex:pageBlockSection title="Contact Us"
             <apex:inputField value="{!Lead.Salutation}"/>
             <apex:inputField value="{!Lead.Title}"/>
             <apex:inputField value="{!Lead.FirstName}"/>
             <apex:inputField value="{!Lead.LastName}"/>
             <apex:inputField value="{!Lead.Email}"/>
             <apex:inputField value="{!Lead.Phone}"/>
             <apex:inputField value="{!Lead.Company}"/>
             <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}"/>

Finally, make the pages available on your site:

  1. Enable the new Visualforce pages:
    1. Click Setup | Develop | Sites.
    2. Click your site label.
    3. In the Site Details page under Site Visualforce Pages, click Edit.
    4. Select ContactUs and ThankYou and click Add.
    5. Click Save.
  2. Make sure the fields you exposed on your “Contact Us” page are visible on your site:
    1. In the Site Details page, click Public Access Settings.
    2. Under Field-Level Security, next to Lead, click View.
    3. Verify that the Visible setting is enabled for the Address, Company, Email, Name, and Phone fields. Click Edit to change the visibility of any settings.
  3. Grant the “Create” permission to your site for the lead object:
    1. In the Site Details page, click Public Access Settings.
    2. In the Profile page, click Edit.
    3. Under Standard Object Permissions, select the “Create” permission for Leads.
    4. Click Save.
  4. Access your “Contact Us” page via your public site and test your new lead form.


Recipe Activity - Please Log in to write a comment

As a workaround for the security issue you can add the argument rendered="{!Id=null}" to apex:page, which will return an entirely blank page when someone tries to add ?id=


<apex:page standardController="Lead" extensions="myWeb2LeadExtension" title="Contact Us" showHeader="false" standardStylesheets="true" rendered="{!Id=null}">

by Ian Sidle  (2015-01-14)

What about the SPAM, bots, etc.... is there a way to add a captcha?????

by Emmanuel Corona  (2013-02-19)

How would I add a place to add an attachment to this form?

by Chris Whitehead  (2012-08-24)

I want to follow up with the following information:

I have opened the following item on the idea exchange: Link here   This idea would make it more transparent and secure.  Please promote it.

In the mean time, you can use the HTTPS:// address for secure transactions with your organization.

by acoleman07122011  (2011-08-18)

I would not recommend using this receipe as you simply cannot use ssl on public sites unless a user has been authenticated (SFDC user or portal user).

I confirmed this with salesforce support.   Log out of your SF session if you are in one (or use a different browser) and then goto the HTTPS addrress of your secure public site.    You will notice that it redirects you to a NON secure (i.e. non SSL) page.   Support told me that was how it was designed.  

by acoleman07122011  (2011-08-16)

As mentioned in this forum post, this method (i.e. using a controller extension) isn't 100% secure as someone could guess lead ID's and do some URL hacking to view (FLS is probably read only but since create requires view....) leads other than their own.  

I am very surpised that salesforce put this in the cook book given this flaw.   IMHO Create should not require view - at least not for the public sites profile.

by acoleman07122011  (2011-07-27)

Is it possible to get the code for "Contact Us" page without the custom page?

by james12122010  (2011-07-13)

Hello All

Any suggestion on how to create a login page showing your org logo, to login your company app without showing SFDC login page?

by Atef Eldahshoury  (2011-05-17)

 Great recipe, but what about test classes for the Apex code? Any help with that?

by Derek Knaggs  (2011-02-23)

 Sorry about the problem in the contact us page - it's now been fixed.

by Jon Mountjoy  (2011-01-17)

Goto to this link to get the correct CONTACT US page

by Greg Wax  (2010-11-03)

I believe the Contact Page and the Thank You page have the same code.

There isn't an input screen for entering the Leads.

Thank you

by Greg Wax  (2010-11-03)

Is the Contact Us page suppose to be the same as the Thank You page?

by David Darkins  (2010-10-09)

This is a great article.
I have one question:
could we change the code slightly to show the usage of the standard controller's save method?
one suggestion:
Could we also add information about auto-response rules and assignment rules with DML options? This allows for reuse of standard Salesforce functionality without having to rewrite the code for it.

by scostanzo07072010  (2010-09-16)


Vote to Verify a Recipe

Verifying a recipe is a way to give feedback to others and broaden your own understanding of the capabilities on When you verify a recipe, please make sure the code runs, and the functionality solves the articulated problem as expected.

Please make sure:
  • All the necessary pieces are mentioned
  • You have tested the recipe in practice
  • Have sent any suggestions for improvements to the author

Please Log in to verify a recipe

You have voted to verify this recipe.