Find your content:

Search form

You are here

apex:actionFunction to return value from Controller

 
Share

Is it possible to get Controller property using action function?

my vf controller has property as

public String contractURL {get;set;}

I want to access this latest value in my javascript.

Note - I cannot access it in this way (and thats why I am posting this question :) )-

var url  = "{!contractURL}";

Attribution to: Prafulla Patil

Possible Suggestion/Solution #1

I know this is an old question, and it already has an accepted answer, but I recently had to do the same thing. However, I didn't want to use static method via JavaScript Remoting as I wanted to have access to some controller variables. In the end I used this simple approach and it worked:

Visualforce:

<apex:page controller="ReturnController">

    <apex:form >
        <apex:commandButton action="{!callMe}"
                oncomplete="processReturnedValue({!valueReturned}); "
                value="Click Me!"/>
    </apex:form>

    <script>
        var processReturnedValue = function(valueReturned){
            alert('Returned Value: ' + valueReturned);
        };
    </script>

</apex:page>

Controller:

public class ReturnController {

    public Integer ValueReturned {get; set;}

    public ReturnController(){
        this.ValueReturned = 0;
    }

    public void callMe(){
        this.ValueReturned++;
    }

}

That's it, that worked for me. And you could use the exactly same approach with apex:actionFunction:

<apex:actionFunction action="{!callMe}" name="actionFuncName" oncomplete="processReturnedValue({!valueReturned}); "/>

Attribution to: smukov

Possible Suggestion/Solution #2

try putting var url = {!contractURL} in a <apex:outputPanel> and have it rerendered by your <apex:actionFunction> I don't know for sure if it will work, but give it a try!

If you want to make calls to a controller without having to rerender and use visualforce functionality, just handle the calls and results in javascript, you should take a look at Javascript Remoting: http://www.salesforce.com/us/developer/docs/pages/Content/pages_js_remoting.htm


Attribution to: pjcarly

Possible Suggestion/Solution #3

You should be able to access the controller property in JavaScript that is directly in your page, such as:

var url = "{!contractURL}";

The Visualforce engine will substitute in all of the values when generating the page.

If you are updating that value in your controller as part of an ajax call (e.g., command button with reRender) it won't update the value of the JavaScript value since that part of the page was already generated.

In a part that you are reRendering you could set the value of the variable, or probably better, create a dedicated outputPanel to set it.

<apex:outputPanel id="reRenderedPanelToSetVariable">
   <script type="text/javascript">
      url = "{!contractURL}";
   </script>
</apex:outputPanel>

Then from your command button or whatever you just include the "reRenderedPanelToSetVariable" in the list of ids to rerender.


Attribution to: Peter Knolle
This content is remixed from stackoverflow or stackexchange. Please visit https://salesforce.stackexchange.com/questions/5456

My Block Status

My Block Content