Create records in a table based on selected records in another table.

Hope this helps…

var params = arguments[0];var step = params.step;var $ = skuid.$;
var models = skuid.model.map();

var Ids = skuid.$.map(arguments[0].list.getSelectedItems(),function(item){     
return item.row.Id;
});

var addingQuotes = skuid.model.getModel(‘BrokerQuote’);
var aircraftdata = skuid.model.getModel(‘database’);

// see if you can access the row your operating on 
$.each(Ids,function(){ 
var currentId = this;
// I don’t know if this will work but it might…
//var currentRow = this.row;
//console.log(currentRow); //check it out in the console
// or maybe this 
//var actualrow = YOURMODEL.getRowById(this);
//either way you get the row, set a variable to the value of operator name 
var operatorName = YOURMODEL.getFieldValue(currentRow,‘stack__Operator__c’); 
var quoteRow = addingQuotes.createRow({ 
        additionalConditions: [
{ field: ‘stack__Aircraft__c’, value: currentId, operator: ‘=’, nameFieldValue: this.Name},

//then populate your operator name field with the right value 
{ field: ‘stack__Operator__c’, value: operatorName}

        ]
    });
});

addingQuotes.save({

    callback: function(result){
        
        if (result.totalsuccess) {

        var aircraftData = skuid.model.getModel(‘database’);
        aircraftData.updateData();
        }
    }
});

Moshe, you could have given me a fish, but instead you taught me how to fish! Thank you. Here is the final snippet, although I am still having trouble refreshing one of the models. Any idea how to do an updateData(); based on the model’s condition? (given that none of the values in any of the rows have changed)

var params = arguments[0];var step = params.step;var $ = skuid.$;
var models = skuid.model.map();

var Ids = skuid.$.map(arguments[0].list.getSelectedItems(),function(item){    
return item.row.Id;
});


var addingQuotes = skuid.model.getModel(‘BrokerQuote’);
var aircraftdata = skuid.model.getModel(‘database’);

$.each(Ids,function(){
var currentId = this;
var actualrow = aircraftdata.getRowById(this);
//console.log(actualrow);
var operatorId = aircraftdata.getFieldValue(actualrow,‘stack__Op_Name__c’);
//console.log(operatorId);
var quoteRow = addingQuotes.createRow({
        additionalConditions: [
{ field: ‘stack__Aircraft__c’, value: currentId, operator: ‘=’, nameFieldValue: this.Name},
{ field: ‘stack__Operator__c’, value: operatorId, operator: ‘=’ }

        ]
    });
});

addingQuotes.save();
console.log(‘saving quotes’);
aircraftdata.updateData();
console.log(‘updating aircraft model’);


//The statement: aircraftdata.UpdateData(); actually works, but doesn’t change the rows which are rendered on the page. I can only think this is because none of the ‘values’ in any of these rows have changed. However the selected rows should be removed based on a condition which has been set on the model.

Any idea how to do an updateData(); based on the model’s condition?



You might be having the issue because update Data happens before the save finishes. To fix that replace this section:

addingQuotes.save();
console.log(‘saving quotes’);
aircraftdata.updateData();
console.log(‘updating aircraft model’);

With this…

addingQuotes.save({callback:function(result){
if(result.totalsuccess){
aircraftdata.updateData();
}
}});

This causes the updateData call to wait until the save has finished.

Still not working unfortunately. I thought perhaps maybe the save wasn’t a ‘totalSuccess’ but the console is still saying it’s successful and updating the table:

addingQuotes.save({callback:function(result){
if(result.totalsuccess){
aircraftdata.updateData();
    console.log(‘updated aircraft list’);
} else { console.log(‘not working’) }
}});


Could it be related to the condition on the model I’m trying to refresh?:






You might have to update brokerQuote together with database. Replace 

aircraftdata.updateData();

with…

skuid.model.updateData([aircraftdata,addingQuotes]);

same result unfortunately. Could it have something to do with the model sitting in a nested tab?


I’ve had a previous issue with a page include component which required a different jquery function, but the models in this case are all inside the same page.

I’ve actually had problems before with update Data on a model condition that uses the in operator. I think this might be a bug, here is a thread with a similar situation I had.

https://community.skuid.com/t/in-operator-bug-in-model-condition

Hi Greg, Try one quick thing.  Do what Moshe said about combining the two models into one updateData call, but switch the order of the models.

skuid.model.updateData([addingQuotes,aircraftdata]);

I’m not 100% sure this will fix your issue, but it’s worth a try.  Order is important in updateData calls, just as it is important when you’re building out your models in the page composer.  You need the source model to be before the dependent model in the list.

Thanks Ben & Moshe, I’ve tried both of those (switching order + updating the parent model) but still nothing :frowning:

the current updateData(); statement I have looks like this:

addingQuotes.save({callback:function(result){
if(result.totalsuccess){
//skuid.model.updateData([ParentModel,aircraftdata,addingQuotes,]);
skuid.model.updateData([ParentModel,addingQuotes,aircraftdata]);
//skuid.model.updateData([addingQuotes,aircraftdata,]);
//skuid.model.updateData([aircraftdata,addingQuotes]);
//skuid.model.updateData([skuid.model.getModel(‘BrokerQuote’),aircraftdata]);
//aircraftdata.updateData();
    console.log(‘updated aircraft list’);
} else { console.log(‘not working’) }
}});


Could this creation of rows in another model be done if the ‘aircraftdata’ list of records was done as a page include component rather than another model on the existing page?

Greg, Would you mind if I took a look in your org to confirm if this is a bug, or something else is going on?  Can you grant Skuidify LLC access to your org and let us know the org Id and page name?  You can email the details to support@skuidify.com

Done. Thanks Bena.

*Ben !

hah, I can go by Bena too. :slight_smile:

I took a look at your org.  I was able to fix the issue by changing the condition on your “database” model from a “Field from another model” condition to a “Result of SubQuery” condition.  

Check out the “CharterOpTEST_Support_Clone” page I made in your org.

I think there’s still a bug with “field from another model” type conditions that are requeried after the initial page load.  I’ll try to replicate that issue in our dev org and see if we can get that fixed.

Ben thats fantastic. Thank you very much. I’m still getting my head around the SubQuery condition on models and wouldn’t have thought of that. It would be good to see some more tutorials on this condition as I don’t recall seeing many in my travels.

Thanks also to Moshe. You guys are awesome.