Hi Airtable Community,
Hoping you can assist.
I'm having trouble merging duplicate records, (Keeping the original record and updating specified fields from the new record, then deleting the new record once complete).
I have no java script knowledge whatsoever so used ChatGPT to come up with a script. The first script deleted both records, the second updated but kept both records and the third script is completely useless!
Essentially, I would like the automation to be able to find a duplicate address (Field Name is Property Address and is linked with another table) I have also
The automation I have set up looks like this
and the script is as follows
let table = base.getTable("Sold Stock");
let query = await table.selectRecordsAsync();
let duplicateMap = new Map();
// Find duplicates and collect their IDs
for (let record of query.records) {
let propertyAddress = record.getCellValue("Property Address");
if (duplicateMap.has(propertyAddress)) {
duplicateMap.get(propertyAddress).push(record);
} else {
duplicateMap.set(propertyAddress, [record]);
}
}
// Merge duplicates and update fields
for (let [address, duplicates] of duplicateMap.entries()) {
if (duplicates.length > 1) {
let masterRecord = duplicates[0];
let deleteRecords = duplicates.slice(1);
for (let deleteRecord of deleteRecords) {
await table.updateRecordAsync(masterRecord, {
"Property Status": deleteRecord.getCellValue("Property Status"),
"Display Online As": deleteRecord.getCellValue("Display Online As"),
"Disclose Price": deleteRecord.getCellValue("Disclose Price"),
"Cancel Auction Day Services": deleteRecord.getCellValue("Cancel Auction Day Services"),
"Is The Reserve Price On The Authority": deleteRecord.getCellValue("Is The Reserve Price On The Authority"),
"Vendors Reserve Price": deleteRecord.getCellValue("Vendors Reserve Price"),
"Holding Deposit Paid": deleteRecord.getCellValue("Holding Deposit Paid"),
"Holding Deposit Amount": deleteRecord.getCellValue("Holding Deposit Amount"),
"Name of Depositor": deleteRecord.getCellValue("Name of Depositor"),
"Important Info To Note": deleteRecord.getCellValue("Important Info To Note")
});
await table.deleteRecordAsync(deleteRecord);
}
}
}
Any help would be greatly appreciated.
Thanks,