Problem
You want to create a web-to-lead
form on your Force.com site to capture prospective customers' personal information. You
also want to create a custom contact page that will appear when customers
submit their information.
Solution
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:- Click Setup | Develop | Pages.
- For the Label, enter Thank You
Page and for the Name, enter ThankYou.
- 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">
<br/>
<center>
<apex:outputText value="Your information is saved. Thank you for your interest!"/>
</center>
<br/>
<br/>
</apex:define>
</apex:composition>
</apex:page>
Next, you'll create an create an extension for the standard
controller. By default in Salesforce.com,
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 {
insert(weblead);
}
catch(System.DMLException e) {
ApexPages.addMessages(e);
return null;
}
PageReference p = Page.ThankYou;
p.setRedirect(true);
return p;
}
}
Now create a “Contact Us” Visualforce page:- Click Setup | Develop | Pages.
- For the Label, enter Contact Us
Page and for the Name, enter ContactUs.
- Replace the existing Visualforce markup with the following:
<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>
Finally, make the pages available on your site:
- Enable the new Visualforce pages:
- Click Setup | Develop | Sites.
- Click your site label.
- In the Site Details page under Site Visualforce Pages, click Edit.
- Select ContactUs and ThankYou and click Add.
- Click Save.
- Make sure the fields you exposed on your “Contact Us” page are
visible on your site:
- In the Site Details page, click Public Access Settings.
- Under Field-Level Security, next to Lead, click View.
- 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.
- Grant the “Create” permission to your site for the lead object:
- In the Site Details page, click Public Access Settings.
- In the Profile page, click Edit.
- Under Standard Object Permissions, select the “Create” permission
for Leads.
- Click Save.
- 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=
i.e.
<apex:page standardController="Lead" extensions="myWeb2LeadExtension" title="Contact Us" showHeader="false" standardStylesheets="true" rendered="{!Id=null}">
What about the SPAM, bots, etc.... is there a way to add a captcha?????
How would I add a place to add an attachment to this form?
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://mycompany.secure.force.com address for secure transactions with your organization.
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.
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.
Is it possible to get the code for "Contact Us" page without the custom page?
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?
Great recipe, but what about test classes for the Apex code? Any help with that?
Sorry about the problem in the contact us page - it's now been fixed.
Goto to this link to get the correct CONTACT US page
http://wiki.developerforce.com/index.php/Web2Lead_with_Force.com_Sites
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
Is the Contact Us page suppose to be the same as the Thank You page?
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.