What is the difference between outputfield and outputtext?

apex:outputText: Displays text on a Visualforce page.

apex:outputField: A read-only display of a label and value for a field on a Salesforce object.

Sample Code:

Visualforce Page:

<apex:page controller="Sample" tabStyle="Account">
<apex:form >
<apex:pageMessages />
    <apex:pageBlock id="pg">
        <apex:pageBlockSection columns="1">
            <apex:outputText >Sample Output Text</apex:outputText>
            <apex:outputField value="{!acct.Name}"/>
        </apex:pageBlockSection>
    </apex:pageBlock>
</apex:form>

</apex:page>

Apex Class:

public class Sample {
    public Account acct {get;set;}
    
    public Sample() {
       acct = [SELECT Name, Industry FROM Account LIMIT 1];
    }
}

Output:


Cheers!!!

1 comment: