Mar 25, 2020 02:06 PM
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 = []
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 = []
peopleRecords.forEach(c => d.push({id: c.id}));
await ptoTable.updateRecordAsync(holidayRecord.id,{
'Person': d
}
);
output.markdown('# Done 🚀')
Jun 22, 2020 05:12 AM
This is really helpful! :clap:
Jun 22, 2020 05:22 AM
Hi Victoria,
I have to say your scripts are really practical. :clap:
A quick question, in your second script, the result is to fetch the whole record of your table"People". I wonder if it’s possible to link a certain view of your table"People" to fetch its all records to your table"PTO"?
Cheers
Jun 25, 2020 08:58 PM
Totally there’s a get.view() function in the scripting block which allows you to do this as well.
Aug 01, 2020 01:03 PM
Thanks for posting this as I learn how to leverage scripting in Airtable. It’s a new thing for me.
I’m trying this code and Airtable tells me forEach is not a function.
Aug 02, 2020 02:21 AM
Hi @Eric_Brodeur and welcome to the community!
AIRSCRIPT - Learn to use Airtable scripts helped me a lot to start and to improve myself.
(thanks to airscript’s Author @JonathanBowen ! )
3 ways to loop chapter in particular about FOR and ForEach !
l also like this one by @VictoriaPlummer
and the Official Airtable Script Block API Docs:
Community’s Experts generously help if you share your TABLE or a TABLE that your code needs to run and your CODE here, with some screenshots to explain what you wish to do, what your code actually does.
If you are importing or exporting files (JSON, CSV, TXT…) please share short pieces of them too.
If you are importing or exporting URL, please show an example of URL to parse.
I’m not (yet ? haha) an Expert but that’s what they need to help.
oLπ
Oct 07, 2020 12:41 AM
Hi Victoria,
thanks so much for the code examples! As a newbie to scripts it helped me a lot with my issue.
Kai
Dec 31, 2020 09:49 AM
Thanks for sharing this! It was helpful.
Nov 01, 2021 09:06 PM
Thank you @VictoriaPlummer - super helpful example!
Jan 04, 2022 01:12 PM
Hi there! I came across this thread trying to figure out a way to bulk select records when adding multiple linked records. Was dismayed to find this note in AT Support:
Multiple records can be selected one at a time from the list of linked records (there isn’t a way to link multiple records at one time).
I am trying to calculate manufacturing costs with a list of parts + costs per product, grouped by product name. Parts are listed in one table and I am linking them in another table to roll up the costs and use that total to figure out retail prices. However each product has multiple parts (20-30+ in a few cases) and it is tedious to search and link each part in the linked records field.
Would this script be able to automate a bulk selection per record?
Thank you!