Given the recent addition of user level controls in the API I wanted to share a new script I wrote that demonstrates batch updating a value in a table on a page.
Feel free to use this example or improve upon it.
var LAST_UPDATED_FIELD_ID = "field_111"; var es_resource_records = {};$(document).on(‘knack-records-render.view_10’, function(event, view, records) {
$("#view_10").prepend("<button id='button_mark_reviewed’style=‘padding: 0 10px; line-height: 22px;’ class=‘kn-button-menu’>Mark as Reviewed</button>");
es_resource_records = {};
es_resource_records = records;$("#button_mark_reviewed").click(function() {
es_last_updated();
});});
function es_last_updated() {
Knack.showSpinner();
var records_updated = 0;
for (var x = 0; x < es_resource_records.length; x++) {
es_update_field(es_resource_records[‘id’], LAST_UPDATED_FIELD_ID, new Date().toLocaleDateString(), function() {
if (++records_updated == es_resource_records.length) {
location.reload();
Knack.hideSpinner();
}
});
}
}function es_update_field(record, field, value, callback) {
var data = {};
data[field] = value;$.ajax({
url: “https://api.knackhq.com/v1/objects/object_10/records/”+record,
type: “PUT”,
headers: {
“X-Knack-Application-Id”: “<your app id>”,
“Authorization”: Knack.getUserToken()
},
data: data,
success: function(response) {
callback();
}
});
}