Copy multiple rows

Hi,

I have a table view with checkboxes following the code here: http://knack.freshdesk.com/support/solutions/articles/5000447560-javascript-code-examples#jq-checkboxes

I want the user to be able to check a few records and then have these records duplicated with a single click (not having to individually duplicate each row). Any suggestions?

Thank you,

Leeat

Leeat, Thanks for sharing. Where do we place this code?

also do you have the code to copy child record as well?

Thanks.

Hi,

If anyone ever needs this, I figured it out. Basically, this code works on a table view. When the user presses the button, he is prompted to insert a new value for one of the fields, and then all entries of the table are duplicated with this new value.

Enjoy!

var es_resource_records = {};

$(document).on('knack-records-render.view_94', function(event, view, records) {

$("#view_94").prepend("<button id='button_duplicate_panel'style='padding: 0 10px; line-height: 22px;' class='kn-button-menu'>Duplicate rows</button>");

es_resource_records = {};
es_resource_records = records;

$("#button_duplicate_panel").click(function() {
es_last_updated();
});

});


function es_last_updated() {

var newPanel = prompt("Enter Panel");

Knack.showSpinner();

var url = "https://api.knackhq.com/v1/objects/object_10/records"
headers = { "X-Knack-Application-Id": '588b8b4a8b7b111e4b612826',"X-Knack-REST-API-Key" : 'af065ef0-ea37-11e6-94af-eff314feebf5'}
var filters = [{
field: 'field_81',
operator: 'is',
value: newPanel
}]
get_url = url + '?filters=' + encodeURIComponent(JSON.stringify(filters))
//doing GET api call to get the data
$.ajax({
url: get_url,
type: 'GET',
headers: headers,
success: function (data) {
record_id = data.records[0].id

var records_updated = 0;
for (var x = 0; x < es_resource_records.length; x++) {
// es_update_field(es_resource_records[x]['id'], LAST_UPDATED_FIELD_ID, 5, function() {
es_duplicate_record(es_resource_records[x],record_id, function() {
if (++records_updated == es_resource_records.length) {
location.reload();
Knack.hideSpinner();
}
});
}
}
});
}


function es_duplicate_record(record,record_id, callback) {
var duplicateData = {
field_73: record.field_73_raw[0].id, // If field is connection, we need to match the ID field
field_74: record.field_74,
field_92: record_id,
field_111: record.field_111
}


$.ajax( {
url: 'https://api.knackhq.com/v1/objects/object_8/records/',
type: 'POST',
headers: {'X-Knack-Application-Id': '588b8b4a8b7b111e4b612826', 'X-Knack-REST-API-Key':'af065ef0-ea37-11e6-94af-eff314feebf5'},
data: duplicateData,
success: function(response) {
callback();
}
});
}

1 Like