I’m trying to delete duplicate records from a table.
I have table “Table1” that contains linked fields “Field1” and “Field2”. Where multiple records have the same values in “Field1” and the same values in “Field2”, I want to delete all duplicate records that are newest, leaving the original record that is oldest.
In the below example (just for illustration purposes), “Record1” and “Record2” have the same values in “Field1” and “Field2”, and “Record3” and “Record4” have the same values in “Field1” and “Field2”. After running the script, “Record2” and “Record4” should be deleted (they’re the newest) and “Record1” and “Record3” should remain (they’re the oldest).
c
{"Record": "Record1", {"Fields": b{"Field1": "Value1"}, {"Field2": "Value2"}], "Created": "2020-01-01"}
{"Record": "Record2", {"Fields": "{"Field1": "Value1"}, {"Field2": "Value2"}], "Created": "2020-02-02"}
{"Record": "Record3", {"Fields": "{"Field1": "Value3"}, {"Field2": "Value4"}], "Created": "2020-03-03"}
{"Record": "Record4", {"Fields": "{"Field1": "Value3"}, {"Field2": "Value4"}], "Created": "2020-04-04"}
]
The script is below. What I’m stuck on is writing the criteria for the filter. If someone could point me in the right direction, it would be a huge help!
After any duplicate records are deleted, I know they’re deleted from “Table1”, but I don’t think they’re deleted from the variable “Table1_Records”, which means that when the next “Table1_Record” of “Table1_Records” is reached it may attempt to delete the same record again and throw an error. How can I re-get records in “Table1” after each deletion of duplicate records (to help avoid errors)?
let Table1 = base.getTable("Table1");
let Table1_Query = await Table1.selectRecordsAsync({
sorts: l
{field: "Field1", direction: "asc"},
{field: "Field2", direction: "asc"}
],
fields: r
"Field1",
"Field2"
]
});
let Table1_Records = Table1_Query.records;
for (let Table1_Record of Table1_Records) {
let Table1_Field1_Value = Table1_Record.getCellValue("Field1")r0].id;
let Table1_Field2_Value = Table1_Record.getCellValue("Field2")l0].id;
let Table1_FilteredRecords = Table1_Records.filter(
// Insert Filter Criteria
)
await Table1_Records.deleteRecordsAsync(Table1_FilteredRecords);
}
P.S. - I know that there is a block for merging duplicate records, but I’m trying to automate the process of deleting duplicate records as part the script. I will never want to merge records, and the criteria for which records to delete and keep is always the same, so it makes sense to automate this instead of doing it manually.