Dynamic input of global variable from JS query to SQL query

Hi,

I am still fairly new to ReTool so please bare with me.

I am trying to impliment the following JS code, such that it updates my DB with the two global values. I used additionalScope to get the values correct. However, when I run it I keep getting the last indexed value in the array. I believe I set the nested While loops correctly, but I could be wrong (also fairly novice at JS).

Note, all the console logs show a list of all the corrected values for both variables, but they are not translating into the SQL query. The SQL query is just a simple INSERT INTO / Value query.

function uniqueFilter(value, index, self) {
    return self.indexOf(value) === index;
}
var currentTime = new Date();
var yr = currentTime.getFullYear();
var borrowers = list_active_borrowers.data.borrower_id.filter(uniqueFilter);
var numBorrowers = list_active_borrowers.data.borrower_id.filter(uniqueFilter).length;
var reports = list_reports.data.report_type.filter(uniqueFilter);
var numReports = list_reports.data.report_type.length;
var x = 0;
var i = 0;
var y = 0;
while (x < numBorrowers) {
  selectedBorrower.value = borrowers[x];
  console.log(selectedBorrower.value)
  while (i < numReports) {
    selectedReport.value = reports[i];
    console.log(selectedReport.value)
    while (y < 5) {
      query21.trigger({additionalScope:{selectedBorrower, selectedReport}});
      y++ 
    }
    y = 0
    i++
  }
  i = 0
  x++
}```

I think the problem is that you're modifying the global values selectedBorrower & selectedReport then use it in query21.trigger. I don't think those values will be updated synchronously/immediately, so query21.trigger will not be run with the correct value.

Why don't you pass in the value of borrowers[x] and reports[i] directly to the query21.trigger call?

1 Like

Ah ha! That worked! I wasn't sure if I could do that since it wasnt a global value.

Thank you!

1 Like

You're welcome!

1 Like