getFirstRow() returns 'undefined' after row created and model saved

  • 1
  • Problem
  • Updated 4 years ago
  • Solved
I start with an empty model. I create a row. I save the model. Then when I use getFirstRow(), it returns undefined. It looks like mC.data.length = 0, even after I've created a row and saved. What am I doing wrong here?

I tried re-setting the value for the model variable (mC) after the save, but it still shows data.length = 0.

Here's my code:

// Schedule -- new case  
  var //mP = skuid.$M('Patient'),
    mC = skuid.$M('Case'),
  mA = skuid.$M('Open'),
  rA = mA.getFirstRow(),
  $ = skuid.$,
  dfd = new $.Deferred();
  
  //Create Patient Case
  $.blockUI({
     message: 'Creating Patient Case...'
  });
  
  mC.createRow({
  additionalConditions:[
  {field: 'Case_Type__c', value: rA.Case_Type__c},
  {field: 'LMP__c', value: rA.LMP__c},
  {field: 'Preliminary_AVR__c', value: rA.Preliminary_AVR__c},
  {field: 'Payment_Option__c', value: rA.Payment_Option__c},
  {field: 'Permission_to_Call__c', value: rA.Permission_to_Call__c},
  {field: 'Permission_to_Text__c', value: rA.Permission_to_Text__c},
  {field: 'Permission_to_Leave_Message__c', value: rA.Permission_to_Leave_Message__c},
  {field: 'Referral_Source__c', value: rA.Referral_Source__c}
  ]
  });
  
  //Save Case model
  $.when(mC.save())
        .done(function(){
            //Set Interaction Patient_Case__c to case just created, and query Interaction model
            mC = skuid.$M('Case');
            var rC = mC.getFirstRow(),
                mI = skuid.$M('Interaction');
            mI.setCondition(mI.getConditionByName('CaseID'), rC.Id);
            $.when(mI.updateData())
                .done(function(){
                    dfd.resolve();
                })
                .fail(function(){
                    console.log('Interaction model query failed.');
                    dfd.reject();
                });
            dfd.resolve();
        })
        .fail(function(){
            console.log('Case model save failed.');
            dfd.reject();
        });
    return dfd.promise();
Photo of Matt Sones

Matt Sones, Champion

  • 31,478 Points 20k badge 2x thumb

Posted 4 years ago

  • 1
Photo of Irvin Waldman

Irvin Waldman, Champion

  • 9,006 Points 5k badge 2x thumb
Hi Matt,

I had a similar issue and using this pattern worked for me:


PricebookEntriesModel.save({            
       callback: function (results) {
                if (results.totalsuccess) {
                    console.log('Created PricebookEntry');
                    pbe = PricebookEntriesModel.getFirstRow();
                    createLineItem(pbe);
                } else {
                    console.log('Error creating PricebookEntry');
                    deferred.reject();
                }
            }
        });
Photo of Matt Sones

Matt Sones, Champion

  • 31,478 Points 20k badge 2x thumb
Thanks, Irvin. I'm testing the callback method out. I'll let you know.
Photo of Matt Sones

Matt Sones, Champion

  • 31,478 Points 20k badge 2x thumb
Seems to be working! Thanks, Irvin.

Not sure why the $.when methodology didn't work. Seems like it should accomplish the same thing.
Photo of Irvin Waldman

Irvin Waldman, Champion

  • 9,006 Points 5k badge 2x thumb
Hey Matt,


I agree that $.when would be the ideal pattern. Seems just not implemented yet.


Call it fate or happenstance, but I just happened to have struggled with the same issue hours before your post and followed one of Pat's replies to another post. What a community!


Best,

Irvin
(Edited)
Photo of Matt Sones

Matt Sones, Champion

  • 31,478 Points 20k badge 2x thumb
Nice. I've never seen a software support community as effective as this one.
Photo of Ben Hubbard

Ben Hubbard, Employee

  • 12,490 Points 10k badge 2x thumb
Using deferreds and promises should work.  I'll have to do a bit of research to see what is going on here.