Tuesday, 12 April 2011

Creating a Web-to-Lead Form (Part 2)

When we left off in the last post we mentioned creating an extension for our Web-to-Lead form that would direct the user to our custom Visualforce page. This extension will redirect the users to the "Thank You" page we created in the last post and will create a new lead post. We can begin this by clicking 'Setup', then 'Develop', then 'Apex Classes', and then finally 'New'. Once we are in the editor we can begin adding the following content:

01 public class myWeb2LeadExtension {
02
03     private final Lead weblead;
04
05     public myWeb2LeadExtension(ApexPages.StandardController
06                                       stdController) {
07            weblead = (Lead)stdController.getRecord();
08     }
09
10     public PageReference saveLead() {
11            try {
12            insert(weblead);
13            }
14            catch(System.DMLException e) {
15                   ApexPages.addMessages(e);
16                   return null;
17            }
18            PageReference p = Page.ThankYou;
19            p.setRedirect(true);
20            return p;
21       }
22 }

Once this is completed we need to create the "Contact Us" page. Begin by clicking 'Setup', then 'Develop', and finally 'Pages'. For the label you have the option to choose whatever you like but for simplicity we will go with 'Contact Us Page' and the name will be 'ContactUs'. The final step is to just replace the existing VF markup with the following content:

01 <apex:page standardController="Lead" extensions="myWeb2LeadExtension"
02  title="Contact Us" showHeader="false" standardStylesheets="true">
03   <apex:composition template="{!$Site.Template}">
04    <apex:define name="body">
05      <apex:form >
06        <apex:messages id="error" styleClass="errorMsg" layout="table"
07        style="margin-top:1em;"/>
08        <apex:pageBlock title="" mode="edit">
09        <apex:pageBlockButtons>
10        <apex:commandButton value="Save" action="{!saveLead}"/>
11        </apex:pageBlockButtons>
12        <apex:pageBlockSection title="Contact Us" collapsible="false"
13        columns="1">
14        <apex:inputField value="{!Lead.Salutation}"/>
15        <apex:inputField value="{!Lead.Title}"/>
16        <apex:inputField value="{!Lead.FirstName}"/>
17        <apex:inputField value="{!Lead.LastName}"/>
18        <apex:inputField value="{!Lead.Email}"/>
19        <apex:inputField value="{!Lead.Phone}"/>
20        <apex:inputField value="{!Lead.Company}"/>
21        <apex:inputField value="{!Lead.Street}"/>
22        <apex:inputField value="{!Lead.City}"/>
23        <apex:inputField value="{!Lead.State}"/>
24        <apex:inputField value="{!Lead.PostalCode}"/>
25        <apex:inputField value="{!Lead.Country}"/>
26       </apex:pageBlockSection>
27      </apex:pageBlock>
28    </apex:form>
29   </apex:define>
30  </apex:composition> 
31 </apex:page>

Now that all the content has been uploaded we need to make these pages available on our site. The first step is to enable all the Visualforce pages.  Click 'Setup', then 'Develop', then 'Sites', and finally click our site label. In the 'Site Details' page under 'Site Visualforce Pages' we will need to click 'Edit'. We have already saved our "ContactUs" and "ThankYou" pages so we need to select them both and click 'Add'. Finally click 'Save' to complete the process.

Next we need to make sure that the fields we created on our "ContactUs" page is completely visible. Go to the 'Site Details' page and click 'Public Access Settings'. Next go to the 'Field-Level Security' section and click 'View'. Finally make sure that the Address, Company, Phone, Email, and Name fields are marked as visible. If you want to change the visibility of any of the settings simply click 'Edit'.

Another step that needs to be taken is to grant the "Create" permission to our site for the lead object. Go to the 'Site Details' page and click 'Public Access Settings'. Then head to the 'Profile' page and click 'Edit". Under the 'Standard Object Permissions', select 'Create' permissions for Leads. Once again click 'Save' to complete the process.

Last step is to access our "ContactUs" page on the actual public site and see if the new lead form works! And fingers crossed it should.... :)

No comments:

Post a Comment