Use Standard Controller In Visualforce Page

How to use standard controller in Visualforce Page?

Using Standard Controller we easily insert update and delete record from custom Visualforce Page.

To associating Standard Controller with Visualforce Page use standardController attribute of <apex:page> tag.

To associate custom Object controller use custom object:-

<apex:page standardController=”Custom_Object_Name__c”>

To associate standard Object controller use standard object, suppose you want to bind with Lead object this your code will:-

<apex:page standardController=”Lead”>

Let’s try to save a lead using standard controller. There are some point need to focus during using standard controller:-

  1. Use <apex:pageMessages/> tag insde <apex:form> tag. This tag handle messages like error or warning.
  2. You must include all required fields in your form.
  3. Use any one action from (save,quicksave,delete,edit,cancel and list)
  4. In case of save action if record created successfully then controller redirect to record detail page.
  5. Use command button, action support or action function  to bind with a action(save,quicksave,delete,edit,cancel and list)
<apex:page standardController="Lead" showHeader="true" sidebar="true">
		<apex:outputLabel>Last Name</apex:outputLabel>
		<apex:inputField id="lastName" value="{!lead.LastName}"/>
		<apex:outputLabel>Company Name</apex:outputLabel>
		<apex:inputField id="company" value="{!lead.Company}"/>
		<apex:commandButton action="{!save}" value="Save" />

Output of above code will be

Standard Controller In Visualforce Page

In Lead and Contact  object Last name is mandatory(required) field. Also in Lead Object Company name is required. Click save button without enter field data. You will get error message  like following

Standard Controller In Visualforce Page

Leave a Reply

Your email address will not be published. Required fields are marked *