1

My requirement is:

On the click of a command button, I need to call a controller method and set a value to a controller variable. After that assignment is done, oncomplete attribute of actionfunction should call the javascript code which uses that controller variable value which was set when we clicked on the button. When I did debug the the value using console.log, I don't see any value and it is showing as null. But the Debug statement in the controller is showing the value as expected.

The value is is not passed to VF page JS code. Please let me know if I am doing any wrong in the code. Here's the snippet of the code.

// Javacript Code
<script type="text/javascript">

function secondary ( action) {
   var url;
    if ( action == 'Provider' ) {
        url = "{!MWUrl}";
        console.log('URL ===='+url);       
    }
}

</script>


<apex:actionFunction name="mWProviderSearchURL" action="
{!mWProviderSearchURL}" rerender="form" oncomplete="secondary('Provider');" 
/>
<apex:commandButton onclick="mWProviderSearchURL(); return false;">

public class exampleController {

  public String MWUrl {get; set;}

  public void mWProviderSearchURL() {

    MWUrl = 'https://www.google.com';
    System.debug('MWUrl===='+MWUrl);
  }
}
  • Is your <script> inside the form? If not, it won't get the freshly rerendered value. – Caspar Harmer May 16 '17 at 1:46
  • hang on... you need to make MWUrl a string. Currently it's not defined – Caspar Harmer May 16 '17 at 1:50
  • Or it has a type, but no var name. Not sure how that code compiled - or was that a typo? – Caspar Harmer May 16 '17 at 1:51
  • @CasparHarmer I have edited the code to declare it as String. <Script> is inside form. I just added part of code to make it look simple. – SFDC_BigDog May 16 '17 at 1:55
  • Try changing the command button to a regular button - you don't need a command button since you have an actionfunction – Caspar Harmer May 16 '17 at 1:56
2

If you Javascript is outside of your rerender target, then the value won't be refreshed on completion of your server call.

Move the <script> tag inside the form to ensure that it gets rerendered or add it to an outputPanel and add that to the rerender target.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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