Aug 25, 2020 05:40 PM
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 !!!
Solved! Go to Solution.
Aug 25, 2020 08:01 PM
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}]
})
}
})
Aug 25, 2020 08:01 PM
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}]
})
}
})
Aug 26, 2020 08:36 AM
It works like a charm!!! Amazing! Many-many thanks for your help :pray:t2: :pray:t2:
Aug 26, 2020 08:47 AM
Glad to hear it. Can you mark my first comment as the solution?
Aug 26, 2020 10:26 AM
Of course! It’s done. Thanks again Kamille!