kuovonne wrote:
Adam, I like seeing other people’s code. Your code is very similar to how I would do it, with a some exception. (1) I would batch the record updates and record creation. (2) I would use the await keyword when updating records (but this may be a typo).
You’re totally right about the batch updating and creation, as well as the missing await (ha, I just completely missed that sigh)
(Edited to add: with reference to kuovonne’s statement below, the if
statements were removed as they were not needed!)
Here’s the new version, and the base has been updated as well:
let table = base.getTable('Table 1')
let newData = [
{
title: 'Title 4',
date: 'Date 4',
uniqueID: 'unique4'
},
{
title: 'New Title 1',
date: 'New Date 1',
uniqueID: 'unique1'
}
]
let query = await table.selectRecordsAsync({
fields:['Unique ID']
})
let existingUniqueIDs = new Object
for (let record of query.records){
let uniqueID = record.getCellValue('Unique ID')
existingUniqueIDs[uniqueID] = record.id
}
let recordsToCreate = new Array
let recordsToUpdate = new Array
for (let data of newData){
if(existingUniqueIDs[data.uniqueID]){
recordsToUpdate.push({
id: existingUniqueIDs[data.uniqueID],
fields: {
"Title": data.title,
"Date": data.date
}
})
}
else{
recordsToCreate.push({
fields:{
"Title": data.title,
"Date": data.date,
"Unique ID": data.uniqueID
}
})
}
}
while (recordsToCreate.length > 0) {
await table.createRecordsAsync(recordsToCreate.slice(0, 50));
recordsToCreate = recordsToCreate.slice(50);
}
while (recordsToUpdate.length > 0) {
await table.updateRecordsAsync(recordsToUpdate.slice(0, 50));
recordsToUpdate = recordsToUpdate.slice(50);
}