function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
alonmalonm 

Passing a parameter with a CommandButton

Hi All,

I am trying to pass a parameter to the action method of a commanButton, and i can't seem to get it work, although i tried several different methods. If I work with a commandLink i can pass the variable. This is the code of the page and the controller:

 

<apex:page controller="test">
    <apex:form >
           <apex:commandButton action="{!action1}" value="1, 2">
               <apex:param name="param1" value="val1"/>
               <apex:param assignto="{!param2}" value="val2"/>
           </apex:commandButton>
          <apex:commandLink action="{!action2}" value="3, 4 ">
               <apex:param name="param3" value="val3"/>
               <apex:param assignto="{!param4}" value="val4"/>
           </apex:commandLink>

           <apex:commandButton onclick="clickMethod('val5')" value="5">
          </apex:commandButton>
           <apex:actionFunction action="{!action3}" name="clickMethod" >
               <apex:param name="firstParam" assignTo="{!param5}" value="" />
           </apex:actionFunction>
    </apex:form>
</apex:page>

 

public class test
{
     public string param2
     {
      get;
      set {param2 = value; system.debug('param2 (property) is ' + param2);}
    }
   
     public string param4
     {
      get;
      set {param4 = value; system.debug('param4 (property) is ' + param4);}
    }

     public string param5
     {
      get;
      set {param5 = value; system.debug('param5 (property) is ' + param5);}
    }
     
      public PageReference action1()
     {
           System.debug('param1 is ' + System.CurrentPageReference().getParameters().get('param1'));
        System.debug('param2 (function) is ' + param2);
        return null;
      }
    
     public PageReference action2()
     {
        system.debug('param3  is '+ System.CurrentPageReference().getParameters().get('param3')); 
        system.debug('param4 (function) is ' + param4); 
        return null;
      }
     
     public PageReference action3()
     {
        system.debug('param5 (function) is ' + param5); 
        return null;
      }
}

 

When I press on the "1,2" button: the set of param2 is not invoked (I dont get the debug message), but action1 does, but the two parameters are null.

When I press on the "3,4" link: the set of param4 is invoked, then action2 runs, and the parameters have the right values.

When I press on the "5" button: same behaviour as with the "1,2" button.

 

Can some one tell me what I am doing wrong? or is there a known bug associated with passing parameters with a command button?

 

Thanks,

Alon

David VPDavid VP

 Perhaps it might be this :

 

getter and setters are always called before any action method is called. (to make sure that any method has all the up to date values to work with if you call them).

This might be causing the behaviour you see.

 

Have you tried displaying the parameter values back to the page ? They just might show up if you let the full request cycle run.

 

 

-david-