Help

Re: Help with Script - Script to add all available records to linked record field

Solved
Jump to Solution
1999 0
cancel
Showing results for 
Search instead for 
Did you mean: 
Michael_Andrew
8 - Airtable Astronomer
8 - Airtable Astronomer

Here is my goal. I have three fields within my table as follows:

Button Field (for the purpose of running script, available in Beta)
Check Box Field (used for filtering view)
Linked Record Field

I am looking for script (or other method) to do the following:
Click button to run script
Script marks Check Box with Check
Script adds all available records from linked table in the Linked Record field.

For example, say there are 3 records in the linked table. When I click the button in the record in my table, the Linked Record field should populate with links to those three records.

Thanks!

1 Solution

Accepted Solutions
Michael_Andrew
8 - Airtable Astronomer
8 - Airtable Astronomer

I was able to get this code working:

let Table1 = base.getTable('Table 1');
let Record1 = await input.recordAsync("Select the Record",Table1)
let Table2 = base.getTable('Table 2');
let Query2 = await Table2.selectRecordsAsync();
let Records2 = Query2.records;
let d = [ ]
Records2.forEach(c => d.push({id: c.id}));
await Table1.updateRecordAsync(Record1.id,{'Linked Record Field': d});
let check = true
await Table1.updateRecordAsync(Record1.id,{'Checkbox Field': check});
output.markdown('# Done')

See Solution in Thread

4 Replies 4
Michael_Andrew
8 - Airtable Astronomer
8 - Airtable Astronomer

I was able to get this code working:

let Table1 = base.getTable('Table 1');
let Record1 = await input.recordAsync("Select the Record",Table1)
let Table2 = base.getTable('Table 2');
let Query2 = await Table2.selectRecordsAsync();
let Records2 = Query2.records;
let d = [ ]
Records2.forEach(c => d.push({id: c.id}));
await Table1.updateRecordAsync(Record1.id,{'Linked Record Field': d});
let check = true
await Table1.updateRecordAsync(Record1.id,{'Checkbox Field': check});
output.markdown('# Done')

I am new to Airtables. Where do save and run this code please and which language is it? Thanks!

Glad you were able to get it figured out, @Michael_Andrew! You could also combine those two updateRecordAsync calls into one –

await Table1.updateRecordAsync(Record1, {
    'Linked Record Field': Query2.records.map(r => ({ id: r.id })),
    'Checkbox Field': true
});

Welcome to the forums @Peter_Vollawetscher!

This code is run in the scripting block, which can be found in the blocks gallery in your base. It’s currently free to use on all plans through September 2020! Scripts are written in JavaScript, and can interact with the data in your base – you can read more on the scripting docs site.

Thanks. I know very little about programming, but can generally figure out from trial and error. I was able to work it out from other response so give rightful credit here: