Page looping on function in main.js

  • 1
  • Problem
  • Updated 4 years ago
  • Solved
I'm running the following script. Somewhere in the middle of that, it's triggering this function in main.js:


And it loops and loops over lines 19531-19534 (doesn't seem to ever reach 19535).

Here's my code:

var mA = skuid.$M('Open'),   	rA = mA.getFirstRow(),
    $ = skuid.$;
   
//Check for related caller info
$.blockUI({
  message: 'Checking for Caller other than Patient...'
});
console.log('Checking for Caller other than Patient.');
if (rA.Caller_Other_Than_Patient && rA.Caller_First_Name__c) {
   console.log('Adding Caller other than Patient as Related Person.');
   var dfd = new $.Deferred(),
        mR = skuid.$M('Related'),
       fullName = rA.Caller_First_Name__c + ' ' + rA.Caller_Last_Name__c;
   
   //Check if row for caller already exists (if so, update that row)
   var exists = false;
   $.each(mR.data, function(i,row){
       if (row.Name == fullName) {
           exists = true;
           mR.updateRow(row,{
               Relationship_to_Patient__c: rA.Relationship_to_Patient__c,
               Primary_Phone__c: rA.Caller_Primary_Phone__c,
               Primary_Phone_Type__c: rA.Caller_Primary_Phone_Type__c
           });
       }
   });
   
   //If row for caller doesn't exist, create it
   if (!exists){
       mR.createRow({
           additionalConditions:[
               {field: 'Name', value: rA.Caller_First_Name__c + ' ' + rA.Caller_Last_Name__c},
               {field: 'First_Name__c', value: rA.Caller_First_Name__c},
               {field: 'Last_Name__c', value: rA.Caller_Last_Name__c},
               {field: 'Relationship_to_Patient__c', value: rA.Relationship_to_Patient__c},
               {field: 'Primary_Phone__c', value: rA.Caller_Primary_Phone__c},
               {field: 'Primary_Phone_Type__c', value: rA.Caller_Primary_Phone_Type__c},
           ]
       });
   }
   $.when(mR.save())
        .done(function(){
            console.log('Created Related Person.');
            dfd.resolve();
        })
        .fail(function(){
            console.log('Related Person model save failed.');
            dfd.reject();
        });
    return dfd.promise();    
}
Any thoughts on what might be going on?
Photo of Matt Sones

Matt Sones, Champion

  • 31,478 Points 20k badge 2x thumb

Posted 4 years ago

  • 1
Photo of Ben Hubbard

Ben Hubbard, Employee

  • 12,490 Points 10k badge 2x thumb
main.js is javascript code that Salesforce wrote that comes down with any page that has a Salesforce header on it.  I'm not sure exactly what it's doing.  Are you getting performance issues because of it?  Or is it just something that came up in a profiler?
Photo of Matt Sones

Matt Sones, Champion

  • 31,478 Points 20k badge 2x thumb
The page is hanging up there. It never finishes the snippet that it's trying to run.
Photo of Ben Hubbard

Ben Hubbard, Employee

  • 12,490 Points 10k badge 2x thumb
What do your logs look like?  Are you getting the "Checking for caller other than patient" and "Created Related Person" items in your log?
Photo of Matt Sones

Matt Sones, Champion

  • 31,478 Points 20k badge 2x thumb
Ok, I realized that my if statement was wack. I've updated my code as follows. Now I'm getting the error "object is not a function" on skuid_SkuidJS:


Here's my console:


The code:
var mA = skuid.$M('Open'),   	rA = mA.getFirstRow(),
    $ = skuid.$;
   
//Check for related caller info
$.blockUI({
  message: 'Checking for Caller other than Patient...'
});
if (rA.Primary_Phone_Type__c == 'Caller other than patient' && rA.Caller_First_Name__c) {
    console.log('Adding Caller other than Patient as Related Person.');
    var dfd = new $.Deferred(),
        mR = skuid.$M('Related'),
        fullName = rA.Caller_First_Name__c + ' ' + rA.Caller_Last_Name__c;
    
    mR.updateData({
        callback: function(){
               //Check if row for caller already exists (if so, update that row)
               var exists = false;
               $.each(mR.data, function(i,row){
                   if (row.Name == fullName) {
                       exists = true;
                       mR.updateRow(row,{
                           Relationship_to_Patient__c: rA.Relationship_to_Patient__c,
                           Primary_Phone__c: rA.Caller_Primary_Phone__c,
                           Primary_Phone_Type__c: rA.Caller_Primary_Phone_Type__c
                       });
                   }
               });
               
               //If row for caller doesn't exist, create it
               if (!exists){
                   mR.createRow({
                       additionalConditions:[
                           {field: 'Name', value: rA.Caller_First_Name__c + ' ' + rA.Caller_Last_Name__c},
                           {field: 'First_Name__c', value: rA.Caller_First_Name__c},
                           {field: 'Last_Name__c', value: rA.Caller_Last_Name__c},
                           {field: 'Relationship_to_Patient__c', value: rA.Relationship_to_Patient__c},
                           {field: 'Primary_Phone__c', value: rA.Caller_Primary_Phone__c},
                           {field: 'Primary_Phone_Type__c', value: rA.Caller_Primary_Phone_Type__c},
                       ]
                   });
               }
               $.when(mR.save())
                    .done(function(){
                        console.log('Created Related Person.');
                        dfd.resolve();
                    })
                    .fail(function(){
                        console.log('Related Person model save failed.');
                        dfd.reject();
                    });
        }
    });
return dfd.promise();
}
Photo of Ben Hubbard

Ben Hubbard, Employee

  • 12,490 Points 10k badge 2x thumb
Unfortunately, the APIs are slightly different for for skuid.model.Model.save() and skuid.model.Model.updateData().  In save you pass in an object that has a callback property, but in updateData, you just pass in a callback function.

Right Way...

updateData(function(){
   // My Stuff
});

vs.

Wrong Way...

updateData({ callback : function(){
   // My Stuff
}});

We should probably allow the "Wrong Way" to work for updateData, but we haven't done that yet.
Photo of Matt Sones

Matt Sones, Champion

  • 31,478 Points 20k badge 2x thumb
Haha, nice. Yeah, consistency on implementation of callbacks would be nice. Same for updateRow() and createRow().

Not that I'm complaining... I should have read the documentation more carefully!
Photo of Matt Sones

Matt Sones, Champion

  • 31,478 Points 20k badge 2x thumb
Closing the loop on this. Got it working! Thanks.
Photo of Rob Hatch

Rob Hatch, Official Rep

  • 44,006 Points 20k badge 2x thumb
Glad to hear!  Thansk for letting us know.