Passing Parameters with a CommandLink

March 3rd, 2010

Here’s a small example of how you can pass a value to another method via a command link for Salesforce.com. When the link is clicked, the setter fires for the public member nickName. The button click then calls the processLinkClick method where you can do something like process the variable further with DML statement or running a SOQL query with the value.

The Visualforce page that simply displays a link that copies the contact’s firstName into the public member nickName via the “assignTo” attribute.

1
2
3
4
5
6
7
8
9
<apex:page standardController="Contact" extensions="CommandLinkParamController">
    <apex:form >
        <apex:commandLink value="Process Nickname" action="{!processLinkClick}">
            <apex:param name="nickName"
                value="{!contact.firstname}"
                assignTo="{!nickName}"/>
        </apex:commandLink>
    </apex:form>
</apex:page>

The controller extension used by the Visualforce page. The processLinkClick method is called after the setters fire, performs some processing and returns a null PageReference allowing Visualforce to refresh.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
public with sharing class CommandLinkParamController {
 
    // an instance varaible for the standard controller
    private ApexPages.StandardController controller {get; set;}
     // the object being referenced via url
    private Contact contact {get;set;}
    // the variable being set from the commandlink
    public String nickName {get; set;}
 
    // initialize the controller
    public CommandLinkParamController(ApexPages.StandardController controller) {
 
        //initialize the stanrdard controller
        this.controller = controller;
        // load the current record
        this.contact = (Contact)controller.getRecord();
 
    }
 
    // handle the action of the commandlink
    public PageReference processLinkClick() {
    	System.debug('nickName: '+nickName);
    	// now process the variable by doing something...
    	return null;
    }
 
}


Categories: Apex, Code Sample, Salesforce, Visualforce

Leave a comment

Comments Feed5 Comments

  1. Wes

    Forget the jQuery posts, it’s these type of posts that help the most people. It’s easy to forget this tech is still new, and a lot of people are still learning. Good post buddy.

  2. Chris

    Keep up the great posts! (Don’t listen to Wes! There’s not nearly enough SFDC-jQuery info out there.) Your posts are extremely helpful in all areas… apex, vf, and jquery…. and I look forward to trying out the gwt. Thank you!

  3. Jeff Douglas

    Thanks for the feedback Chris! Indeed, Wes is crazy. I think it’s too many pints before breakfast. I hope he continues the jQuery stuff also.

  4. Sharath

    Jeff, i wanted to know if we could pass more than one value by concatinating them by some means and passing it over as a single parameter?
    For example, I hav around 25 values to pass to the controller. Can I somehow concatenate them on VisualForce and send it over as a Single parameter to the controller?

  5. Srikanth Seela

    Hi Jeff,

    Wanted to see if it is possible to invoke the method when a button/button link is clicked in a flex app that is embedded in a visual force page.

    Basically I want to invoke a method as a response a flex event.Is it possible at all?

Leave a comment

Feed

http://blog.jeffdouglas.com / Passing Parameters with a CommandLink

WordPress Appliance - Powered by TurnKey Linux