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

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 🚀')
3 Likes

This is really helpful! :clap:

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.

1 Like

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π

1 Like

Hi Victoria,

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

Kai

1 Like