Friday, 7 September 2012

Create a Basic Form - Visualforce


The key to any data input is using a form. In this lesson you’ll create the most basic form.
  1. Create a new Visualforce page called MyForm, which uses a standard controller for the Account object.
    <apex:page standardController="Account">
    </apex:page>
  2. Insert an <apex:form> component, into which all your input fields will be placed:
    <apex:form>
    </apex:form>
  3. Within the form, add an input field for the name field of an account, as well as command button that saves the form when clicked:
    <apex:inputField value="{! account.name}"/>
    <apex:commandButton action="{! save}" value="Save!"/>
This form, although very basic, contains all the essential elements: a form, an input field, and a button that processes the form:
A Sample Form Box
In this case, you use a <apex:commandButton> which produces a button. Note that the action element is bound to {! save}. This expression language syntax looks similar to the syntax you used to specify a field in a record. However, in this context, it references a method—a piece of code named save. Every standard controller automatically supplies a save() method—as well as update() and other methods—so in this case, the save() method on the standard controller is invoked when the button is clicked.
If you enter a value and click Save, the values of the input fields are bound to like-named field values in a new record, and that record is inserted. In other words, the<apex:inputField> component produces an input field for the name field of a new account record, and when you click Save, ensures that the value you entered is automatically saved to that field.
After you click Save, the platform displays the newly-created record. Return to your Visualforce page by entering its URL, which will look something likehttps://na6.salesforce.com/apex/MyForm.

No comments:

Post a Comment