Understanding apex:outputLink Component in Visualforce

The <apex:outputLink> component is used to create a standard HTML link that navigates to a specified URL or page. It does not invoke controller actions like <apex:commandLink> does.


Syntax

xmlCopyEdit<apex:outputLink value="/apex/AccountDetails">Go to Account Details</apex:outputLink>

Key Attributes

AttributeDescription
valueURL of the page to navigate to.
targetOptional. Specifies where to open the linked document (e.g., _blank, _self).
styleClassOptional. CSS class for styling.

Example 1: Basic Navigation Link

xmlCopyEdit<apex:outputLink value="/apex/AccountPage">View Account</apex:outputLink>
  • Redirects to the AccountPage when clicked.

Example 2: Link With Dynamic Parameters

xmlCopyEdit<apex:outputLink value="/apex/AccountPage?id={!account.Id}">
    {!account.Name}
</apex:outputLink>
  • Dynamically passes the account.Id in the URL.

Example 3: Open Link in New Tab

xmlCopyEdit<apex:outputLink value="https://www.salesforce.com" target="_blank">
    Visit Salesforce
</apex:outputLink>
  • Opens the Salesforce homepage in a new browser tab.

When to Use <apex:outputLink>

  • To navigate to another Visualforce page or external URL.
  • When you don’t need to invoke Apex controller logic.
  • Best for static or dynamic navigation based on record data.

Best Practices

  • Use it for simple redirection needs.
  • Combine with expressions like {!record.Id} for dynamic URLs.
  • Use target="_blank" when linking to external websites.