+ Start a Discussion
David HollandDavid Holland 

Remote Actions not computing in correct order!

Hi everyone. I have a JS/JQuery function, which also calls a remote action from a controller.

 

I have written it so that it completes the remote action first, as the results from this is needed later on. However, it is firing the loop before computing the remote action, meaning I am not populating the fields I need to.

 

Below is the code:

 

function UpdateTotals(){

var EntityId = 'a1Mc00000001aaw';
var CurrentReg = 0;
var ProjReg = 0;

Visualforce.remoting.Manager.invokeAction(
'{!$RemoteAction.VWPCareTradeExtension.getFeesList}',
EntityId,
function(result, event){
if (event.status) {
CurrentReg = result.Current_Registration__c;
ProjReg = result.Projected_Registration__c;
alert('First:' + CurrentReg);

} else if (event.type === 'exception') {
alert('There is No Fee Record for that Entity.');
} else {
alert('Something Has Gone Wrong, please check all details and try again.');
}
},
{escape: true}
);


for (var i=1;i<6;i++){

alert (i+CurrentReg);
var Type = j$("select[id$=Type"+i+"]").val();

//if (Type=='CTA' || Type='FMT'){

j$("input[id$=Registration"+i+"]").val(CurrentReg);
//}


// Passes all values to function, which resolves to numbers, or null (so we do not get any NaN errors).
var abdir = getFieldValue("ABDir"+i);
var abdep = getFieldValue("ABDepreciation"+i);
var abman = getFieldValue("ABManage"+i);
var abban = getFieldValue("ABBank"+i);
var abexc = getFieldValue("Excessive"+i);
var dir = getFieldValue("NDir"+i);
var dep = getFieldValue("NDepreciation"+i);
var man = getFieldValue("NManage"+i);
var ban = getFieldValue("NBank"+i);
var equ = getFieldValue("NEquipment"+i);
var othPerson = getFieldValue("OtherPerson"+i);
var feeInc = getFieldValue("FeeInc"+i);
var otherInc = getFieldValue("OtherInc"+i);
var wage = getFieldValue("Wages"+i);
var otherwage = getFieldValue("OtherWages"+i);
var rate = getFieldValue("Rates"+i);
var refuse = getFieldValue("Refuse"+i);
var heat = getFieldValue("Heat"+i);
var water = getFieldValue("Water"+i);
var repair = getFieldValue("Repair"+i);
var insure = getFieldValue("Insure"+i);
var rent = getFieldValue("Rent"+i);
var otherProp = getFieldValue("PropOther"+i);
var food = getFieldValue("Food"+i);
var laundry = getFieldValue("Laundry"+i);
var car = getFieldValue("Car"+i);
var account = getFieldValue("Account"+i);
var uniform = getFieldValue("Uniform"+i);
var medical = getFieldValue("Medical"+i);
var welfare = getFieldValue("Welfare"+i);
var stationary = getFieldValue("Stationary"+i);
var regFees = getFieldValue("RegFees"+i);
var market = getFieldValue("Market"+i);
var sundries = getFieldValue("Sundries"+i);
var otherOper = getFieldValue("OtherOper"+i);
var reg = getFieldValue("Registration"+i);
var averageFill = getFieldValue("AverageFill"+i);

//Subtotals

var propTotal = rate+refuse+heat+water+repair+insure+otherProp;
var operTotal = food+laundry+car+account+uniform+medical+welfare+stationary+regFees+market+sundries+otherOper;
var personTotal = dir+dep+man+ban+equ+othPerson;
var totalWages = wage+otherwage;
var totalInc = feeInc+otherInc;
var abTotal = abdir+abdep+abman+abban+abexc;
var ebitda = totalInc-totalWages-propTotal-operTotal-personTotal;
var totalOther = propTotal+operTotal+personTotal;
var totalTotal = totalOther+totalWages;

j$("input[id$=TotalInc"+i+"]").val(totalInc);
j$("input[id$=TotalWages"+i+"]").val(totalWages );
j$("input[id$=PropTotal"+i+"]").val(propTotal);
j$("input[id$=OperTotal"+i+"]").val(operTotal);
j$("input[id$=PersonTotal"+i+"]").val(personTotal);
j$("input[id$=ABTotal"+i+"]").val(abTotal);
j$("input[id$=TotalOther"+i+"]").val(totalOther);
j$("input[id$=TotalTotal"+i+"]").val(totalTotal);
j$("input[id$=EBITDA"+i+"]").val(ebitda);
j$("input[id$=EBITDAR"+i+"]").val(ebitda+rent);
j$("input[id$=EBITDAAdj"+i+"]").val(abTotal+ebitda);
j$("input[id$=EBITDAPB"+i+"]").val(reg!=0?((abTotal+ebitda)/reg).toFixed(2):0);
j$("input[id$=Expenses"+i+"]").val(totalTotal);
j$("input[id$=ExpensesPRPW"+i+"]").val(averageFill!=0?(((totalTotal)/averageFill)/52).toFixed(2):0);
j$("input[id$=NPC"+i+"]").val(totalOther);
j$("input[id$=NPCPRPW"+i+"]").val(averageFill!=0?(((totalOther)/averageFill)/52).toFixed(2):0);

}
}
function getFieldValue(fieldName){

if (!isNaN(parseInt(j$("input[id$="+fieldName+"]").val(),10))){
return (parseInt(j$("input[id$="+fieldName+"]").val(),10));
}
else {
return 0;
}
}

 

