May 27, 2020 12:14 AM
I have table with 3 fields name(lastname,firstname, email). id seems to be auto incremented in airtable.
Now am trying to update records where id is 1 with the code below but it displays error below
here is the code
I have table with 3 fields name(lastname,firstname, email). id seems to be auto incremented in airtable.
Now am trying to update records where id is 1 with the code below but it displays error below
here is the code
1.) update records where id is = 1
const first_update = base.getTableByNameIfExists('myfirst_table');
// Update cell values
//{id: record1.id, fields: {'My field name': 'updated value 1'}},
const ok = first_update.updateRecordAsync({id: '1', fields: {'lastname': 'Carrots-2','firstname': 'joy-2', 'email': 'joy-2@gmail.com'}});
if(ok){
alert('records updated successfully');
}
2.) update record where id is = 1 and email is = john@gmail.com
const first_update2= base.getTableByNameIfExists('second_table');
// Update cell values
//{id: record1.id, fields: {'My field name': 'updated value 1'}},
const ok1 = first_update2.updateRecordAsync({id: '1', 'email': 'john@gmail.com', fields: {'lastname': 'Carrots-2','firstname': 'joy-2', 'email': 'joy-2@gmail.com'}});
if(ok1){
alert('records updated successfully');
}
Error running block
entries@https://localhost:9000/__runFrame/bundle.js:13084:17
_cellValuesByFieldIdOrNameToCellValuesByFieldId@https://localhost:9000/__runFrame/bundle.js:9983:60
updateRecordsAsync$/recordsWithCellValuesByFieldId<@https://localhost:9000/__runFrame/bundle.js:9205:43
updateRecordsAsync$@https://localhost:9000/__runFrame/bundle.js:9203:56
tryCatch@https://localhost:9000/__runFrame/bundle.js:74212:40
invoke@https://localhost:9000/__runFrame/bundle.js:74441:30
defineIteratorMethods/</prototype[method]@https://localhost:9000/__runFrame/bundle.js:74264:21
tryCatch@https://localhost:9000/__runFrame/bundle.js:74212:40
invoke@https://localhost:9000/__runFrame/bundle.js:74302:28
callInvokeWithMethodAndArg/<@https://localhost:9000/__runFrame/bundle.js:74337:17
callInvokeWithMethodAndArg@https://localhost:9000/__runFrame/bundle.js:74336:16
enqueue@https://localhost:9000/__runFrame/bundle.js:74359:13
defineIteratorMethods/</prototype[method]@https://localhost:9000/__runFrame/bundle.js:74264:21
[419]</runtime</exports.async@https://localhost:9000/__runFrame/bundle.js:74386:14
updateRecordsAsync@https://localhost:9000/__runFrame/bundle.js:9199:35
updateRecordAsync$@https://localhost:9000/__runFrame/bundle.js:8986:54
tryCatch@https://localhost:9000/__runFrame/bundle.js:74212:40
invoke@https://localhost:9000/__runFrame/bundle.js:74441:30
defineIteratorMethods/</prototype[method]@https://localhost:9000/__runFrame/bundle.js:74264:21
tryCatch@https://localhost:9000/__runFrame/bundle.js:74212:40
invoke@https://localhost:9000/__runFrame/bundle.js:74302:28
callInvokeWithMethodAndArg/<@https://localhost:9000/__runFrame/bundle.js:74337:17
callInvokeWithMethodAndArg@https://localhost:9000/__runFrame/bundle.js:74336:16
enqueue@https://localhost:9000/__runFrame/bundle.js:74359:13
defineIteratorMethods/</prototype[method]@https://localhost:9000/__runFrame/bundle.js:74264:21
[419]</runtime</exports.async@https://localhost:9000/__runFrame/bundle.js:74386:14
updateRecordAsync@https://localhost:9000/__runFrame/bundle.js:8980:35
[2]<@https://localhost:9000/__runFrame/bundle.js:76:20
o@https://localhost:9000/__runFrame/bundle.js:1:265
o/<@https://localhost:9000/__runFrame/bundle.js:1:316
runBlock@https://localhost:9000/__runFrame/bundle.js:29:33
e/<@https://static.airtable.com/js/by_sha/8f4b64fb/block_frame.js:1:2329961
r@https://static.airtable.com/js/lib/regenerator-runtime.min.js:1:160
L/<@https://static.airtable.com/js/lib/regenerator-runtime.min.js:4:58
D/</a[b]@https://static.airtable.com/js/lib/regenerator-runtime.min.js:1:339
F@https://static.airtable.com/js/by_sha/8f4b64fb/block_frame.js:1:2324534
a@https://static.airtable.com/js/by_sha/8f4b64fb/block_frame.js:1:2324738
Solved! Go to Solution.
May 27, 2020 07:06 PM
.updateRecordsAsync()
, just like .deleteRecordsAsync()
takes an array of record objects with the Record ID defined. you can’t use lastname
the way you are. Proper usage is shown here. Again, I highly recommend reading through that website to be better understand how to code for custom blocks. All of these concepts are explained there.
I’ve already pointed you to this example which updates records. Follow their methodology for const updates
to .map()
a filtered record list.
You’ve already setup a variable called recordsToUpdate
which selects only the records where lastname
= “fred”. Just like your “delete records” question, there’s no point in defining the variable if you aren’t going to use it:
const updates = recordsToUpdate.map(record => ({
id: record.id,
fields: {
}
}))
Just like in the linked examples, ^ that code creates an array of objects. Finish your code with table.updateRecordsAsync(updates)
.
May 27, 2020 10:11 AM
This is a similar problem to your other post re: deleting records
The problem is that Airtable record IDs are randomly generated following the pattern rec**************
. You’ll first need to fetch the records you want to update and then modify that object before passing it into table.updateRecordAsync()
.
If this answers your question, please consider marking it as “solution”. If not, I’m happy to work with you further. Thanks!
May 27, 2020 05:32 PM
I try to update records with lastname but it throws error but if I update with id, it works. what could be the problem
Not working
// update firstname and email where lastname is fred is not working
table.updateRecordsAsync({lastname: 'fred', fields: {'firstname': 'joy-2', 'email': 'joy-2@gmail.com'}});
Working
//update firstname and email where id is is working
const first_tab= base.getTableByName('myfirst_table');
const records = useRecords(first_tab);
//alert(records);
const recordsToUpdate = records.filter(record => record.getCellValueAsString('lastname') == 'fred');
table.deleteRecordsAsync(recordsToDelete);
table.updateRecordsAsync({id: 'rec**********', fields: {'firstname': 'joy-2', 'email': 'joy-2@gmail.com'}});
May 27, 2020 07:06 PM
.updateRecordsAsync()
, just like .deleteRecordsAsync()
takes an array of record objects with the Record ID defined. you can’t use lastname
the way you are. Proper usage is shown here. Again, I highly recommend reading through that website to be better understand how to code for custom blocks. All of these concepts are explained there.
I’ve already pointed you to this example which updates records. Follow their methodology for const updates
to .map()
a filtered record list.
You’ve already setup a variable called recordsToUpdate
which selects only the records where lastname
= “fred”. Just like your “delete records” question, there’s no point in defining the variable if you aren’t going to use it:
const updates = recordsToUpdate.map(record => ({
id: record.id,
fields: {
}
}))
Just like in the linked examples, ^ that code creates an array of objects. Finish your code with table.updateRecordsAsync(updates)
.
May 28, 2020 12:45 AM
Thank you so much. its working now
May 28, 2020 09:23 AM
Hi @Kamille_Parks, I hope you are doing good. please do you have an idea on how to paginate records with airtable. here is my post
Thanks