+ Start a Discussion
dJadhavdJadhav 

Retrieve updated value of Controller variable in JS function

Hi,

 

I have 'message'  variable in my controller.  onComplete event of actionFucntion,I want  updated value of message  variable.

 

Could you please tell me how to do this?

 

Thanks in advance.

Dipak

Best Answer chosen by Admin (Salesforce Developers) 
Saurabh DhobleSaurabh Dhoble

Dipak,

 

Here's a working solution, you can copy/paste the code into test classes and try it out.

 

The VF page :-

<apex:page controller="SimpleExampleController">
    <apex:form id="theForm">
        <apex:pageBlock id="thePageBlock">
            <apex:outputPanel onclick="jsCallMethod()">Click me to call the action function</apex:outputPanel>
            <apex:actionFunction action="{!callMethod}" name="jsCallMethod" reRender="thePageBlock" />
        	<script>
                alert("Current value of variable : {!var}");
            </script>            
        </apex:pageBlock>
    </apex:form>
</apex:page>

 

And the controller :-

public class SimpleExampleController {
	public string var
    {
        get
        {
            if(var==null)
                var='Set from get method';
            return var;
        }
        set;
    }
    
    public void callMethod()
    {
        var='Set from inside callMethod()';
    }
}

 

Of special note here is that, in the reRender attribute of the action function, I am specifying the entire pageBlock. Especially I want to refresh the javascript code. This is so that when the entire pageBlock is refreshed, the "{!var}" variable is also refreshed in the javascript.

 

All Answers

Sridhar VenkateswaraluSridhar Venkateswaralu

Hi

 

Think u should be able to pass the variable directly to the JS function.

 

have a function

<script>

function testFunction(test){

alert(test);

}

</script>

 

and in VF, pass the (Message) controller Variable as a parameter to the JS function.

<apex:actionFunction name="call" action="{!Submit}" oncomplete="testFunction('{!message}')"

 

 

Saurabh DhobleSaurabh Dhoble

I'm wondering what exactly you are tring to do. You can set the value of the controller variable from within the controller method itself. Why would you do that in the onComplete event via javascript. Can you please post your code.

 

Thx.

Sridhar VenkateswaraluSridhar Venkateswaralu

Hi Saurabh,

 

As per my understanding, think the requirement is not to set the value to the variable rather it is to retrieve the value of the  variable set in the Controller to the Javascript Function on Oncomplete of an action Function.

 

Thanks,

dJadhavdJadhav

Sridhar Venkateswaralu wrote:

Hi Saurabh,

 

As per my understanding, think the requirement is not to set the value to the variable rather it is to retrieve the value of the  variable set in the Controller to the Javascript Function on Oncomplete of an action Function.

 

Thanks,


Hi Saurabh,

Sridhar is correct. I want to retive the updated value of controller varible to the JS function on Oncomplete event.

 

Thanks,

Dipak

Sridhar VenkateswaraluSridhar Venkateswaralu

Hi Dipak,

 

Did you try the Solution above?

 

If it works, can you please mark it as Solution.

 

Thanks,

 

 

Saurabh DhobleSaurabh Dhoble

Dipak,

 

Here's a working solution, you can copy/paste the code into test classes and try it out.

 

The VF page :-

<apex:page controller="SimpleExampleController">
    <apex:form id="theForm">
        <apex:pageBlock id="thePageBlock">
            <apex:outputPanel onclick="jsCallMethod()">Click me to call the action function</apex:outputPanel>
            <apex:actionFunction action="{!callMethod}" name="jsCallMethod" reRender="thePageBlock" />
        	<script>
                alert("Current value of variable : {!var}");
            </script>            
        </apex:pageBlock>
    </apex:form>
</apex:page>

 

And the controller :-

public class SimpleExampleController {
	public string var
    {
        get
        {
            if(var==null)
                var='Set from get method';
            return var;
        }
        set;
    }
    
    public void callMethod()
    {
        var='Set from inside callMethod()';
    }
}

 

Of special note here is that, in the reRender attribute of the action function, I am specifying the entire pageBlock. Especially I want to refresh the javascript code. This is so that when the entire pageBlock is refreshed, the "{!var}" variable is also refreshed in the javascript.

 

This was selected as the best answer
Andrew B. DavisAndrew B. Davis
Based on Sridhar Venkateswaralu's proposed solution, here is a code snippet illustrating this:
VF Page:
<script>
  var messageJS = "";
  someAF();
  // This value will NOT show correctly, probably 
  // (it won't be updated in time by the asynchronous actionFunction call
  alert(messageJS); 
</script>
<apex:actionFunction name="someAF" action="{!someControllerMethod}" oncomplete="messageJS = '{!message}'; alert(messageJS); // This value WILL show correctly">
Controller:
 
public String message {get;set;}
public void someControllerMethod() {
  message = 'The message is from the controller';
}