Sample: Updating multiple records in a table view

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();
}
});
}

 

I've got this 90% working, however my update button only updates a single record in the table, and does not update all checked rows. I have followed this code presented here exactly. Did anyone else run into this issue?

This works great for updating all the records in the table, thank you!  I am wondering if there's a way to modify the code to update all the records in an object, whether present in the table or not?

 

Thanks again.

Hi, 

 

I have been trying to write a script inspired by the post but I still have issues (same as Pal, the spinner spin but nothing happen). Before I spend more time on this I would like to make sure that the records updated using this script will trigger my zap on Zapier. 

Only records updated within the app can trigger Zapier (so it does not work with knack builder updates or API updates) so I am wondering. I feel like the answer is most likely that it will not trigger Zapier :( 

 

Thanks 

 

Loic 

This works awesome!! I just want to add a little extra for those folks looking to batch update a variable, instead of a constant (and may be new to the coding experience). 

function es_last_updated() {

   //add variable here this example does a pop up box to enter information

  var checkNumber = prompt("Enter Check Number");

   Knack.showSpinner();

   

  var records_updated = 0;

  for (var x = 0; x < es_resource_records.length; x++) {  // change new Date() to the name of your var

    es_update_field(es_resource_records[x]['id'], LAST_UPDATED_FIELD_ID, checkNumber , 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>",

        "X-Knack-REST-API-Key": "<your app key>"

     // I also had to make a change here to get the call back to work, these can both be found on your API page in your app 

},

      data: data,

      success: function(response) {     

        callback();

      }

    });

  }

Thanks so much to the person who posted this!!

Are you accessing the update records via root access or page access?

It looks like page access to me.  How are you getting user authentication to root access?

If you use "chrome" or a browser that lets you see the javascript console output, I would bet you would see there are some errors happening.

My guess is you didn't update the URL re: Object_10 part of the API url?

Never mind, I figured out what I did wrong.

Now it works like a charm!

REALLY appreciate you sharing this!!

Thank you for the quick reply!

Ok, so now I've;

1. changed the field # to the one I want changed, 

2. Changed the view # 

3. changed the "data[field] = value;" to data[field] = "Exported";

4.Changed the object number 

5.Inserted my app ID.


When I press the button now, it just show the spinner, and nothing more happens. Do you see what have I done wrong / forgotten to do?


Thank you!

Hi there.

This is the field # that you want to update for a particular record.

The es_update_field function takes the field and the value you want to update into that field.


data[field] = value; 

//sets the field to the specified value.


In the example above, I'm using that function to set the value of field_111 to the current date.


Does that help?

Thanks for sharing this!

I've been looking for a solution for batch-updating a field in a table view.

Now, I don't have very much javascript knowledge, so I have a couple of questions:

- var LAST_UPDATED_FIELD_ID = "field_111";    <- is this the field that you want to be updated?

If so, where do I put the new value I want the field to be updated with?

Again, thanks for sharing.