Model Condition Not Updated in a Loop

  • 1
  • Problem
  • Updated 5 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(scoreModelInit.data, 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(scoreModelFinal.data)); }); });
----------------------------------------------------------------------------------------------------

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
  field:"Interviewer_Override__c"
  fieldTargetObjects:"Contact"
  inactive:false
  label:""
  name:"Interviewer_Override__c"
  noValueBehavior:"deactivate"
  operator:"="
  originalInactive:false
  originalValue:""
  row:0
  state:"filterableon"
  type:"fieldvalue"
  value:"00380000012V8SjAAK" <== THIS VALUE SHOULD BE 0038000000YJgmYAAT
  values:undefined
}

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?


Thanks!
Eulogio
Photo of Eulogio Gallo

Eulogio Gallo

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

Posted 12 months ago

  • 1
Photo of John Dahlberg

John Dahlberg, Champion

  • 2,442 Points 2k 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,874 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.
Photo of Peter Herzog

Peter Herzog

  • 1,664 Points 1k badge 2x thumb
Hey Eulogio, I am stuck on the same thing.  Would you mind sharing your solution?