Hey Y’all,
So sometimes I find myself in a situation where I want to do something that sounds simple: I want to link a record to multiple records at a time. Which is usually easy if there’s only one linked record present in a field, but not so much if linked records already exist. For example: Say I want to add Christmas Day to everyone’s PTO requests in the below table.
I can’t copy and paste because that will overwrite the existing records and I want to keep those. I can technically update it bulk from the PTO Requests table, but only if I have my People names in a comma separated array, which I don’t. So I’m using a script.
I’m sure you can dial this in to your needs a little bit more, but this script works by adding everyone in the People table to the visible records in the Holiday view of the PTO table. Example base here. Script here
let ptoTable = base.getTable('PTO');
let holidayView = base.getTable('PTO').getView('Holidays');
let holidayQuery = await holidayView.selectRecordsAsync();
let holidayRecords = holidayQuery.records;
let peopleTable = base.getTable('People');
let peopleQuery = await peopleTable.selectRecordsAsync();
let peopleRecords = peopleQuery.records;
let d = b]
peopleRecords.forEach(c => d.push({id: c.id}));
let updateRecords = holidayRecords.map(c=> ({id:c.id,fields:{'Person': d}}));
await ptoTable.updateRecordsAsync(updateRecords);
output.markdown('# Done
')
If you want to go ahead and choose which record you’re bulk adding, you can use the input method.
let ptoTable = base.getTable('PTO');
let holidayRecord = await input.recordAsync("Enter the Holiday you'd like to add to everyone's PTO",ptoTable)
let peopleTable = base.getTable('People');
let peopleQuery = await peopleTable.selectRecordsAsync();
let peopleRecords = peopleQuery.records;
let d = r]
peopleRecords.forEach(c => d.push({id: c.id}));
await ptoTable.updateRecordAsync(holidayRecord.id,{
'Person': d
}
);
output.markdown('# Done
')