I searched and couldn’t find a simple answer here or in KTL. I would like users to be able to make a copy of a single record at a time, and include pre-defined fields to be replicated into the new record. Is there a simply way to do this? I’ve tried using some API/js code I found here, but Claude has me going in circles trying to get it to work error-free.
Here’s what I’ve got so far, but I keep getting CORS errors trying to run it:
$(document).on(‘knack-view-render.view_634’, function(event, view, data) {
// Add a duplicate button to the view header
var $button = $(‘Duplicate Record’);
$(‘#view_634 .view-header’).append($button);
$button.on(‘click’, function() {
var recordId = data.id; // The current record’s ID
// Fields you want to copy — map field keys to their values
var newRecord = {
field_440: data.field_440_raw || data.field_440,
field_441: data.field_441_raw || data.field_441,
field_1497: data.field_1497_raw || data.field_1497
// Add/remove fields as needed
};
// POST the new record to your object
$.ajax({
url: Knack.api_url + '/v1/objects/object_35/records',
type: 'POST',
headers: {
‘X-Knack-Application-Id’: Knack.application_id,
‘X-Knack-REST-API-Key’: ‘knack’,
‘Authorization’: Knack.getUserToken()
// ← Remove Content-Type header entirely
},
// Use contentType and processData instead
contentType: 'application/json',
data: JSON.stringify(newRecord),
success: function(response) {
alert('Record duplicated! New record ID: ' + response.id);
},
error: function(xhr, status, err) {
console.error('Error:', xhr.responseText);
}
});
});
});


