Help

Script to autofill a linked field when new record created

Topic Labels: Extensions
Solved
Jump to Solution
3547 4
cancel
Showing results for 
Search instead for 
Did you mean: 
Romain_Serman
5 - Automation Enthusiast
5 - Automation Enthusiast

Hi everyone! My coding skills are pretty limited :pensive: Here’s my problem:

I have 2 tables.

  • Table 1: a list of companies (records) with various fields.

  • Table 2: I use this table to perform various statistics on all the companies in table 1 through many “Count” fields.

Both tables are linked: in table 1, I have field with a “Stats” for each record. That field is linked to table 2 where I only have one record (“Stats”).

My problem: every time I create a new record in table 1, I have to populate the linked field with “Stats” to make sure my stats in table 2 are updated. It’s OK to do that manually for a few new records, but I have to enter dozens of new records a day. I am looking for a simple script that would automatically populate my linked field in table 1 with “Stats” (i.e link it to the sole record ID of the record in table 2).

I would be eternally grateful to anyone who could help me on this ! I have read lots of posts on this but I can’t find any solution that fits my single issue (and my limited coding skills). Many thanks in advance !!!

1 Solution

Accepted Solutions
Kamille_Parks
16 - Uranus
16 - Uranus

Sine you’ll only ever be linking to one record, I’d find that one record’s ID (expand the record and find it in the URL, or create a formula field with RECORD_ID()).

So if that one record’s id is “recWqRR6Sx4MXwfBU”, then your script could look like this:

const table = base.getTable("Table 1")
const query = await table.selectRecordsAsync()
const records = query.records

const singleRecordId = "recWqRR6Sx4MXwfBU"

records.forEach(x => {
    if (!x.getCellValue("Stats")) {
        table.updateRecordAsync(x.id, {
            "Stats": [{id: singleRecordId}]
        })
    }
})

See Solution in Thread

4 Replies 4
Kamille_Parks
16 - Uranus
16 - Uranus

Sine you’ll only ever be linking to one record, I’d find that one record’s ID (expand the record and find it in the URL, or create a formula field with RECORD_ID()).

So if that one record’s id is “recWqRR6Sx4MXwfBU”, then your script could look like this:

const table = base.getTable("Table 1")
const query = await table.selectRecordsAsync()
const records = query.records

const singleRecordId = "recWqRR6Sx4MXwfBU"

records.forEach(x => {
    if (!x.getCellValue("Stats")) {
        table.updateRecordAsync(x.id, {
            "Stats": [{id: singleRecordId}]
        })
    }
})
Romain_Serman
5 - Automation Enthusiast
5 - Automation Enthusiast

It works like a charm!!! Amazing! Many-many thanks for your help :pray:t2: :pray:t2:

Glad to hear it. Can you mark my first comment as the solution?

Of course! It’s done. Thanks again Kamille!