Help

Re: Bulk Add a Linked Records to Fields that have existing linked records

4785 0
cancel
Showing results for 
Search instead for 
Did you mean: 
VictoriaPlummer
7 - App Architect
7 - App Architect

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.

Screen Shot 2020-03-25 at 4.47.42 PM

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.

Screen Recording 2020-03-25 at 04.52 PM

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.
Screen Recording 2020-03-25 at 05.04 PM

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 🚀')
11 Replies 11
PM_Yanaji
4 - Data Explorer
4 - Data Explorer

This is really helpful! :clap:

PM_Yanaji
4 - Data Explorer
4 - Data Explorer

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

Totally there’s a get.view() function in the scripting block which allows you to do this as well.

Eric_Brodeur
4 - Data Explorer
4 - Data Explorer

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.

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π

Kai_Dickas
6 - Interface Innovator
6 - Interface Innovator

Hi Victoria,

thanks so much for the code examples! As a newbie to scripts it helped me a lot with my issue.

Kai

Matt_Boudreau
5 - Automation Enthusiast
5 - Automation Enthusiast

Thanks for sharing this! It was helpful.

Patrick-Kennedy
7 - App Architect
7 - App Architect

Thank you @VictoriaPlummer - super helpful example!

Rosie_Li
4 - Data Explorer
4 - Data Explorer

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!