Update after Form Submit

I need some help with updating a record after a form submit.

The following code work nicely, except it exposes the API Key. My understanding is this is an object update.

$(document).on('knack-record-create.view_1084', function(event, view, record) {

$.ajax({
url: "https://api.knackhq.com/v1/objects/object_79/records/" + record.id,
type: "PUT",
headers: {
"X-Knack-Application-Id": KnackAppID,
"X-Knack-REST-API-Key": KnackAPIKey
},
data: {
field_760 : NewDescription,
},
success: function(response) {
console.log("Done!");
alert(record.field_760_raw);
}
});

});

I have tired converting to using a View Update using the following code and it does not work.

$(document).on('knack-record-create.view_1084', function(event, view, record) {

$.ajax({
url: "https://api.knack.com/v1/pages/scene_556/views/view_1084/records/"+record.id,
type: "PUT",
headers: {
"X-Knack-Application-Id": KnackAppID,
"Authorization": Knack.getUserToken()
},
data: {
field_760 : NewDescription,
},
success: function(response) {
console.log("Done!");
alert(NewDescription);
}

});

The alert shows the correct data to be saved, however it does not make the change using the second set of code.

Thanks

Martin

Martin - looks okay to me but a few things to check:

  • Scene & views numbers are correct for your app.
  • The variable KnackAppID is correctly set (it's not set inside this code) - perhaps use the Knack function Knack.app.id instead.

Hi, I had some success in using the API without exposing my key when I was fiddling with Power query in Excel or using Power BI last year.. and I remember I had to pass a request to Knack firstly to get a user token.. and then passing a second request back to Knack after getting the token back from Knack from the first request. Not sure if that's the issue here, but hope it helps you somehow

I wrote my experience here.. Good luck anyway

https://support.knack.com/hc/en-us/community/posts/360029613471-Remote-login-request-with-Power-query-Power-BI