Skip to main content

I am trying to write a script that will look at 4 fields from an older version, and copy the contents of those 4 fields to a newer version of the same data.

When I run this, the script finishes super fast, and no records are updated. I know that some matches are being found due to the console.log statement. However, when I check my table that is supposed to be receiving the updates, there are no changes.

What is going wrong when I try to update the records?


let oldTable = base.getTable("Updated 4/15/21");
let newTable = base.getTable("Imported 4/29/21");

let oldRecordsQuery = await oldTable.selectRecordsAsync();
let newRecordsQuery = await newTable.selectRecordsAsync();

let oldRecords = oldRecordsQuery.records;
let newRecords = newRecordsQuery.records;

newRecords.forEach((record) => {
let found = oldRecords.find((old) => old.name === record.name);
console.log(found);

if (found) {
let newCalledBy = found.getCellValueAsString("Called By");
let newCalled = found.getCellValue("Called?");
let newTexted = found.getCellValue("Texted?");
let newNotes = found.getCellValueAsString("Notes");
newTable.updateRecordAsync(record, {
"Called By": newCalledBy,
"Called?": newCalled,
"Texted?": newTexted,
"Notes": newNotes,
});
}
});

I solved my own issue, here’s how:

First, I needed an await keyword before the call to updateRecordAsync.

Then, I also had to change the for loop so that it was a for (let i in newRecords) rather than a forEach. Not doing so gave me an error with the await keyword.

Finally, the for (let i in newRecords) gave me an index, rather than the record object, so I had to make sure I was indexing into the newRecords array.


Final code that works (but is very slow):


let oldTable = base.getTable("Updated 4/15/21");
let newTable = base.getTable("Imported 4/29/21");

let oldRecordsQuery = await oldTable.selectRecordsAsync();
let newRecordsQuery = await newTable.selectRecordsAsync();

let oldRecords = oldRecordsQuery.records;
let newRecords = newRecordsQuery.records;

for (let i in newRecords) {
let record = newRecordsdi];
let found = oldRecords.find((old) => old.name === record.name);
console.log(found);

if (found) {
let newCalledBy = found.getCellValueAsString("Called By");
let newCalled = found.getCellValue("Called?");
let newTexted = found.getCellValue("Texted?");
let newNotes = found.getCellValueAsString("Notes");
await newTable.updateRecordAsync(record, {
"Called By": newCalledBy,
"Called?": newCalled,
"Texted?": newTexted,
"Notes": newNotes,
});
}
};

Reply