The alerts(i+reg) is firing before the alert('First:'+reg), meaning I am not populating the data.

Best Answer chosen by Admin (Salesforce Developers) 
Peter_sfdcPeter_sfdc

I'm pretty sure the problem is that the call to alert('First:' + CurrentReg) happens in the callback anonymous function. This function is not invoked synchronously with the other code, rather at some undetermined point in the future when the AJAX request decides to come back from the server. 

 

In the mean time, once the remote call is invoked, the rest of your code in the update totals function will go off, most likely before the callback is actually invoked. As a fix, you might try taking the code, putting it in a different function and then invoking it from the callback, or even just putting it inside the callback function itself. 

 

Here's a stab in pseudocode as to what that might look like (also, I removed your getFieldValue function to make it more concise): 

 

function UpdateTotals(){

var EntityId = 'a1Mc00000001aaw';
var CurrentReg = 0;
var ProjReg = 0;

Visualforce.remoting.Manager.invokeAction(
'{!$RemoteAction.VWPCareTradeExtension.getFeesList}',EntityId,
function(result, event){
	if (event.status) {
		CurrentReg = result.Current_Registration__c;
		ProjReg = result.Projected_Registration__c;
		alert('First:' + CurrentReg);
		updateRegTotals(CurrentReg,ProjReg);

	} else if (event.type === 'exception') {
		alert('There is No Fee Record for that Entity.');
	} else {
		alert('Something Has Gone Wrong, please check all details and try again.');
	}
},
{escape: true}
);

function updateRegTotals(CurrentReg, ProjReg){
	for (var i=1;i<6;i++){

	alert (i+CurrentReg);
	var Type = j$("select[id$=Type"+i+"]").val();

	//if (Type=='CTA' || Type='FMT'){

	j$("input[id$=Registration"+i+"]").val(CurrentReg);
	//}


	// Passes all values to function, which resolves to numbers, or null (so we do not get any NaN errors).
	var abdir = getFieldValue("ABDir"+i);
...more get field value stuff...
	var averageFill = getFieldValue("AverageFill"+i);

	//Subtotals

	var propTotal = rate+refuse+heat+water+repair+insure+otherProp;
...more subtotals stuff...
	var totalTotal = totalOther+totalWages;

	j$("input[id$=TotalInc"+i+"]").val(totalInc);
...more jquery stuff...
	j$("input[id$=NPCPRPW"+i+"]").val(averageFill!=0?(((totalOther)/averageFill)/52).toFixed(2):0);

	}
	}
}	

}