Add the following snippet to a Details view to easily copy a record
// CHANGE: the view key to your own view
$(document).on('knack-view-render.view_12', function (event, view, data) {
/* Add all of the fields you want copied and match them with detail data */
var duplicateData = {
field_57: data.field_57_raw[0].id, // If field is connection, we need to match the ID field
field_56: data.field_56_raw[0].id,
field_17: data.field_17,
field_18: data.field_18,
field_27: data.field_27,
field_28: data.field_28,
field_30: data.field_30,
field_35: data.field_35,
field_34: data.field_34,
field_31: data.field_31_raw
}
// Add a button to execute the action
$('#' + view.key).prepend('<button id="copy-button">Duplicate Record</button>');
// Add the duplicate function to the button we just added
document.getElementById('copy-button').addEventListener('click', function () {
Knack.showSpinner();
$.ajax( {
// Change object here to match your record's object #
// Replace XXX with your own Application ID and API Key found in your Knack Builder
url: 'https://api.knackhq.com/v1/objects/object_4/records/',
type: 'POST',
headers: {'X-Knack-Application-Id': 'XXX', 'X-Knack-REST-API-Key':'XXX'},
data: duplicateData,
success: function(response) {
alert('Record Copied!');
Knack.hideSpinner();
}
});
});
Just follow the example given but you would have to loop through multiple time to create multiple records. If you need professional help I can do the work for you, just write me: mark@appweaverz.com.
Hi, I am desperately looking for a similar solution to either copy multiple records or, as Bob mentioned, copy multiple records from a parent to a child object. Any ideas? Thank you
Since it appears that your field_72 can sometimes be blank, including the check statement that Mark has written above should resolve this error. This will add a check to make sure that there is data within your field_72 and if so, it will be included as a field to copy.
field_31 would be any complex field, like an address field or a date / time field. _raw will give Knack all of the properties it needs to insert the right values where needed.
Yes I have found a way.... it is fairly complex and not as easy would hope.... at least the way I have found... Could we email each other to talk more ?
Been working on a way do copy the record from the parent vs. the detail that would duplicate all the connected records along with the duplicated parent record.
I’m trying to use Nic’s example. I’m Javascript illiterate. So please bear with me.
I changed view_12 in the example to my view name, updated my url, ap id and api key.
I can’t get the button to even show on the page’s view.