-2

I am setting a variable from VF page by using Javascript Remoting Action and the same variable when I am trying to access from a different method in Controller it always says NULL.

The below code explains the problem I am facing:

Variable is always null when trying to read from the method 'methodOne' in Controller but from the method setValue it shows the correct value sent from VF page.

Controller class:

global with sharing class MyController {
    public String variableOne{get;set;}

    public PageReference setValue() {
        System.debug('Variable value from setValue method is '+ variableOne); 
        //The above line prints the correct value "Andrew123"
        return null;
    }

    public PageReference methodOne() {
        System.debug('variable value from methodOne is '+ variableOne);
        //The above line always prints null Not sure Why
        return null;
    }
}

VisualForce Page:

    <apex:page extensions="MyController" >
        <script>
            function someMethod(){
                Visualforce.remoting.Manager.invokeAction(
                        '{!$RemoteAction.MyController.remoteMethod}',                           
                        "Andrew",                           
                        function(result, event){
                            if (event.status) {   
                    setValueJS(result[0]['Id']);
                    methodOneJS();

                            }else{
                                console.log('event' + event.message);
                            } 
                        }
                    );          
            }

        </script type="text/javascript">

<apex:form>        
        <apex:actionFunction name="setValueJS" action="{!setValue}">
            <apex:param name="parm1" assignTo="" value=""/>
        </apex:actionFunction>
        <apex:actionFunction name="methodOneJS" action="{!methodOne}" />
</apex:form>
    </apex:page>
7
  • Not sure how your page compiles. MyController is not a standardController, it is a custom controller. Also, I thought actionFunction had to be nested in a form tag. Finally, methodOne does not return any value. How did this ever save?
    – Adrian Larson
    Mar 20, 2015 at 20:03
  • I just created this psuedo code to express my problem clearly..I edited the code now..Thanks for your time and sorry for misleading :)
    – ram
    Mar 20, 2015 at 20:08
  • @AdrianLarson It's possible we're just seeing a snippet of his code. Also, you edited out the @RemoteAction method. Aside from the compile issues, I think his code might work but it just behaves differently from how he describes it in his code comments.
    – Jan Julian
    Mar 20, 2015 at 20:10
  • Actually, it is not an extension either, it would be a controller (controller-MyController). One issue is your setter (setValue) should be taking a parameter.
    – JimRae
    Mar 20, 2015 at 20:11
  • @JimRae Then how come debug statement in setValue method shows the value that I am passing from the page.. I thought <apex:param> for setValueJS is setting the controller variable.
    – ram
    Mar 20, 2015 at 20:15

1 Answer 1

0

Alright, I understand you gave us a pseudocode that has errors and that doesn't compile, but I'll attempt to systematically pseudo-answer it anyway.

You cannot set variableOne from MyController.remoteMethod because

  1. variableOne is an instance variable, and
  2. remoteMethod(String) is a static method.

Static methods cannot access instance variables. Your code wouldn't have worked because it wouldn't have compiled in the first place.

As an alternative, create another instance method (ie, a method that does not have the static keyword) that sets the value of variableOne. For example

public void methodTwo() {
    variableOne = ApexPages.currentPage().getParameters().get('myParam');
}

You can bind this method to an actionFunction that passes a value through myParam.

With methodTwo in place, your current methodOne should return the correct value.

1
  • Thanks for the explanation. I think My question was not clear..Actually I am fixing the existing code and not much of a salesforce guy..So i will comeback with a working class and page to express my problem clearly. I know about the static vs instance variable. Thanks for your time.
    – ram
    Mar 20, 2015 at 21:04

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .