Clone record without using Clone button...

  • 1
  • Question
  • Updated 4 years ago
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);
            }
    }
    });
}
Photo of Scott

Scott

  • 2,722 Points 2k badge 2x thumb

Posted 4 years ago

  • 1
Photo of Zach McElrath

Zach McElrath, Employee

  • 49,004 Points 20k badge 2x thumb
Scott, your approach makes sense, here's a slightly more optimized approach using a multi-value variant of model.updateRow():

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

Scott

  • 2,722 Points 2k badge 2x thumb
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...
Photo of Scott

Scott

  • 2,722 Points 2k badge 2x thumb
Anyone have any thoughts?
Photo of Rob Hatch

Rob Hatch, Official Rep

  • 44,006 Points 20k badge 2x thumb
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.