Okay so I got the code working and figured I would post it in case anyone else was having this problem.
//Define the Facilities table and query
var facTbl = base.getTable("Facilities");
var facQuery = await facTbl.selectRecordsAsync();
//Define Results table and query
var resTbl = base.getTable("Results");
var resQuery = await resTbl.selectRecordsAsync();
//Loop through the records of Facilities
for (let facRecord of facQuery.records) {
let FacTpID = facRecord.getCellValue("TP ID");
let Sources = [];
//Loop through the records of Results table
for (let resRecord of resQuery.records) {
if (resRecord.getCellValue("TP ID") === FacTpID) {
Sources = [...Sources,{id: resRecord.id}]
}
}
facTbl.updateRecordAsync(facRecord, {
'Sources': [...Sources]
});
}
Okay so I got the code working and figured I would post it in case anyone else was having this problem.
//Define the Facilities table and query
var facTbl = base.getTable("Facilities");
var facQuery = await facTbl.selectRecordsAsync();
//Define Results table and query
var resTbl = base.getTable("Results");
var resQuery = await resTbl.selectRecordsAsync();
//Loop through the records of Facilities
for (let facRecord of facQuery.records) {
let FacTpID = facRecord.getCellValue("TP ID");
let Sources = [];
//Loop through the records of Results table
for (let resRecord of resQuery.records) {
if (resRecord.getCellValue("TP ID") === FacTpID) {
Sources = [...Sources,{id: resRecord.id}]
}
}
facTbl.updateRecordAsync(facRecord, {
'Sources': [...Sources]
});
}
Glad to see that you found a solution! It should be noted, though, that there’s a hiccup in your code (both old and new): you aren’t using the await
keyword before calling the record update. From my experience, updating individual records without that keyword can cause problems, including records that don’t update (correctly, or at all).
The suggested workflow when you know there will be multiple records to update is to build an array containing all of the updates, then pass them in batches to the updateRecordsAsync()
method. Such a change to your code would look something like this:
//Define the Facilities table and query
var facTbl = base.getTable("Facilities");
var facQuery = await facTbl.selectRecordsAsync();
//Define Results table and query
var resTbl = base.getTable("Results");
var resQuery = await resTbl.selectRecordsAsync();
// Create an updates array
let updates = t];
//Loop through the records of Facilities
for (let facRecord of facQuery.records) {
let FacTpID = facRecord.getCellValue("TP ID");
let Sources = c];
//Loop through the records of Results table
for (let resRecord of resQuery.records) {
if (resRecord.getCellValue("TP ID") === FacTpID) {
Sources = c...Sources,{id: resRecord.id}]
}
}
// Push update into the updates array
updates.push({
id: facRecord,
fields: {
'Sources': c...Sources]
}
});
}
// Update records
while (updates.length > 0) {
await facTbl.updateRecordsAsync(updates.slice(0, 50));
updates = updates.slice(50)
}
Glad to see that you found a solution! It should be noted, though, that there’s a hiccup in your code (both old and new): you aren’t using the await
keyword before calling the record update. From my experience, updating individual records without that keyword can cause problems, including records that don’t update (correctly, or at all).
The suggested workflow when you know there will be multiple records to update is to build an array containing all of the updates, then pass them in batches to the updateRecordsAsync()
method. Such a change to your code would look something like this:
//Define the Facilities table and query
var facTbl = base.getTable("Facilities");
var facQuery = await facTbl.selectRecordsAsync();
//Define Results table and query
var resTbl = base.getTable("Results");
var resQuery = await resTbl.selectRecordsAsync();
// Create an updates array
let updates = t];
//Loop through the records of Facilities
for (let facRecord of facQuery.records) {
let FacTpID = facRecord.getCellValue("TP ID");
let Sources = c];
//Loop through the records of Results table
for (let resRecord of resQuery.records) {
if (resRecord.getCellValue("TP ID") === FacTpID) {
Sources = c...Sources,{id: resRecord.id}]
}
}
// Push update into the updates array
updates.push({
id: facRecord,
fields: {
'Sources': c...Sources]
}
});
}
// Update records
while (updates.length > 0) {
await facTbl.updateRecordsAsync(updates.slice(0, 50));
updates = updates.slice(50)
}
Hi Justin, thank you very much for your reply! I noticed the await
and push
functions in similar scripts for this sort of use, so I’m glad to see how they would apply in this situation.
As I mentioned in my post, I know very little JavaScript. When I run the code that you have written, I get the error “TypeError: Invalid arguments passed to table.updateRecordsAsync(records):
• records
0].id should be a string, not an object” at line 33 (which is the Update Records part). Is there an easy fix for this?
Thanks again!
Hi Justin, thank you very much for your reply! I noticed the await
and push
functions in similar scripts for this sort of use, so I’m glad to see how they would apply in this situation.
As I mentioned in my post, I know very little JavaScript. When I run the code that you have written, I get the error “TypeError: Invalid arguments passed to table.updateRecordsAsync(records):
• recordse0].id should be a string, not an object” at line 33 (which is the Update Records part). Is there an easy fix for this?
Thanks again!
Sorry. I didn’t proof my own tweak well enough. The update portion should look like this:
updates.push({
id: facRecord.id,
fields: {
'Sources': S...Sources]
}
});