Subscribe to event, but only run once

  • 1
  • Question
  • Updated 4 years ago
  • Answered
So I've been trying to use the "row.updated" event to stop users from entering a certain combination of fields. It's working great but I'm also trying to update the field back to the previous value. I'm running into problems with the snippet getting looped and running forever. When I do a simple update to a picklist field it works fine, but when I update a reference field it keeps running forever. I tried using the "initiatorId" to indicate what caused the update, but that doesn't seem to help. My code looks like this, as an inline snippet:

(function(skuid){ var $ = skuid.$;
$(function(){
   //reverse the changes that were made to keep the user from saving the bad combination of data
   function reverseUpdate(updateResult){
       console.log(updateResult);
       var model = skuid.$M(updateResult.modelId);
       if(updateResult.updates.Bill_Type__c){
           model.updateRow(updateResult.row,'Bill_Type__c','Dual Bill',{
               initiatorId: updateResult.initiatorId
           });
       }else if(updateResult.updates.Pricing_Product__c){
           var fixed = skuid.$M('Fixed');
           var fixedRow = fixed.getFirstRow();
           //could this be causing everything to break because it's an update to a reference field?
           model.updateRow(updateResult.row,'Pricing_Product__c',fixedRow.Id,{
               initiatorId: updateResult.initiatorId
           });
       }
   }
   
skuid.events.subscribe('row.updated',function(updateResult){
   if(updateResult.modelId === 'Tenors'){
       var commodityName = skuid.$M('Opportunity').getFirstRow().Commodity__r.Name;
       var countOfRateReadyLDC = 0;
                $.each(skuid.$M('IntakeQueues').data, function(i,rowIQ){
                    countOfRateReadyLDC += rowIQ.LDC__r.LDC_LDC__c;
                });
                var hasSingleBill = false;
                var hasLBMP = false;
                if(updateResult.row.Bill_Type__c === 'Single Bill') hasSingleBill = true;
                if(updateResult.row.Pricing_Product__r.Name === 'LBMP+') hasLBMP = true;
                if(hasLBMP && hasSingleBill && (countOfRateReadyLDC > 0) && (commodityName === 'Electricity')){
                    var pageTitle = $('#MyPageTitle');
                    var editor = pageTitle.data('object').editor;
                    editor.handleMessages([{message:'Sorry! Electric, Indexed, Single Bill deals are not allowed when an LDC is Rate Ready.', severity:'ERROR'}]);
                    reverseUpdate(updateResult);
       }
   }
        });
});
})(skuid);
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
I'm not sure what's causing it to run forever --- possibly the "Pricing_Product__c" Reference field is being rendered as a Picklist somewhere on your page, so whenever you "fix" it, Skuid may be going and refetching data for the field, which would cause updateRow to be called again with new related data (e.g. for Pricing_Product__r.Name) --- but this is just an idea.

In either case, you could probably stop the infinite loop by setting a static flag during the first loop iteration, and then only running your logic if the static flag has not been set yet.

e.g. in simplified code:


var isFirstTime = true;

if (isFirstTime) {
   // do my logic that calls updateRow()
   isFirstTime = false;
} else {
   // do NOT call updateRow() again
}
Photo of Moshe Karmel

Moshe Karmel, Champion

  • 8,646 Points 5k badge 2x thumb
Zach, you are right that I do have the reference field rendered as a picklist. It seems like the update to the reference field causes an infinite loop regardless. I tried putting in a blocker but that doesn't seem to help... 
Photo of Zach McElrath

Zach McElrath, Employee

  • 49,056 Points 20k badge 2x thumb
Did you put the blocker in here?

}else if(updateResult.updates.Pricing_Product__c){
           var fixed = skuid.$M('Fixed');
           var fixedRow = fixed.getFirstRow();
           //could this be causing everything to break because it's an update to a reference field?
           if (isFirstTime) {
              isFirstTime = false;
              model.updateRow(updateResult.row,'Pricing_Product__c',fixedRow.Id,{
                 initiatorId: updateResult.initiatorId
              });
           }
       }
Photo of Moshe Karmel

Moshe Karmel, Champion

  • 8,646 Points 5k badge 2x thumb
Sorry Zach I put it in the wrong place, thanks for the help.