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
bperry8086bperry8086 

How to pass a parameter from Visualforce to Apex?

I want to pass a parameter from a visualforce page to Apex code.  I want the value to come from an InputText control (or similar) so that users can enter a value and apex can then be run that works with that value.  Below is the example I've been working with.

 

Currently, I get a blank for a value on the system log line for "NewDetails.  If I replace the code in red with "fred", the static value "fred" appears in the system log.

 

Clearly I'm doing something wrong, but what?  Any help you can give would be much appreciated.

 

Thanks,

 

Bruce

 

---------------------------------------

Visualforce Page:

 

<apex:page controller="TestParamController">
<apex:form >
    <apex:pageBlock mode="edit" id="block">
        <apex:pageBlockSection >        <apex:inputtext id="NewDetails"  />
        <apex:pageBlockSectionItem >
            <apex:commandLink value="Go!" action="{!passParam}"  rerender="all" >
                 <apex:param name="NewDetails" value="{!NewDetails}" assignTo="{!NewDetails}"/>
            </apex:commandLink>                
         </apex:pageBlockSectionItem>
         </apex:pageBlockSection>
    </apex:pageBlock>    
</apex:form></apex:page>

 

Apex Controller:

 

}

public class TestParamController{
    String paramValue;
    String inputValue;
    String NewDetails;
    
    public TestParamController(){
        this.inputValue = 'Hai';
    }
    
    public String getParamValue(){
        return paramValue;
    }
    
    public void setParamValue(String paramValue){
        this.paramValue = paramValue;
    }
    
     public String getNewDetails(){
        return NewDetails;
    }
    
    public void setNewDetails(String nd){
        this.NewDetails = nd;
    }
    
    public String getInputValue(){
        return inputValue;
    }
    
    public void setInputValue(String inputValue){
        this.inputValue = inputValue;
    }
    
    public void passParam(){
    string pv;
        System.debug('ParamValue :'+ this.paramValue);
        System.debug('InputValue :'+ this.inputValue);
       System.debug('NewDetails :'+ this.NewDetails);
        pv = ApexPages.currentPage().getParameters().get('NewDetails');
             System.debug('pv :'+ pv);
    }

Best Answer chosen by Admin (Salesforce Developers) 
Cory CowgillCory Cowgill

Why don't you just bind your inputText directly to your value in the controller like this:

 

<apex:inputText value="{!NewDetails}"/>

 

 

All Answers

Cory CowgillCory Cowgill

Why don't you just bind your inputText directly to your value in the controller like this:

 

<apex:inputText value="{!NewDetails}"/>

 

 

This was selected as the best answer
bperry8086bperry8086

Thanks!  I knew I must be doing something wrong.  Here's my current code for anyone else who is interested. Note that I'm no longer trying to pass a parameter with the commandlink.  The value in the text box now appears in the system log (by way of the debug statements).

 

-------------------------------------------------------

Visualforce Page:

 

<apex:page controller="TestParamController">
<apex:form >
    <apex:pageBlock mode="edit" id="block">
        <apex:pageBlockSection >
        <apex:inputText id="NewDetails" value="{!NewDetails}"/>        <apex:pageBlockSectionItem >
            <apex:commandLink value="Go!" action="{!passParam}"  rerender="all" >
                 
            </apex:commandLink>                
         </apex:pageBlockSectionItem>
         </apex:pageBlockSection>
    </apex:pageBlock>    
</apex:form></apex:page>

 

Apex Code:

public class TestParamController{
    String paramValue;
    String inputValue;
    String NewDetails;
    
    public TestParamController(){
        this.inputValue = 'Hai';
    }
    
    public String getParamValue(){
        return paramValue;
    }
    
    public void setParamValue(String paramValue){
        this.paramValue = paramValue;
    }
    
     public String getNewDetails(){
        return NewDetails;
    }
    
    public void setNewDetails(String nd){
        this.NewDetails = nd;
    }
    
    public String getInputValue(){
        return inputValue;
    }
    
    public void setInputValue(String inputValue){
        this.inputValue = inputValue;
    }
    
    public void passParam(){
    string pv;
        System.debug('ParamValue :'+ this.paramValue);
        System.debug('InputValue :'+ this.inputValue);
       System.debug('NewDetails :'+ this.NewDetails);
        pv = ApexPages.currentPage().getParameters().get('NewDetails');
             System.debug('pv :'+ pv);
    }
}

 

 

 

Cory CowgillCory Cowgill

No problem.

 

This approach is the simplest use case and more often than not will work for you just fine.

 

If you get into advanced use cases, you can checkout Javascript Remoting which allows you to pass variables to a controller method via Javascript.