Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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}";
share|improve this question
up vote 4 down vote accepted

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

share|improve this answer
    
This will definitely work. The only thing is I don't think you'd want to redeclare the variable, although maybe you would if it was the first, and only time you were using it or were just using it local to that reRender instant. – Peter Knolle Dec 15 '12 at 0:36
1  
is there any way I can get 'contractURL' value in java script without putting it into page and do re-render ? like directly make server call and read it from there ? – Prafulla Patil Dec 15 '12 at 1:13
1  
take a look at Javascript Remoting: salesforce.com/us/developer/docs/pages/Content/… – pjcarly Dec 15 '12 at 13:33
    
I'll add it to my answer, as the comment helped :) – pjcarly Dec 17 '12 at 9:59

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.

share|improve this answer
    
is there any way I can get 'contractURL' value in java script without putting it into page and do re-render ? like directly make server call and read it from there ? – Prafulla Patil Dec 15 '12 at 0:57
3  
You'd need to use JavaScript remoting for that, I think. You tend to lose the stateful nature of the page though. – Bob Buzzard Dec 15 '12 at 8:38

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 wanted 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}); "/>
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.