The <apex:inputField>
component is used to automatically generate input fields for Salesforce standard and custom object fields. It simplifies the process of creating forms and ensures consistency with the field types and validation rules set in Salesforce.
Why Use <apex:inputField>
?
- Automatically generates fields based on Salesforce object schema.
- Ensures field validation (such as data types, required fields, etc.) is applied.
- Reduces the effort of manually creating input forms for Salesforce objects.
Basic Syntax
xmlCopyEdit<apex:inputField value="{!objectName.FieldName}"/>
value
: Binds the field value to a specific Salesforce object and field.
Example: Input Field for Account Name
xmlCopyEdit<apex:page controller="AccountController">
<apex:form>
<apex:inputField value="{!account.Name}"/>
<apex:commandButton value="Save" action="{!saveAccount}"/>
</apex:form>
</apex:page>
Apex Controller:
apexCopyEditpublic class AccountController {
public Account account { get; set; }
public AccountController() {
account = new Account();
}
public void saveAccount() {
insert account;
}
}
Key Attributes
value
: Binds the input field to a specific Salesforce field on the object.required
: Makes the field mandatory based on the field’s Salesforce settings.disabled
: Disables the field, preventing user interaction.label
: Allows for custom labels (optional if the Salesforce field label is sufficient).
Use Cases
- Data Entry Forms: Automatically generate input fields for creating or updating Salesforce records.
- Custom Record Creation: Build forms for custom objects in Salesforce with minimal code.
- Validation-Driven Forms: Ensure that all Salesforce validation rules are automatically applied to the input fields.