Model Condition Not Updated in a Loop

  • 1
  • Problem
  • Updated 3 months ago
  • Solved
  • (Edited)
I'm trying requery a model and compile all the data into a few tables. I'm finding, however, that my model's condition isn't being updated correctly.  Here's my code:

var interviewerSet = new Set();

// get list of all reviewers $.each(, function(i, row) { interviewerSet.add(row.Interviewer_Override__c); }); // create Score_Object for each record interviewerSet.forEach(function(interviewerId) { console.log(interviewerId); scoreModelFinal.setCondition(conditionName, interviewerId); console.log(scoreModelFinal.conditions[1]); // this output remains the same $.when(scoreModelFinal.updateData()).then(function() { scoreList.push(new Score_Object().CreateObject(; }); });

So I'm iterating through this set and using the values to set my condition and requery, but the condition is always stuck on the last value I give it.

Here are some of the logs I'm getting:

interviewerId : 0038000000YJgmYAAT
scoreModelFinal.conditions[1] : {
  encloseValueInQuotes: true
  value:"00380000012V8SjAAK" <== THIS VALUE SHOULD BE 0038000000YJgmYAAT

The condition stays at 00380000012V8SjAAK, even though this is the last value I'm passing in!  Any advice on how to get these model query calls to execute?

Photo of Eulogio Gallo

Eulogio Gallo

  • 1,464 Points 1k badge 2x thumb
  • frustrated

Posted 4 months ago

  • 1
Photo of John Dahlberg

John Dahlberg, Champion

  • 1,880 Points 1k badge 2x thumb
Looks like you need to move the setCondition out of the loop.  The logic should be to set the array variable, enter the loop where you push values to the array, then outside the loop use the setCondition with set of values in the array.  The condition on the model should be set to multiple values. 
Photo of Eulogio Gallo

Eulogio Gallo

  • 1,464 Points 1k badge 2x thumb
Thanks for the reply John! Your advice was helpful, but didn't address the issue I was facing. I was just not handling promises correctly and doing things out of sequence.  A combination of following your advice and reordering my code execution a bit solved the issue.