You need to sign in to do that
Don't have an account?
Jan Vandevelde
Need help with Testclass for custom controller which simply created a new lead from VF page on force.com sites?
Hi Salesforcedevs,
I need some help creating a testclass to test my custom controller. I never wriiten one for a controler before and wouldn't know how to "Push" the save button on the VF page to run the controller.
To explain:
VF page simply displays a apex:form with fields of the Lead object. No id's are passed through URL. Page is hosted on Force.com sites, just a blanc "create a lead form":
Page Tipformulier
Custom Controller Tipform
So when someone comes on the URL he gets a blank form with leadfields he has to fill in and then hits Save. Lead is immediately created in Salesforce and the visitor is redirected to a thank you page.
I have really no clue how to write a testclass for this. Could someone please help me?
I need some help creating a testclass to test my custom controller. I never wriiten one for a controler before and wouldn't know how to "Push" the save button on the VF page to run the controller.
To explain:
VF page simply displays a apex:form with fields of the Lead object. No id's are passed through URL. Page is hosted on Force.com sites, just a blanc "create a lead form":
Page Tipformulier
<apex:page sidebar="false" showHeader="false" controller="Tipform" standardStylesheets="false" applyBodyTag="false" applyHtmlTag="false"> <div id="wrapper"> <div id="header"> <img alt="Van Gansewinkel" class="auto-style1" src="{!$RESOURCE.HeaderTip800}" ></img> </div> <div class='scfForm'> <apex:form > <div id="tipgever"> <div class="scfSectionContent"> <div class="scfIntroBorder"> In het onderstaande formulier kunt u de gegevens over uw tip invullen.<br /> </div> <h2>Gegevens Tipgever</h2> <apex:outputLabel value="Voor- en achternaam *" /> <apex:inputField value="{!lead.Naam_chauffeur__c}" required="true"/> <apex:outputLabel value="Regio *"/> <apex:inputField value="{!lead.Regio__c}" required="true"/> <apex:outputLabel value="E-mailadres"/> <apex:inputField value="{!lead.Privat_email__c}"/> <apex:outputLabel value="Vestiging Tipgever" /> <apex:inputField value="{!lead.Planningslocatie_chauffeur__c}" /> </div> </div> <div id="tip"> <div class="scfSectionContent"> <h2>Tipgegevens</h2> <apex:outputLabel value="Tip voor *"/> <apex:inputField value="{!lead.Lead_for__c}" required="true"/> <apex:outputLabel value="Type Tip *" /> <apex:inputField value="{!lead.Tip_chauffeur__c}" required="true"/> <apex:outputLabel value="Iemand gesproken?"/> <apex:inputField value="{!lead.Contact_met_lead__c}"/> </div> </div> <div id="klant"> <div class="scfSectionContent"> <h2>Bedrijfsgegevens</h2> <apex:outputLabel value="Bedrijfsnaam *" /> <apex:inputField value="{!lead.Company}" required="true"/> <apex:outputLabel value="Voornaam" /> <apex:inputField value="{!lead.FirstName}" /> <apex:outputLabel value="Achternaam *" /> <apex:inputField value="{!lead.LastName}" /> <apex:outputLabel value="E-mailadres" /> <apex:inputField value="{!lead.Email}" /> <apex:outputLabel value="Telefoon *" /> <apex:inputField value="{!lead.Phone}" required="true"/> <apex:outputLabel value="Straat + huisnr *" /> <apex:inputField value="{!lead.Street}" required="true"/> <apex:outputLabel value="Postcode" /> <apex:inputField value="{!lead.PostalCode}"/> <apex:outputLabel value="Plaats *" /> <apex:inputField value="{!lead.City}" onfocus="" required="true"/> <apex:outputLabel value="Land" /> <apex:inputField value="{!lead.Country}"/> <apex:outputLabel value="Sector" /> <apex:inputField value="{!lead.Industry}"/> </div> </div> <div id="bijkomend"> <div class="scfSectionContent"> <h2>Bijkomende Informatie</h2> <apex:outputLabel value="Omschrijving tip *"/> <apex:inputField value="{!lead.Omschrijving_tip__c}" required="true"/> <div class="scfSubmitButtonBorder"> <apex:commandButton action="{!save}" value="Tip Indienen" /> </div> </div> </div> </apex:form> </apex:page>
Custom Controller Tipform
public class Tipform { // the Topform record we are adding values to public Lead lead { get { if (lead == null) lead = new Lead(); lead.RecordTypeId ='012200000009dAI'; lead.LeadSource = 'Tip form'; return lead; } set; } public Tipform() { // blank constructor } // save button is clicked public PageReference save() { try { insert lead; // inserts the new record into the database } catch (DMLException e) { ApexPages.addMessage(new ApexPages.message(ApexPages.severity.ERROR,'Error creating new tipform.')); return null; } // if successfully inserted new contact, then displays the thank you page. return Page.bedankt; } }
So when someone comes on the URL he gets a blank form with leadfields he has to fill in and then hits Save. Lead is immediately created in Salesforce and the visitor is redirected to a thank you page.
I have really no clue how to write a testclass for this. Could someone please help me?
But if you want to give me feedback on it you're welcome to do so.