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
astroboiiiastroboiii 

Passing parameters to controller using ajax doesn't seem to work

i have a simple inputText field a commandbutton an actionstatus section and an outputText field.  I want to enter text in the inputText, hit the command button, bind whatever is in the inputText to the corresponding controller variable and have it display in the outputText field.

 

Now, if I get rid of all the ajaxy stuff it works fine but that just won't do.  Is there any way to get this to work?  The controller variable is always null no matter what i do.

 

Thanks.

 

Below is the code:

 

public with sharing class ParameterPassCntrlr {
	public String input {get; set;}
	
	public ParameterPassCntrlr(){
		
	}
	
	public void parameterAction(){
		try {
			this.input = ApexPages.currentpage().getparameters().get('input'); 
		}catch(Exception e){
			ApexPages.addMessage(new ApexPages.message(ApexPages.severity.ERROR,e.getMessage()));
		}
	}
}

 

<apex:page controller="ParameterPassCntrlr" >
	<apex:actionstatus id="pleasewait">
		<apex:facet name="start">
			<div class="waitingSearchDiv" id="el_loading" style="background-color: #fbfbfb;
			height: 100%;opacity:0.65;width:100%;"> 
				<div class="waitingHolder" style="top: 200px; width: 91px;">
						<img class="waitingImage" src="/img/loading.gif" title="Please Wait..." />
					<span class="waitingDescription">Loading...</span>
				</div>
			</div>
		</apex:facet>
	</apex:actionstatus>
	
	<apex:form >	
		<apex:pageBlock mode="submit" title="Param Pass">            
			<apex:outputPanel id="messages">			                
				<apex:pageMessages />
			</apex:outputPanel>
	            
			<apex:pageBlockButtons >
				<apex:actionRegion >
					<apex:commandButton value="Pass" action="{!parameterAction}" reRender="output" status="pleasewait" >						
					</apex:commandButton>
				</apex:actionRegion>
			</apex:pageBlockButtons>
			
			<apex:pageBlockSection showHeader="true" title="INPUT" columns="1">
				<apex:inputText value="{!input}" label="Input:" title="Input"/>
			</apex:pageBlockSection>
			
			<apex:pageBlockSection Id="output" showHeader="true" title="OUTPUT" columns="1">
				<apex:outputText value="{!input}" label="Output:" />
			</apex:pageBlockSection>		 						
		</apex:pageBlock>	
	</apex:form>
	
</apex:page>

 

Best Answer chosen by Admin (Salesforce Developers) 
bob_buzzardbob_buzzard

You shouldn't need to pass the value as a parameter.  If you simply remove the actionregion, the text that is entered will be present in the input field by the time that your actionmethod is executed.  Using rerender (and this making it an ajax request) makes no difference to the postback.  I've written a blog post explaining this in more detail at:

 

http://bobbuzzard.blogspot.com/2011/07/passing-parameters-to-apex-method-from.html

 

For what its worth, I'd imagine the reason it is null is that your commandbutton is in its own actionregion, which means that the postback will exclude all inputs outside that region.

All Answers

Alok_NagarroAlok_Nagarro

Hi,

 

First thing is that you are using <apex:actionRegion> in wrong way, this tag is used to define a region that will only submit to server, whatever component resides within this region that all are submitted to server as the part of request. That's why in your code the value of textfield is not submitting to server.

Second thing is that you have binded input text to controller so that value will be available to that controller property not via parameters. Now try the code given below -

 

// ---------Controller-----------------

public with sharing class ParameterPassCntrlr {
    public String input {get; set;}
      
    public void parameterAction()
    {
       // do nothing
    }
}

 

//----------VF Page--------------

<apex:page controller="ParameterPassCntrlr" >
    <apex:actionstatus id="pleasewait">
        <apex:facet name="start">
            <div class="waitingSearchDiv" id="el_loading" style="background-color: #fbfbfb;
            height: 100%;opacity:0.65;width:100%;">
                <div class="waitingHolder" style="top: 200px; width: 91px;">
                        <img class="waitingImage" src="/img/loading.gif" title="Please Wait..." />
                    <span class="waitingDescription">Loading...</span>
                </div>
            </div>
        </apex:facet>
    </apex:actionstatus>
    
    <apex:form >    
        <apex:pageBlock mode="submit" title="Param Pass">            
            <apex:outputPanel id="messages">                            
                <apex:pageMessages />
            </apex:outputPanel>
             
            <apex:pageBlockButtons >
               
                    <apex:commandButton value="Pass" action="{!parameterAction}" reRender="output" status="pleasewait" >                        
                    </apex:commandButton>
               
            </apex:pageBlockButtons>
             <apex:actionRegion >   
            <apex:pageBlockSection showHeader="true" title="INPUT" columns="1">
                <apex:inputText value="{!input}" label="Input:" title="Input"/>
            </apex:pageBlockSection>
             </apex:actionRegion>
            <apex:pageBlockSection Id="output" showHeader="true" title="OUTPUT" columns="1">
                <apex:outputText value="{!input}" label="Output:" />
            </apex:pageBlockSection>                                
        </apex:pageBlock>   
    </apex:form>
    
</apex:page>

bob_buzzardbob_buzzard

You shouldn't need to pass the value as a parameter.  If you simply remove the actionregion, the text that is entered will be present in the input field by the time that your actionmethod is executed.  Using rerender (and this making it an ajax request) makes no difference to the postback.  I've written a blog post explaining this in more detail at:

 

http://bobbuzzard.blogspot.com/2011/07/passing-parameters-to-apex-method-from.html

 

For what its worth, I'd imagine the reason it is null is that your commandbutton is in its own actionregion, which means that the postback will exclude all inputs outside that region.

This was selected as the best answer
astroboiiiastroboiii

Thanks guys. Bob your suggestion worked like a charm.  Definitely lack of understanding the actionRegion function.  

Alok your suggestion is also helpful in understanding an alternative way of passing values to the controller.  

 

Is there a reason why one would choose one over the other?  I'm sure I've read why, I guess I ask for completeness sake and hope to help myself and others looking for an answer in one spot.

 

Thanks

bob_buzzardbob_buzzard

I'd use the apex:param mechanism to pass parameters back to the controller so that I could identify a particular selection by a user.  I've covered this in more detail in the blog post above.

 

For simply updating the state of the record being edited etc, I'd always go with the standard mechanism of backing input components with values from the controller.

 

I tend to use actionregion when I want to break my form up into multiple sub forms. That allows me to submit the part of the form that I am interested in, even if required fields are missing elsewhere in the form.