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
mromani1mromani1 

Passing Parameters to Controller Method

Hi,

 

I was wondering if it is possible to pass parameters to our controller method while in a VF page.

 

For example, normally, say we have a controller called "MyController" and inside it we have a method call "Method" which take a string parameter.

 

Is it possible to call this method and pass a value to it from our VF page? If so how?

 

Would this work?

 

<apex:commandButton action="{!Method('stringvalue'}"/>

 

Thanks in advance.

Best Answer chosen by Admin (Salesforce Developers) 
sfdcfoxsfdcfox

You can pass parameters to a controller though the page's parameter list, but not directly to the function. Here's a standard method for passing a parameter:

 

 

<!-- page --> <apex:commandButton action="{!myMethod}" value="Click Me"> <apex:param name="myParam" value="my Value"/> </apex:commandButton> // Controller method public void myMethod() { string myParam = apexpages.currentpage().getparameters().get('myParam'); // Do something with myParam here }

 

 The parameter will only exist if the button is clicked, otherwise the get function will return null.

 

All Answers

sfdcfoxsfdcfox

You can pass parameters to a controller though the page's parameter list, but not directly to the function. Here's a standard method for passing a parameter:

 

 

<!-- page --> <apex:commandButton action="{!myMethod}" value="Click Me"> <apex:param name="myParam" value="my Value"/> </apex:commandButton> // Controller method public void myMethod() { string myParam = apexpages.currentpage().getparameters().get('myParam'); // Do something with myParam here }

 

 The parameter will only exist if the button is clicked, otherwise the get function will return null.

 

This was selected as the best answer
mromani1mromani1

Thanks man!

 

Much appreciated!

varunkaushishvarunkaushish

Thanks, a lot...:smileyhappy:

nik mnik m

Hi all,

 

i am a begineer to the salesforce so i am still in the phase of learning..

i have displayed a list of all the accounts in the app using list controllers and a VF page...

now if i click on the members of the list i want its details to be displayed  below the list.. for this im using "param" but i am not able to fetch the data...

so please help.... 

Jon HazanJon Hazan

Hey Nik,

 

This is one that got me, but apparently its a very persistant and common bug. You need to specify a rerender attribute in the commandButton.

 

<apex:pageBlockid="block">

        <apex:pageBlockSection >

           <apex:pageBlockSectionItem >

               <apex:commandButton value="Save" action="{!save}"  rerender="block" >

                 <apex:param name="thisParam" value="{!value}" />

               </apex:commandButton>                

            </apex:pageBlockSectionItem>

         </apex:pageBlockSection>

</apex:pageBlock>

 

Mohammed MohiddinMohammed Mohiddin
You can also do it using get set property.

This is your visualforce page 
<apex:page Controller="ctrl">
<apex:inputText value="{!searchstring}" html-placeholder="Search here..." label="Input"/> 
 <apex:commandButton value="Search records"  action="{!search}"/>
</apex:page>

This will be the controller
public class ctrl {

public string searchstring {get;set;}

public void method{

string inputString = searchstring ;
//your code to manipulate string

}

}