# Sample: Updating multiple records in a table view

**URL:** <https://forums.knack.com/t/sample-updating-multiple-records-in-a-table-view/10271>\
**Category:** API & Custom Code\
**Created:** [June 3, 2015, 12:16am UTC](https://forums.knack.com/t/sample-updating-multiple-records-in-a-table-view/10271 "2015-06-03T00:16:24Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![PJB14934](https://avatars.discourse-cdn.com/v4/letter/p/b38774/32.png) [@PJB14934](https://forums.knack.com/u/PJB14934)\
**Post date:** [June 3, 2015, 12:16am UTC](https://forums.knack.com/t/sample-updating-multiple-records-in-a-table-view/10271/1 "2015-06-03T00:16:24Z")

</div>

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. &nbsp;

Feel free to use this example or improve upon it.

&nbsp;

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

&nbsp;

---

<div class="post-metadata">

**Author:** ![ZakKatz41795](https://avatars.discourse-cdn.com/v4/letter/z/2bfe46/32.png) [@ZakKatz41795](https://forums.knack.com/u/ZakKatz41795)\
**Post date:** [September 25, 2017, 10:01pm UTC](https://forums.knack.com/t/sample-updating-multiple-records-in-a-table-view/10271/2 "2017-09-25T22:01:12Z")

</div>

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&nbsp;[here&nbsp;](https://forums.knack.com/hc/en-us/community/posts/222365967-Update-multiple-records-with-a-checkbox?page=1)exactly. Did anyone else run into this issue?

---

<div class="post-metadata">

**Author:** ![AndySeger27892](https://avatars.discourse-cdn.com/v4/letter/a/ebca7d/32.png) [@AndySeger27892](https://forums.knack.com/u/AndySeger27892)\
**Post date:** [April 14, 2017, 11:15pm UTC](https://forums.knack.com/t/sample-updating-multiple-records-in-a-table-view/10271/3 "2017-04-14T23:15:04Z")

</div>

This works great for updating all the records in the table, thank you!&nbsp; 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?

&nbsp;

Thanks again.

---

<div class="post-metadata">

**Author:** ![LoicSebe78310](https://avatars.discourse-cdn.com/v4/letter/l/9de053/32.png) [@LoicSebe78310](https://forums.knack.com/u/LoicSebe78310)\
**Post date:** [December 30, 2016, 6:13pm UTC](https://forums.knack.com/t/sample-updating-multiple-records-in-a-table-view/10271/4 "2016-12-30T18:13:57Z")

</div>

Hi,&nbsp;

&nbsp;

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.&nbsp;

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 :(&nbsp;

&nbsp;

Thanks&nbsp;

&nbsp;

Loic&nbsp;

---

<div class="post-metadata">

**Author:** ![Crystal34501](https://avatars.discourse-cdn.com/v4/letter/c/b487fb/32.png) [@Crystal34501](https://forums.knack.com/u/Crystal34501)\
**Post date:** [June 24, 2016, 4:16pm UTC](https://forums.knack.com/t/sample-updating-multiple-records-in-a-table-view/10271/5 "2016-06-24T16:16:07Z")

</div>

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).&nbsp;

function es\_last\_updated() {

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

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

&nbsp;&nbsp;&nbsp;Knack.showSpinner();

&nbsp;&nbsp;&nbsp;

&nbsp;&nbsp;var records\_updated = 0;

&nbsp;&nbsp;for (var x = 0; x \< es\_resource\_records.length; x++) { &nbsp;// change new Date() to the name of your var

&nbsp;&nbsp;&nbsp;&nbsp;es\_update\_field(es\_resource\_records[x]['id'], LAST\_UPDATED\_FIELD\_ID, checkNumber , function() {

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (++records\_updated == es\_resource\_records.length) {

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;location.reload();

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Knack.hideSpinner();

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}

&nbsp;&nbsp;&nbsp;&nbsp;});&nbsp;&nbsp;

&nbsp;&nbsp;}

}

function es\_update\_field(record, field, value, callback) {

&nbsp;&nbsp;

&nbsp;&nbsp;&nbsp;var data = {};

&nbsp;&nbsp;&nbsp;data[field] = value;

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;

&nbsp;&nbsp;&nbsp;$.ajax({

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;url: "[https://api.knackhq.com/v1/objects/object\_10/records/](https://api.knackhq.com/v1/objects/object_10/records/)"+record,

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;type: "PUT",

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;headers: {

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"X-Knack-Application-Id": "\<your app id\>",

&nbsp; &nbsp; &nbsp; &nbsp; "X-Knack-REST-API-Key": "\<your app key\>"

&nbsp; &nbsp; &nbsp;// 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&nbsp;

},

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;data: data,

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;success: function(response) {&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;callback();

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}

&nbsp;&nbsp;&nbsp;&nbsp;});

&nbsp;&nbsp;}

Thanks so much to the person who posted this!!

---

<div class="post-metadata">

**Author:** ![EQPLastics22721](https://avatars.discourse-cdn.com/v4/letter/e/c2a13f/32.png) [@EQPLastics22721](https://forums.knack.com/u/EQPLastics22721)\
**Post date:** [July 13, 2015, 5:56am UTC](https://forums.knack.com/t/sample-updating-multiple-records-in-a-table-view/10271/6 "2015-07-13T05:56:55Z")

</div>

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

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

---

<div class="post-metadata">

**Author:** ![PJB14934](https://avatars.discourse-cdn.com/v4/letter/p/b38774/32.png) [@PJB14934](https://forums.knack.com/u/PJB14934)\
**Post date:** [June 4, 2015, 9:17pm UTC](https://forums.knack.com/t/sample-updating-multiple-records-in-a-table-view/10271/7 "2015-06-04T21:17:17Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![AnneGrethe20989](https://avatars.discourse-cdn.com/v4/letter/a/7ab992/32.png) [@AnneGrethe20989](https://forums.knack.com/u/AnneGrethe20989)\
**Post date:** [June 4, 2015, 9:13pm UTC](https://forums.knack.com/t/sample-updating-multiple-records-in-a-table-view/10271/8 "2015-06-04T21:13:22Z")

</div>

Never mind, I figured out what I did wrong.

Now it works like a charm!  
  
REALLY appreciate you sharing this!!

---

<div class="post-metadata">

**Author:** ![AnneGrethe20989](https://avatars.discourse-cdn.com/v4/letter/a/7ab992/32.png) [@AnneGrethe20989](https://forums.knack.com/u/AnneGrethe20989)\
**Post date:** [June 4, 2015, 8:37pm UTC](https://forums.knack.com/t/sample-updating-multiple-records-in-a-table-view/10271/9 "2015-06-04T20:37:17Z")

</div>

Thank you for the quick reply!

Ok, so now I've;

1. changed the field # to the one I want changed,&nbsp;

2. Changed the view #&nbsp;

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

4.Changed the object number&nbsp;

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!

---

<div class="post-metadata">

**Author:** ![PJB14934](https://avatars.discourse-cdn.com/v4/letter/p/b38774/32.png) [@PJB14934](https://forums.knack.com/u/PJB14934)\
**Post date:** [June 3, 2015, 7:17pm UTC](https://forums.knack.com/t/sample-updating-multiple-records-in-a-table-view/10271/10 "2015-06-03T19:17:06Z")

</div>

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;&nbsp;

//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?

---

<div class="post-metadata">

**Author:** ![AnneGrethe20989](https://avatars.discourse-cdn.com/v4/letter/a/7ab992/32.png) [@AnneGrethe20989](https://forums.knack.com/u/AnneGrethe20989)\
**Post date:** [June 3, 2015, 5:13pm UTC](https://forums.knack.com/t/sample-updating-multiple-records-in-a-table-view/10271/11 "2015-06-03T17:13:09Z")

</div>

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&nbsp;LAST\_UPDATED\_FIELD\_ID = "field\_111"; &nbsp; &nbsp;\<- 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.
