Input field & output field Examples

<apex:inputfield> and <apex:outputfield>

Example 1: Difference between <apex:inputfield> and <apex:outputfield>
<apex:page sidebar=”false” standardController=”account”>
<apex:form >
<apex:pageBlock >
<apex:pageBlockSection >
<apex:outputfield value=”{!account.name}”> </apex:outputfield>
<apex:inputfield value=”{!account.phone}”></apex:inputfield>
</apex:pageBlockSection>
</apex:pageBlock>
</apex:form>
</apex:page>

Example 2: Placing input fields without using standard controller:
TO place any input field you have to create a property for that object in apex class.
So that you can use that property name with in input field to place the input component.
page:
<apex:page Controller=”ContactClass” sidebar=”false”>
<apex:form >
<apex:pageBlock >
<apex:pageBlockSection >
<apex:inputfield value=”{!con1.lastname}”/>
</apex:pageBlockSection>
</apex:pageBlock>
</apex:form>
</apex:page>
class:
public with sharing class ContactClass {
public Contact con1{get;set;}
}

Example 3: Action function to populate the Address Fields:
In this scenario whenever we select an account from a contact
automatically the address fields are copied from account to contact.
page:
<apex:page standardController=”Contact” extensions=”ContactClass” sidebar=”false”>
<apex:form >
<apex:actionFunction name=”populatefields” action=”{!dopop}”/>
<apex:pageBlock >
<apex:pageBlockSection columns=”1″>
<apex:inputfield value=”{!con1.AccountId}” onchange=”populatefields()”/>
<apex:inputfield value=”{!con1.Mailingcity}”/>
<apex:inputfield value=”{!con1.Mailingcountry}”/>
<apex:inputfield value=”{!con1.Mailingpostalcode}”/>
<apex:inputfield value=”{!con1.Mailingstate}”/>
</apex:pageBlockSection>
</apex:pageBlock>
</apex:form>
</apex:page>
class:
public with sharing class ContactClass {
public ContactClass(ApexPages.StandardController controller) {
con1 =(contact)controller.getRecord();
}
public PageReference dopop() {
Account objAcc =[select Id,name,Billingcity,Billingcountry,billingstate,billingpostalcode from Account where Id =:con1.AccountId ];
con1.Mailingcity=objAcc.billingcity;
con1.mailingcountry = objAcc.billingcountry;
con1.mailingpostalcode = objAcc.billingpostalcode;
con1.mailingstate = objAcc.billingstate;
return null;
}
public Contact con1{get;set;}
}

Leave a Reply