
My VisualForce Page: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';
}
}
<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>
- 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?
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.