7

How do command buttons and command links differ when it comes to using <apex:param>? The param value is not being passed to the controller.

I have the following code in a Controller :

public pagereference Pageredirect_new(){
     currentpageId = Apexpages.currentpage().getparameters().get('id');
    redirectUrl = new pagereference('/a2A/e?CF00NK0000000cZqA='+account_name+'&CF00NK0000000cZqA_lkid='+currentpageId);
    return redirectUrl ;
    }

  <apex:commandLink value="New" action="{!Pageredirect_new}">
           <apex:param assignTo="{!account_name}" value="{!Account.name}" name="account_name"/>
           </apex:commandLink>

I get the resulting URL as :

/a2A/e?CF00NK0000000cZqA=donotshow......

When I do

<apex:commandButton value="New" action="{!Pageredirect_new}">
               <apex:param assignTo="{!account_name}" value="{!Account.name}" name="account_name"/>
               </apex:commandButton>

The resulting URL : a2A/e?CF00NK0000000cZqA=null....

Am I missing something?

  • I thought the only difference was visual; one is a link and one is a button. – Mike Chale Nov 30 '12 at 2:29
  • i thought the same, but sadly doesnt look like it is :( – Rao Nov 30 '12 at 6:31
13

It's something that's perceived as a "known bug" by the dev community but it never made to the list...

Jeff Douglas has written this up together with viable workarounds in March 2010: http://blog.jeffdouglas.com/2010/03/04/passing-parameters-with-a-commandbutton/ (the post and comments also contain few useful links to SF message boards).

Simple fix is to use commandLink but style it to look like a button:

<apex:commandLink action="{!Pageredirect_new}" value="New" styleClass="btn" style="color:white;text-decoration:none">
    <apex:param assignTo="{!account_name}" value="{!Account.name}" name="account_name"/>
</apex:commandLink>

Or (ab)use the fact that commandButton suddenly behaves as it should if one of it's side-effects will be a rerender of something (even of a region you don't care about).

<apex:commandButton action="{!Pageredirect_new}" value="New" rerender="hiddenBlock">
    <apex:param assignTo="{!account_name}" value="{!Account.name}" name="account_name"/>
</apex:commandButton>

<apex:pageBlock id="hiddenBlock" rendered="false"></apex:pageBlock>

Last but not least, allow me to chip in my usual rant ;) Do you realize that you don't need an action method in your controller at all? Well, unless you have greatly simplified the code...

<apex:commandLink action="/a2A/e?CF00NK0000000cZqA={!Account.name}&CF00NK0000000cZqA_lkid={!$CurrentPage.parameters.id}" value="New" />

In fact you could go even further, if you'll realize you need immediate="true" in this link then you might be better of with plain old <a> tag...

If you'll keep such links in VF and not Apex they're much easier to modify later on (new fields, forgotten URLENCODE'ing etc) plus that's always couple lines less to cover with unit test ;)

| improve this answer | |
  • Dang this was exactly what I was looking for today :) !!! I was not sure of the syntax on how to write the returnURL as a dynamic param when I figured it out that it was as simple as retURL = '/apex/mypage?id='+object.Id I had used up a little bit controller code and felt lazy to change it to the page :P I used the URLFOR A little differently URLFOR(!{$Action.account.new,[retURL='/apex/mypage?id'+object.Id])} I was looking that the URLFOR Additional params are passed as above from : salesforcesource.blogspot.com/2008/12/… – Rao Nov 30 '12 at 7:52
  • I've pasted my pretty long URLFOR example into salesforce.stackexchange.com/a/4707/799. It has limitations like not liking field names that begin with '00' so you might have to pass some stuff outside of URLFOR anyway. – eyescream Nov 30 '12 at 8:07
  • Also I have seen awesome browsers like Internet Explorer have limits on the URL length to be 2,083 and some of our users still do not know there are browsers by name chrome and firefox!!! – Rao Nov 30 '12 at 8:10
  • 1
    Consider yourself lucky if they know what a browser is. Otherwise you might get "What browser do you use?" - "Google" or "Facebook" ;) youtube.com/watch?v=o4MwTvtyrUQ – eyescream Nov 30 '12 at 8:17
  • Ahh, what a useless public known issues list when Salesforce never actually posts any of the known issues they're not going to fix. Nothing like working for weeks to escalate a case to support only to find yes they're aware of it, and no they aren't going to do anything about it. – Ralph Callaway May 5 '13 at 4:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.