Web2Lead with Force.com Sites

Problem


You created your Force.com Site and you want to create a "Contact Us" page to capture prospect information and create a lead record in salesforce.com.

Solution


We will create two visualforce pages: one to capture the information from prospects and one to display the confirmation.

The "Save" action of a standard controller by default takes user to the standard detail page after saving the related record. However, standard detail pages can not be made public via Force.com Sites. In order to take users to a custom visualforce page after the save action we will create an extension for the standard controller.

This new extension controller (myWeb2LeaExtenstion) will create a new Lead record and redirect user to the "Tank You" page.

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

Create your "Thank You" page to inform the end user their information is saved to the system

<apex:page title="Job Application" showHeader="false" standardStylesheets="true">
  <apex:composition template="{!$Site.Template}"> 
    <apex:define name="body"> 
        <br/>
        <br/>
        <center><apex:outputText value="Your information is saved. Thank you for your interest!"/></center>
        <br/>
        <br/>
    </apex:define> 
  </apex:composition>  
</apex:page></nowiki>

Now that your extension controller and your confirmation page is created, you are ready to create your "Contact Us" visualforce 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: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.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}"/>
        </apex:pageBlockSection>
     </apex:pageBlock>
   </apex:form>
  </apex:define> 
 </apex:composition>  
</apex:page>

Your web2Lead flow is ready to be exposed via your site to your prospects. Navigate to your site details page and enable the two visualforce pages you just created File:W2lead1.png

Next, make sure your site has the correct Filed Level Visibility for the lead object. Navigate to the site details page and click the "Public Access Settings" button. Scroll down to the Field-Level Security section and click the "View" link. Review the fields that you expose on your contact us form and make sure they are set as visible.

File:W2lead2.png


And finally, grant the Create permission to your site for the Lead object. Navigate to the site details page and click the "Public Access Settings" button then click the "Edit" button. Scroll down to the Standard Object Permissions section, check the Create permission for the Lead object and save your changes.

File:EW2lead2.png

Access your contact us page via your public site and test your new lead form.

Discussion


You can easily create web forms by leveraging visualforce, standard controllers and Force.com Sites. Creating marketing landing pages with your own look and feel is just a couple steps away.