Clone record without using Clone button...

I have a model that, when a new row is added, sets 2 default values through the model Actions.  I also need to be able to duplicate a record in the model, but I can’t use the “clone” button option because I need to do multiple things in the process.  I have a button that changes a value in the current record (deactivate), and then runs a custom snippet to add a new row and use the values from the pre-existing row (see code below).  The problem I’m seeing is that the 2 default values established in the model Actions seem to override the JS and the values I need.  Can the code below (I got it from an older post) be modified to override the defaults?  Are there other options I’m missing?


var params = arguments[0],    row = params.item.row,
    model = params.model,
    $ = skuid.$;
var newRow = model.createRow();
if (row) {
    $.each(row,function(fieldId,val) {
        if ((fieldId != ‘attributes’) && (val != null) && (fieldId != ‘Id’)) {
        var modelField = model.getField(fieldId);
            if ((typeof val === ‘object’) 
            || (modelField && modelField.createable)) {
                model.updateRow(newRow,fieldId,val);
            }
    }
    });
}

Scott, your approach makes sense, here’s a slightly more optimized approach using a multi-value variant of model.updateRow():

var params = arguments[0],&nbsp; &nbsp; <br>&nbsp; &nbsp; row = params.item.row,<br>&nbsp; &nbsp; model = params.model,<br>&nbsp; &nbsp; $ = skuid.$;<br>var newRow = model.createRow();<br>if (row) {<br>&nbsp; &nbsp; var rowUpdates = {};<br>&nbsp; &nbsp; $.each(row,function(fieldId,val) {<br>&nbsp; &nbsp; &nbsp; &nbsp; if ((fieldId != 'attributes') &amp;&amp; (val != null) &amp;&amp; (fieldId != 'Id')) {<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;var modelField = model.getField(fieldId);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if ((typeof val === 'object')&nbsp;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; || (modelField &amp;&amp; modelField.createable)) {<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; rowUpdates[fieldId] = val;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }<br>&nbsp; &nbsp; &nbsp; &nbsp; }<br>&nbsp; &nbsp; });<br>&nbsp; &nbsp; model.updateRow(newRow,rowUpdates);<br>}

Thanks for the response Zach… but it is still using the model Action defaults over the Snippet… I’m a complete novice at JS, so I have no idea how to get around this…

Anyone have any thoughts?

Scott; 
It seems to me that you should be able to accomplish what you are after without any custom Javascript. 

1. Instead of using Model Actions to prepopulate your values on “initial row creation”  you could add a global action to the table which uses actions to create the new row and add default values.  This will isolate that prepopulation to a specific action and not trigger it simply with any row creation in the model. 

2. The Clone action can also be an action -  in this case probably a row action that first changes the value in the existing row,  and then adds a new row with the default values from the current row. 

Having the two actions more explicitly separated like this will most likely help with the collisions you were experiencing.