Click Here

I never see the debug line in methodTwo priting. Debug line in methodThree always says that the variable value is null This might be javascript or jquery but the page reloads after i clicking on the p element Anybody suggestions on what I am doing wrong?", "answerCount": 3, "upvoteCount": 0, "datePublished": "2015-03-23T13:59:34.000Z", "author": { "@type": "Person", "name": "Mark Cuban", "url": "https://trailblazers.salesforce.com/profileView?u=0053A00000EL7XyQAL", "affiliation": { "@type": "Organization", "name": "--" } }, "acceptedAnswer": { "@type": "Answer", "text": "You have a race condition there - your JavaScript invokes setValueJS followed immediately by methodOneJS. Both of these fire off a postback and whichever one fires last will win.  The losing postback request will simply be dropped, as I believe the server will view that as a connection reset.  If you need to call multiple methods from the server side you have two choices I think: (1) Invoke them all server side from a single action method - I'd always recommend this as its much more straightforward to understand. (2) Use the rerender tags to turn the requests into Ajax and add oncomplete handlers that invoke the next function in the chain.  These can be hard to debug.", "upvoteCount": 1, "url": "https://trailhead.salesforce.com/trailblazer-community/feed/0D54V00007T48SASAZ", "datePublished": "2015-03-24T10:13:28.000Z", "author": { "@type": "Person", "name": "Keir Bowden", "url": "https://trailblazers.salesforce.com/profileView?u=005300000030rqoAAA", "affiliation": { "@type": "Organization", "name": "Self-employed" } } }, "suggestedAnswer": [ { "@type": "Answer", "text": "Thank yo so much bob. You are absolutely true. I spent two days on this issue and finally figured that out these are not executing in sequence. Why doesn't Javascript wait until the setValueJS method is executed successfully before executing the methodOneJS? Is it because these are actionFunctions?? Thanks again for your time in looking into it.", "upvoteCount": 0, "url": "https://trailhead.salesforce.com/trailblazer-community/feed/0D54V00007T48SASAZ", "datePublished": "2015-03-24T13:26:24.000Z", "author": { "@type": "Person", "name": "Mark Cuban", "url": "https://trailblazers.salesforce.com/profileView?u=0053A00000EL7XyQAL", "affiliation": { "@type": "Organization", "name": "--" } } } ] } } Skip to main content
I am setting a Controller extension variable from VF page using apex action Function in the call back of the remote action method and then trying to read this value from another method in the controller extension.

Here is the code:

Controller Extension:

 

global with sharing class MyController {

public String variableOne{get;set;}

public MyController(ApexPages.StandardController stdController) {

}

public PageReference methodTwo() {

System.debug('Variable value from methodTwo is '+ variableOne);

return null;

}

public PageReference methodThree() {

System.debug('variable value from methodThree is '+ variableOne);

return null;

}

@RemoteAction

global static String methodOne(String s1){

return s1+'123';

}

}

My VisualForce Page:

 

<apex:page standardController="Account" extensions="MyController">

<c:ScriptsComponent id="scmp"/>

<script type="text/javascript">

var records;

function functionOne(){

console.log('Entered the functionOne method');

Visualforce.remoting.Manager.invokeAction(

'{!$RemoteAction.MyController.methodOne}',

'Andrew',

function(result,event){

console.log('result->' + JSON.stringify(result));

records = result;

if(event.status){

console.log('result we got back from controller is '+records);

setValueJS(records);

methodOneJS();

}

});

}

</script>

<div>

<p onClick="functionOne(); return false;">Click Here</p>

<apex:form >

<apex:actionFunction name="setValueJS" action="{!methodTwo}">

<apex:param name="parm1" assignTo="{!variableOne}" value=""></apex:param>

</apex:actionFunction>

<apex:actionFunction name="methodOneJS" action="{!methodThree}">

</apex:actionFunction>

</apex:form>

</div>

</apex:page>

  1. I never see the debug line in methodTwo priting.
  2. Debug line in methodThree always says that the variable value is null
  3. This might be javascript or jquery but the page reloads after i clicking on the p element

Anybody suggestions on what I am doing wrong?

3 answers
  1. Mar 24, 2015, 10:13 AM
    You have a race condition there - your JavaScript invokes setValueJS followed immediately by methodOneJS. Both of these fire off a postback and whichever one fires last will win.  The losing postback request will simply be dropped, as I believe the server will view that as a connection reset.  If you need to call multiple methods from the server side you have two choices I think:

    (1) Invoke them all server side from a single action method - I'd always recommend this as its much more straightforward to understand.

    (2) Use the rerender tags to turn the requests into Ajax and add oncomplete handlers that invoke the next function in the chain.  These can be hard to debug.
0/9000