snippet on mass actions works only once

  • 1
  • Question
  • Updated 1 year ago
  • Answered
  • (Edited)
I have a mass action which comprises of a series of actions, out of which one runs a javascript snippet to get the list of selected rows from the model in context and create records in another model - 'APF_onboardingProduct'.

var params = arguments[0],
$ = skuid.$,
list = arguments[0].list,
selectedItems = list.getSelectedItems();
var modelORProducts = skuid.model.getModel('APF_onboardingProduct');
if (selectedItems.length > 0){
    $.each( selectedItems, function( i, item ){
        var rowORProducts = modelORProducts.createRow();
        modelORProducts.updateRow(rowORProducts, {Product__c : item.row.Id});
    });
    modelORProducts.save({callback: function(result){
        if (result.totalsuccess){
            console.log('modelORProducts saved');
        }
        else {
            console.log('modelORProducts not saved');
        }
    }});
}

Once the snippet is executed, I have 2 other actions that requery both the models in question. Everything works beautifully until here. But when I select a few more rows and try to do the same, it does nothing.

Also, on the first instance when it works correctly it saves the model correctly but doesn't log anything that I have within the save callback. The 2nd instance when it doesn't work correctly, it logs the save callback as 'modelORProducts saved'. 

Can someone please help me with this?
Photo of Ashwat

Ashwat

  • 768 Points 500 badge 2x thumb
  • frustrated

Posted 1 year ago

  • 1
Photo of Bill McCullough

Bill McCullough, Champion

  • 12,436 Points 10k badge 2x thumb
atlantabadi,

Take a look at this post in the community-> https://community.skuid.com/skuid/topics/save-model-javascript-callback-function

Your 2 other actions may be running before the save has completed.  Try switching to the deferred promise method shown in the post I linked.

Thanks,

Bill
Photo of Ashwat

Ashwat

  • 768 Points 500 badge 2x thumb
Thanks a lot Bill. That worked like a charm. Also thanks to you I got to learn something new today.