'in' operator bug in model condition

  • 1
  • Question
  • Updated 4 years ago
I have a model (ESCO Product) with a condition where records are in another model (PTI). 
I want to update the source model (PTI) using Javascript, which will then affect the 'in' condition.
I am assuming that if I call update data on the PTI model and then call update data on the ESCO Product, the in portion will have been updated and therefore be properly updated. Here is my code (which runs on a third model : ) )
<code>
var params = arguments[0],   $ = skuid.$;
var model = params.model;
var PTI = skuid.$M('PTI');
var ESCO = skuid.$M('ESCOProduct');
console.log(ESCO);
var adder = skuid.$M('Adder');
var item = params.item;
var row = item.row;
var Id = model.getFieldValue(row,'Id');
var indexCondition = PTI.getConditionByName('Index');
console.log(PTI);
PTI.setCondition(indexCondition,Id);
PTI.updateData({callback:function(){
    adder.updateData();
    ESCO.updateData();
}});
console.log(PTI);
console.log(adder);
console.log(ESCO);
$.each(PTI.data, function(coolRow){
    console.log(PTI.getFieldValue(coolRow,'ESCO_Product__c', true));
});
</code>
I have been looking in the console, and the SOQL for the ESCO Product model seems to be the same even after updating the PTI model. Can this not be affected by code? I am also getting the following error on the loop :
Uncaught TypeError: Cannot use 'in' operator to search for 'ESCO_Product__c' in 1
I would appreciate some feedback, Thanks.
Photo of Moshe Karmel

Moshe Karmel, Champion

  • 8,646 Points 5k badge 2x thumb

Posted 4 years ago

  • 1
Photo of Zach McElrath

Zach McElrath, Employee

  • 49,056 Points 20k badge 2x thumb
Hi Moshe,

So the trick with "Field from another Model" Conditions as they relate to JavaScript is this: you have to call updateData on the source models as well as dependent Models concurrently in order to get the "Field from another Model" Conditions to reevaluate themselves --- otherwise, there's no Source Model in Apex memory for the dependent Models to pull from, and so they will rely on their existing values.

What this means is that you should use the multi-Model version of updateData, like this:

PTI.setCondition(indexCondition,Id);
skuid.model.updateData([PTI,adder,ESCO],function(){
   console.log('in callback');
   console.log(PTI);
   console.log(adder);
   console.log(ESCO);
   $.each(PTI.data, function(coolRow){
      console.log(PTI.getFieldValue(coolRow,'ESCO_Product__c', true));
   });
});

Photo of Moshe Karmel

Moshe Karmel, Champion

  • 8,646 Points 5k badge 2x thumb
Thanks Zach, however it still doesn't seem to be working. The PTI model gets updated so that the "data" element contains only 2 records instead of the original 91. But the ESCO model still is using the original values in the "in" condition. And the soql for the ESCO model looks like this...
"SELECT Id,Name,Max_Adder_Date__c,LDC__r.Name,LDC__c,Index__r.Name,Index__c,Commodity__r.Name,Commodity__c,Adder_Rate__c FROM ESCO_Product__c WHERE (Id in ('a39e0000000Ax2VAAS','a39e0000000Ax9WAAS','a39e0000000Az4FAAS','a39e0000000Az4UAAS','a39e0000000Az53AAC','a39e0000000AzBpAAK','a39e0000000AzDMAA0','a39e0000000AzBaAAK' " etc... it's still using the original values even though PTI has been updated.
Photo of Ben Hubbard

Ben Hubbard, Employee

  • 12,490 Points 10k badge 2x thumb
Moshe,

I think you're right about this.  I'll need to investigate a little more, but it looks like in certain cases, the values that are put into the condition never change once the page first loads.  I'll try to replicate this issue in our dev org.