Skip to content

Instantly share code, notes, and snippets.

@ngalluzzo
Last active October 25, 2022 18:23
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save ngalluzzo/8ca62a13fef3d2827901 to your computer and use it in GitHub Desktop.
Save ngalluzzo/8ca62a13fef3d2827901 to your computer and use it in GitHub Desktop.
Copy a Record!
// I took one of Knack's examples and tweaked it a bit to duplicate my own records since I needed this ASAP.
// This example works off of a record's detail view to access the data. I have not worked up a table solution yet.
// TODO: - Add connected records
// Change your view
$(document).on('knack-view-render.view_12', function (event, view, data) {
// Map new record fields with current record in view
var duplicateData = {
// If field is connection, we need to match the ID in order for Knack to pick it up
field_57: data.field_57_raw[0].id,
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_12").prepend("<button id='duplicate'>Duplicate Record</button>");
// Add duplicate function to button
document.getElementById('duplicate').addEventListener('click', function () {
Knack.showSpinner();
$.ajax({
// Change object here to match your record's object #
url: "https://api.knackhq.com/v1/objects/object_4/records/",
type: "POST",
headers: {"X-Knack-Application-Id": "YOUR KNACK-APPLICATION-ID", "X-Knack-REST-API-Key":"YOUR KNACK API REST KEY"},
data: duplicateData,
success: function(response) {
alert('Record Added!');
Knack.hideSpinner();
}
});
});
});
@rtrice1
Copy link

rtrice1 commented Sep 29, 2021

I'd really hesitate to put your rest api key in your code. Anyone would have access to your database this way.

What you'd want to do is pull your users permissions and use the scene/view pages to do the same.

@meneghet
Copy link

I could not figure out how to upload records with connection fields, this solved it for me, thanks dude!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment