Aug 17, 2020 02:52 PM
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.
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.
Solved! Go to Solution.
Aug 17, 2020 07:37 PM
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:
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:
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]
})
Aug 17, 2020 03:25 PM
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.
Try to improve the script’s efficiency
Break the operations performed by your script up into 2 or more separate automations that use the same trigger
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.
Aug 17, 2020 04:03 PM
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.
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.
Aug 17, 2020 05:31 PM
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.
Aug 17, 2020 07:37 PM
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:
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:
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]
})
Aug 21, 2020 02:26 PM
Thank you Jeremy! Appreciate your detailed response here - this was extremely helpful.