When running a callback inside a loop, we need to pass a variable defined in the loop into the callb

We have two models. We are trying to take the id from the first model and pass it into the second model to find the matching record. When we find the matching record, we are trying to find it’s id and pass it into a field in the field of the current row of the first model. The code seems to work, but inside the callback for update, it always uses the same row id. We tried passing the row id into the callback function, but that doesn’t work either. How do we ensure that the callback function gets access to the id of the current record in the outer for loop?

var params = arguments[0], $ = skuid.$;

var skuid_model = skuid.model.getModel(‘MMA_Transfirst_Batch_Transaction’);
var skuid_model_number_of_records = skuid_model.data.length;
console.log (’ skuid_model_number_of_records ’ + skuid_model_number_of_records);
for (i=0;i<skuid_model_number_of_records;i++){
console.log ( " ==>i " + i );
var TransBatchRow = skuid_model.data[i];
var TransBatchRowId = TransBatchRow.Id;
console.log (‘TransBatchRowId’ + TransBatchRowId);
var UpdateRowsBatchTrans = {};
var CnPTrans_Model = skuid.model.getModel(‘CnP_PaaS__CnP_Transaction_Filtered’);
var TranRefNo = TransBatchRow.Tran_Ref__c;
console.log (’ TranRefNo ’ + TranRefNo);
var TransRefNoCondition = CnPTrans_Model.getConditionByName(‘TransRefCondition’);
CnPTrans_Model.setCondition(TransRefNoCondition,TranRefNo);
CnPTrans_Model.updateData(
function(TransBatchRowId){
x=TransBatchRowId.toString();
console.log (‘===> TransBatchRowId ’ + x);
var NoOfMatches=CnPTrans_Model.data.length;
console.log(’ NoOfMatches ’ + NoOfMatches);
if(NoOfMatches===1){
var CnPName=CnPTrans_Model.data[0].Name;
//data[0] because there is only one row after we apply the filter
console.log(’ CnPName ’ + CnPName);
UpdateRowsBatchTrans[TransBatchRowId] = {C_P_Transaction__c:CnPName };
var x=JSON.stringify(UpdateRowsBatchTrans);
console.log (’ UpdateRowsBatchTrans ’ + x);
skuid_model.updateRows( UpdateRowsBatchTrans );
}//end of if
}//end of function
);//end of updateData on line 16
} //End for loop

Anna,

I may not be following your flow correctly, but couldn’t you use ‘i’ from your ‘for’ loop as the index in the line ‘var CnPName=CnPTrans_Model.data[0].Name;’.  So this line would look like:

var CnPName=CnPTrans_Model.data[<b>i</b>].Name;

Using the ‘i’ will allow you to select the matching row from all the rows in the model.  Using ‘0’ means you always get the first row in the model.

Thanks,

Bill