Updating selected items in a callback causing Uncaught TypeError

Hello!

I’m trying to update selected rows in a table after having saved a new record in a different model - and I’m trying to do this in one fell swoop using a mass action button on the table.

I’m using a callback because I want to reference the newly created record in the updates to the rows in the tables by inserting the ID into a lookup field on the selected records, however, when I use getSelectedItems to fetch the selected rows in the table I’m getting the following error:

Uncaught TypeError: Cannot read property ‘getSelectedItems’ of undefined
I’ve tried moving the getSelectedItems section below the callback which also didn’t work. The saving of the record is working as I can console.log the ID of the saved record.

I’m sure I’m doing something daft - any ideas? Here’s the snippet with the pertinent section in bold:

var params = arguments[0], $ = skuid.$, reqBooking = skuid.model.getModel('RequiringBooking'); var newBooking = skuid.model.getModel('NewBooking'); var listBooking = skuid.model.getModel('Bookings'); newBooking.save({callback: function(result){ <b>if (result.totalsuccess) { var rowNewBooking = newBooking.getFirstRow(); $.each(arguments[0].list.getSelectedItems(),function(i,item){ reqBooking.updateRow(item.row,{ 'Booking__c': rowNewBooking.Id, 'Desired_required_or_completed__c': 'Enrolled', 'Certification_Date__c': rowNewBooking.Date_and_Time_of_Course__c, 'Location__c': rowNewBooking.Location__c }); }); </b> reqBooking.save(); reqBooking.updateData(); listBooking.updateData(); } else { console.log(result.insertResults[0]); } }});

You can probably get a good idea of what you have available to you by console logging everything.
I would log arguments[0], arguments[0].list and arguments[0].list.getSelectedItems(), just to see what you’re getting back. Also I have used the getSelectedItems function with the below syntax, it might just work…

skuid.$.map(arguments[0].list.getSelectedItems(),function(item){&nbsp;<br><b>&nbsp;reqBooking.updateRow(item.row,{<br></b><b> 'Booking__c': rowNewBooking.Id, 'Desired_required_or_completed__c': 'Enrolled', 'Certification_Date__c': rowNewBooking.Date_and_Time_of_Course__c, 'Location__c': rowNewBooking.Location__c &nbsp;<br></b><b> });</b>
});&nbsp;

The problem is with your use of arguments[0] inside the callback function — within the callback function, arguments[0] refers to the save result object, which does not have a list property like you’re looking for. You need to store a reference to the list variable at the top of your snippet so that you can safely reference it from within your save callback function:


var params = arguments[0], <br>&nbsp; &nbsp;<b>list = params.list,<br></b>&nbsp; &nbsp;reqBooking = skuid.model.getModel('RequiringBooking'),<br>&nbsp; &nbsp;newBooking = skuid.model.getModel('NewBooking'),<br>&nbsp; &nbsp;listBooking = skuid.model.getModel('Bookings'),<br>&nbsp; &nbsp;$ = skuid.$;<br>newBooking.save({callback: function(result){<br>&nbsp; if (result.totalsuccess) {<br>&nbsp; &nbsp; var rowNewBooking = newBooking.getFirstRow();<br>&nbsp; &nbsp; $.each(<b>list.getSelectedItems()</b>,function(i,item){<br>&nbsp; &nbsp; &nbsp; reqBooking.updateRow(item.row,{<br>&nbsp; &nbsp; &nbsp; &nbsp; 'Booking__c': rowNewBooking.Id,<br>&nbsp; &nbsp; &nbsp; &nbsp; 'Desired_required_or_completed__c': 'Enrolled',<br>&nbsp; &nbsp; &nbsp; &nbsp; 'Certification_Date__c': rowNewBooking.Date_and_Time_of_Course__c,<br>&nbsp; &nbsp; &nbsp; &nbsp; 'Location__c': rowNewBooking.Location__c<br>&nbsp; &nbsp; &nbsp; });<br>&nbsp; &nbsp; });<br>&nbsp; &nbsp; reqBooking.save();<br>&nbsp; &nbsp; reqBooking.updateData();<br>&nbsp; &nbsp; listBooking.updateData();<br>&nbsp; } else {<br>&nbsp; &nbsp; console.log(result.insertResults[0]);<br>&nbsp; }<br>}});





Brilliant, that makes a lot of sense. I’ve just tested it and it works just as I’d hoped.

The only other thing now is that I’m trying to update a table with the ‘Bookings’ model after the save (listBooking.updateData():wink: and this doesn’t seem to be happening - any ideas why?

(the table is beneath the popup - I should add)

You need to wait for your reqBooking model to finish saving before you call reqBooking.updateData() — and check that the save operation was a total success, otherwise your reqBooking updateData operation will fail, because you can’t call updateData on models that have unsaved changes (because then the unsaved data would get thrown out and lost).


So change these lines:

reqBooking.save();<br>reqBooking.updateData();<br>listBooking.updateData();


to something like this:

reqBooking.save({callback:function(result){<br>&nbsp; &nbsp;if (result.totalsuccess) {<br>&nbsp; &nbsp; &nbsp; skuid.model.updateData([reqBooking,listBooking]);<br>&nbsp; &nbsp;}<br>}});


The other thing I changed was to use the multi-model version of updateData, which is much more efficient, as it allows for multiple models to be requeried in a single call to the server.

Moshe, Zach - As ever, thanks for your help! I think I’ll need to use the multi-model version of updateData in a few other places too by the looks of things.

Cheers!