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>
MyController
is not astandardController
, it is a customcontroller
. Also, I thoughtactionFunction
had to be nested in aform
tag. Finally,methodOne
does not return any value. How did this ever save?@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.