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
nimdharnimdhar 

how to access the parameters sent using a command button in a controller

Hi,

I am sending parameters using a command button to a controller as follows
 <apex:commandButton value="Attach" action="{!attach}" >
 <apex:param name="leadId" value="{!$CurrentPageReference.parameters.q}" />
 <apex:param name="locId" value="{!location.id}" />
 </apex:commandButton>


I am trying to access those parametrs in the controller by having a get and set method. But it is not working. Can anyone suggest me how to access those parameters in a controller.

Thanks,
Nimdhar
Steve ChadbournSteve Chadbourn
Here is how I did it:

Visualforce:
<apex:commandButton action="{!selectClaimant}" value="Select" rerender="claimantResults">
 <apex:param name="selectedClaimantId" value="{!claimant.Id__c}"/>
</apex:commandButton>

 
Apex:
public PageReference selectClaimant()
{
 String selectedClaimantString = System.currentPageReference().getParameters().get('selectedClaimantId');
 Decimal selectedClaimantId = Decimal.valueOf(selectedClaimantString);
 return null;
}

 


nimdharnimdhar
Hi Steve,
I have an S-contol that pop ups the visual force page and I passed the query parameter in the URL
window.open ("/apex/searchLocation?q={!Lead.Id}","myWindow",' status=1,height=600, width=1000,menubar=no,toolbar=no');

I am able to access that parameter directly in my controller as below
Lead lead = [select Id,Location__c,LastName from Lead where Id= :System.currentPageReference().getParameters().get('q')];

But when I tried to access the parameter of a CommandButton 
System.debug('Location passed from visual Force' + System.currentPageReference().getParameters().get('locationId')); 
I am getting null value.

command Button in my visual force page
<apex:commandButton value="Attach" action="{!attach}" >
  <apex:param name="locationId" value="{!location.id}" />
</apex:commandButton>

Thanks
Nimdhar
Steve ChadbournSteve Chadbourn
The trick seems to be passing the parameter to the current page using a partial page refresh. Try adding a rerender="(other component on your page)" to the command button and make sure the action function in the controller returns null.
nimdharnimdhar
Thank you Steve. Will try this way.

Thanks,
Nimdhar
dchasmandchasman
You can also save yourself a couple lines of superflous code when you have an action method that just needs to redisplay using the current view:
public void selectClaimant() {
String selectedClaimantString = System.currentPageReference().getParameters().get('selectedClaimantId');
Decimal selectedClaimantId = Decimal.valueOf(selectedClaimantString);
}