Help

Re: Updating linked records with script

Solved
Jump to Solution
1824 1
cancel
Showing results for 
Search instead for 
Did you mean: 
Ben_Jackson
4 - Data Explorer
4 - Data Explorer

Hi all,

My scripting ability is pretty basic, so bear with me.

I’m trying to automatically update linked records based whenever a record in a table is updated. Basically as follows.

We have a table called “Jobs in Progress”, and each record in that table has separate records attached from time to time in another table called “Tasks”. It isn’t uncommon to have several records in “tasks” linked to one record in “jobs in progress”. Records in “Jobs in progress” have a checkbox called “completed” which is either manually or automatically selected depending on several conditions. Records in tasks also have a checkbox called “completed”, which are usually manually checked.

What I would like to achieve is that if any record in “Jobs in progress” has the “completed” checkbox checked (true), that any linked records in the “Tasks” table with the “completed” checkbox unchecked, update so that they are checked, or true as well. The reason for this is that on some simple tasks we overtake the process that would normally lead to us manually checking these checkboxes, and it would be helpful to avoid having to manually go back through the “tasks” table and check the “completed” checkbox.

Any suggestions or examples would be greatly appreciated.

Thanks in advance.

1 Solution

Accepted Solutions
Justin_Barrett
18 - Pluto
18 - Pluto

Welcome to the community, @Ben_Jackson! :grinning_face_with_big_eyes: This can be done via an automation that runs a script action (your base will need to be in a Pro plan workspace). Set up the automation using the “When record matches conditions” trigger, with the trigger being a check in the {Completed} field in the [Jobs in Progress] table.

The script action will need the triggering record’s ID as an input variable:

Screen Shot 2020-11-21 at 5.37.23 PM

Here’s the script:

// Setup
const config = input.config()
const jobsQuery = await base.getTable("Jobs in Progress").selectRecordsAsync()
const tasksTable = base.getTable("Tasks")
const taskRecords = await tasksTable.selectRecordsAsync()
const checkedJob = jobsQuery.getRecord(config.recordID)

// Collect linked tasks and find those that aren't checked
let tasks = checkedJob.getCellValue("Tasks")
let uncheckedTasks = tasks.filter(linkObj => !taskRecords.getRecord(linkObj.id).getCellValue("Completed"))

// Build array of updates
let updates = uncheckedTasks.map(linkObj => {
    return {
        id: linkObj.id,
        fields: {
            "Completed": true
        }
    }
})

// Update task records
while (updates.length > 0) {
    await tasksTable.updateRecordsAsync(updates.slice(0, 50))
    updates = updates.slice(50)
}

auto-complete tasks

See Solution in Thread

6 Replies 6
Justin_Barrett
18 - Pluto
18 - Pluto

Welcome to the community, @Ben_Jackson! :grinning_face_with_big_eyes: This can be done via an automation that runs a script action (your base will need to be in a Pro plan workspace). Set up the automation using the “When record matches conditions” trigger, with the trigger being a check in the {Completed} field in the [Jobs in Progress] table.

The script action will need the triggering record’s ID as an input variable:

Screen Shot 2020-11-21 at 5.37.23 PM

Here’s the script:

// Setup
const config = input.config()
const jobsQuery = await base.getTable("Jobs in Progress").selectRecordsAsync()
const tasksTable = base.getTable("Tasks")
const taskRecords = await tasksTable.selectRecordsAsync()
const checkedJob = jobsQuery.getRecord(config.recordID)

// Collect linked tasks and find those that aren't checked
let tasks = checkedJob.getCellValue("Tasks")
let uncheckedTasks = tasks.filter(linkObj => !taskRecords.getRecord(linkObj.id).getCellValue("Completed"))

// Build array of updates
let updates = uncheckedTasks.map(linkObj => {
    return {
        id: linkObj.id,
        fields: {
            "Completed": true
        }
    }
})

// Update task records
while (updates.length > 0) {
    await tasksTable.updateRecordsAsync(updates.slice(0, 50))
    updates = updates.slice(50)
}

auto-complete tasks

Ben_Jackson
4 - Data Explorer
4 - Data Explorer

Hi Justin,

Thanks a bunch, I got a quarter of the way there. I have no coding background so it was always going to be an uphill battle.

I tested it and it works a treat. Thanks heaps for your help!

Glad to know that you got the answer you were seeking! If you would, please mark my comment (the one above, not this one) as the solution to your question. This helps others who may be searching with similar questions. Thanks!

airballer86
6 - Interface Innovator
6 - Interface Innovator

@Justin_Barrett Is there any limit to the number of records that can be updated by the above automation/script combo? I have two tables, one with 23K records that I need to join.

@airballer86 The only limit would be based on how long the update takes to run. Automation scripts are only allowed to run for 30 seconds before timing out. My gut says that 23K records would not update during that timeframe.

@Justin_Barrett Your gut is correct. It takes about 15 min, and click "wait" when Chrome tries to kill the page. It does work, but not ideal.