function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
deter danglerdeter dangler 

Variable set from VF page is not available for a controller method

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?
Best Answer chosen by deter dangler
bob_buzzardbob_buzzard
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.

All Answers

bob_buzzardbob_buzzard
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.
This was selected as the best answer
deter danglerdeter dangler
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.
bob_buzzardbob_buzzard
JavaScript is asynchronous, so quite a lot of the time control is returned to the calling code while a response is pending from the server. That's why there are so many "working" spinners out there, whose sole job is to block the user from doing anything until the call completes!