Help

Automations: Scripting Actions Error

Topic Labels: Scripting extentions
Solved
Jump to Solution
3576 5
cancel
Showing results for 
Search instead for 
Did you mean: 
Tori_Klein
5 - Automation Enthusiast
5 - Automation Enthusiast

I’ve set up a few automated scripting actions using the beta automations features but keep receiving the same error message when I try to run the automation.

image

The same script runs without issue in the scripting block, so I’m not sure how to resolve this. Any pointers? I’m also a JavaScript novice.

1 Solution

Accepted Solutions

When you open up the Script editor, after having captured a record in the trigger (a record that has either been created or that has entered a view), you can pull that same record into the script by adding it to the input variable on the left side:
CleanShot 2020-08-17 at 19.14.49

Just select + Add input variable, give it the name by which you will retrieve it, and for “Value” use the blue + button to find the Record ID of the record that triggered Step 1.

Then, you can retrieve that record from the input variable in your script, and act only on that record. So your new script might look something like this:
CleanShot 2020-08-17 at 19.33.09

Here’s a copy-pastable version of that:

let recordId = input.config().recordID

let trainingTable = base.getTable("Training | Started")
let recordQuery = await trainingTable.selectRecordsAsync()
let recordToUpdate = recordQuery.records.find(record => record.id == recordId)
let trainingEmail = recordToUpdate.getCellValue("Email")

let applicationsQuery = await base.getTable("Applications").selectRecordsAsync()
let applicationsToLink = applicationsQuery.records.map(applicationRecord => {
    if (applicationRecord.getCellValue("Email") === trainingEmail) {
        return { id: applicationRecord.id }
    }
})

await trainingTable.updateRecordAsync(recordId, {
    "Applications record": [applicationsToLink]
})

See Solution in Thread

5 Replies 5

The Scripting Block in Airtable runs the JavaScript code in your browser, using your system resources (processor / ram) to do the processing. This is why the Scripting Block requires Airtable to be open and running on your machine to be used. Because of this, Airtable doesn’t care as much how big your scripts are or how much processing time they take up. It doesn’t cost them anything if your Scripting Block script is running for 5 minutes and hogging up tons of system resources.

The Scripting Action in the Automations feature, however, runs on Airtable’s server resources – which is why they are capable of being run even if there are no instances of your Airtable base open at the time.This costs Airtable money in server resources every time your script runs. Thus, understandably, they put a cap on how much processor time your Scripting Action scripts can use, and that cap is 1000ms.

Depending on what you are trying to do with your script, and just how far over that 1000ms the current script extends, you may have a couple options for approaching this.

  1. Try to improve the script’s efficiency

    • I’d go this route first. Being that you are a novice, it’s very possible, if not likely, that there are aspects of your script which could be written to process more efficiently, and use less CPU time to do the same job. I’m not being condescending as I’m in the same boat – I am frequently finding or being shown ways that my JavaScript can be made more efficient
    • Things to look out for: nested loops; operations on one record at a time out of a collection that could be grouped together into a single operation; repetitive queries; queries inside of loops
  2. Break the operations performed by your script up into 2 or more separate automations that use the same trigger

    • If it’s not possible to improve efficiency to the point that you are using less than 1000ms of CPU time, then pursue this by extracting functionality out of one automation script and into a new one
    • This will require writing a bit more code, and repetition of queries and such from one script to the next, but it may break up what you are trying to achieve enough that each separate automation script stays under the 1000ms CPU time limit
    • Be aware that Airtable also limits the number of Automations you can have active at a time (and thus, there is a hard cap to the CPU time you can consume in a single base) - I don’t know exactly what that limit is

If you need further help accomplishing either of these, post back with your script code and I’m sure I or somebody else on the forums can provide more specific guidance.

Thank you Jeremy this is very helpful! This is the script code - I created it based on a video I found in this thread on creating a script for vlookups / linked records.

image

I want to automatically link records in two different tables using the records’ Email address as the reference point. Very possible that it’s not the most efficient script but I don’t think with my knowledge I’ll be able to identify efficiencies. If you or anyone else has advice, it would be much appreciated.

The reason you are putting this into an Automation is because you are wanting this linking of a record to a sibling record in another table to happen automatically for each record, as the record is created/updated, right?

Automations are designed with the intention of acting on one record at a time, and so I’m going to assume that what I stated above is the case.

If that is the case, then you do not need to fetch all the records from your main table and loop through them, establishing the link for each one — they have already been linked, for the most part, so you are redoing a bunch of work that has already been done.

You are wanting, here in this Automation, to link just the record that triggered the Automation. And you already have access to that record, because it was passed into the Automation actions. You can access it in your script by passing it through the input variable, and retrieving it from input.config(). Then you can directly update just the one record, without having to loop through all the records in your main table.

I’m on my phone right now so I can’t provide much in the way of further explanation, but I’ll try to hop on my computer later tonight and expand more on this.

When you open up the Script editor, after having captured a record in the trigger (a record that has either been created or that has entered a view), you can pull that same record into the script by adding it to the input variable on the left side:
CleanShot 2020-08-17 at 19.14.49

Just select + Add input variable, give it the name by which you will retrieve it, and for “Value” use the blue + button to find the Record ID of the record that triggered Step 1.

Then, you can retrieve that record from the input variable in your script, and act only on that record. So your new script might look something like this:
CleanShot 2020-08-17 at 19.33.09

Here’s a copy-pastable version of that:

let recordId = input.config().recordID

let trainingTable = base.getTable("Training | Started")
let recordQuery = await trainingTable.selectRecordsAsync()
let recordToUpdate = recordQuery.records.find(record => record.id == recordId)
let trainingEmail = recordToUpdate.getCellValue("Email")

let applicationsQuery = await base.getTable("Applications").selectRecordsAsync()
let applicationsToLink = applicationsQuery.records.map(applicationRecord => {
    if (applicationRecord.getCellValue("Email") === trainingEmail) {
        return { id: applicationRecord.id }
    }
})

await trainingTable.updateRecordAsync(recordId, {
    "Applications record": [applicationsToLink]
})

Thank you Jeremy! Appreciate your detailed response here - this was extremely helpful.