I have a script that takes the records in a table called "Runs" and creates a separate entry in a table called "Breakout" for each linked entry in the "Runners" field. However, this script is currently only able to run manually, and if it's run multiple times, it creates duplicate entries.
I'm looking to modify this script so it either runs automatically each time a record is inserted, or when it's run manually, it doesn't create duplicate entries.
Here is the script:
const sourcetable = base.getTable('Runs');
const desttable = base.getTable('Breakout');
const query=await sourcetable.selectRecordsAsync({fields:['Game','Runners']});
const converse=rec=>rec.getCellValue('Runners').map(x=>[rec.getCellValue('Game'),{id:x.id}])
const create=(el)=>({fields:{'Run':el[0], 'Participant':[el[1]]}})
const crt=query.records.flatMap(converse).map(create)
while (crt.length) await desttable.createRecordsAsync(crt.splice(0,50))
Any assistance would be appreciated! Thanks