salesforce
  1. salesforce-apex-commandlink-component

apex:commandLink Component

The apex:commandLink component in Visualforce Salesforce is used to create a hyperlink on a Visualforce page that fires an action when clicked. This component is used for navigating within a Visualforce page or to an external URL.

Syntax

The syntax for the apex:commandLink component is as follows:

<apex:commandLink action="{!controllerMethod}" value="Link Text"/>

Example

Consider the following example of the apex:commandLink component:

<apex:page controller="MyController">
   <apex:form >
      <apex:commandLink action="{!showDetails}" value="Details" />
   </apex:form>
</apex:page>

In the above example, clicking the Details link will call the showDetails() method in the MyController class.

Output

The apex:commandLink component generates a hyperlink that is used to trigger an action on click.

Explanation

The apex:commandLink component is used to create a hyperlink in Visualforce that, when clicked, fires an action defined in the specified controller. The value attribute is used to specify the text displayed on the hyperlink.

Use

The apex:commandLink component can be used to create navigation links that trigger actions, such as opening a new window or displaying additional information on the same page. It is useful when you need to perform custom actions that are not possible with standard HTML hyperlinks.

Important Points

  • The apex:commandLink component must always be enclosed within an apex:form component.
  • The action attribute of the apex:commandLink component must be bound to a method in the specified controller that returns a PageReference object.
  • The value attribute is used to specify the text displayed on the hyperlink.

Summary

The apex:commandLink component in Visualforce Salesforce is a useful component that creates a hyperlink on a Visualforce page that fires an action when clicked. It is beneficial when custom actions need to be performed, such as opening a new window or showing additional information on the same page. The value attribute is used to specify the text displayed on the hyperlink. It is important to note that the apex:commandLink component must always be enclosed within an apex:form component and the action attribute must be bound to a method in the specified controller that returns a PageReference object.

Published on: