0

I don't know how to put this question, so I will just start with the code I have:

MyController.cls

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';
    }

}

TestPage.page

<apex:page standardController="Account" extensions="MyController">
<c:ScriptsComponent id="scriptsComponent"/>
<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>

Problem:

My Testpage is sending a variable to the methodOne in Controller and it returns the string by appending 123 and then I set this to a variable on the controller using methodTwo defined by actionFunction and then trying to retrieve this variable from methodThree..

But this is what happening:

  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 click on the p element
1

Notice that your @RemoteAction is static and hence cannot modify page state, which you are trying to access from your other methods. If you wish to modify page state, do not use a static method.

  • I cannot remove static from methodOne beause it needs to be invoked using RemoteAction.. Any other alternative?? – javanoob Mar 23 '15 at 15:27
  • setValueJS method which is calling the non-static methodTwo but still methodThree is having null reference for the variable.. – javanoob Mar 23 '15 at 15:28
  • I knew that it has nothing to do with static vs instance. Thanks for your time Adrian looking into it. – javanoob Mar 24 '15 at 13:18
1

Use reRender="" attribute in methodTwo actionFunction like this :

<apex:actionFunction name="setValueJS" action="{!methodTwo}" reRender="">
        <apex:param name="parm1" assignTo="{!variableOne}" value=""></apex:param>
 </apex:actionFunction>

Try doing same with methodThree as well.

0

Finally I was able to find the solution..

What was happening is the below two lines are not executed in sequence.

            setValueJS(records);
            methodOneJS();

To solve this, I added onComplete attribute for the actionFunction 'setValueJS' to execute the methodOneJS

Not sure why it was running asynchronously.

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.