- 01 Nov 2022
- 6 Minutes to read
Run a script action
- Updated on 01 Nov 2022
- 6 Minutes to read
When building an automation, you may choose the "Run a script" action. When an automation containing one of these actions runs successfully, it will allow you to trigger a script based on record changes, a form submission, using the Airtable API, and more.
An automation's scripting action runs a script in the background of the base. This differs from the Scripting extension, which runs a script in the foreground. For scripts that you would rather run manually, the Scripting extension is the ideal tool. For scripts that you would like to run automatically, setting up a scripting automation is the best choice.
What plans is the Run a script action available on?
This action is only available on the Pro and Enterprise plans. It is not available on the Pro Trial, Plus, Free, Creator, or Student plans.
Does the Run a script action support interactive input/output?
Scripting action does not support interactive input and output. Unlike scripting extension, scripting action does not have the ability to display a form or pop up a message.
Are the input and output APIs available via the Run a script action?
output APIs from the scripting extension are not currently available.
Automations scripts have different, simpler input/output APIs. The input/output APIs in the "Run script" action can not intake user input, but they can input variables for your script to use, and output data from your script for later steps in your API.
output API can currently return a maximum of 6MB of data.
What limits is the Run a script action subject to?
- Scripts need to finish in under 30 seconds
- API calls timeout at 12 seconds
- Scripts can use 512mb of memory
- Scripts can make 50 fetch requests and 30 selectRecords queries.
- Scripts can make up to 15 mutations a second. A mutation is creating, updating, or deleting records. Each mutation can change up to 50 records at once.
Need help gathering your ideas? Consider these options:
- Explore the "Example Script Showcase " in the Airtable Universe
- Visit the Airtable community scripting page
- Check out our Scripting documentation for examples of Scripts.
To illustrate how to use the Run a script action, let's walk through an example scenario. In this example, we will create a simple script that will add the value of two fields together with the result of the calculation appearing in a third field.
To get started, go to your desired Airtable base. Open the automations panel and create an automation. Then, choose a trigger, such as "When a record matches conditions".
In our example, the "Calculate?" field being checked will trigger our automation.
Setting up input variables
After testing the trigger successfully, we can move on to setting up the action. Click the blue "+ Add Action" button. Then, click on the "Run a script" option. This will bring up a window for editing your script's code. We'll start by setting up our input variables.
Input variables let you bring information from triggers and previous actions into your Run a script action. In this example, we're going to bring our "First Number" and "Second Number" fields into the script so that we can calculate their sum.
TIPGive careful consideration to how you name your script variables. As a rule of thumb, variables should be clear, descriptive, and distinct.
After we've named our variables and added them in the Input Variables sidebar, we need to declare those variables in our script. We do this by creating an
inputConfig variable with the
input.config() object, after which we can declare each of our desired variables in the manner shown below:
// Set up input variables let inputConfig = input.config(); let firstNumber = inputConfig.firstNumber; let secondNumber = inputConfig.secondNumber;
Copy and paste the code above into your script editor before proceeding.
Setting up your script
Now we're going to add the code below to the main body of our script.
//Perform the calculation let calculation = firstNumber + secondNumber;
For more information on internal Airtable classes that can be accessed via your script—check out our API reference documentation.
Copy the code above into the console below the input variables we declared above.
Setting up output variables
Finally, we're going to set up an output variable. Output variables let us pass data from the Run a script action to any remaining actions in the automation run. In this example, we're going to pass our
calculation variable to an Update record action. We do this by using the
output.set() method to declare any variables we want to use outside of this Run a script action.
// Set the output variable output.set('calculationOutput', calculation);`
Copy the code above into the console below the rest of the script above, then click the blue test button to make sure your code does not have any errors.
As a final step, we'll add one more action to our automation: an Update record action that updates "Addition" field of the triggering record with the
calculationOutput from our Run a script action.
Once you've successfully tested the Update record action, you can proceed with toggling on the automation in the upper right-hand corner of the automations console.
Starting on January 25th, 2022, Airtable made a change to URL formatting in Automation triggers and actions. Now, the base ID (appXXXXXXXXXXXXXX) will appear at the start of the URL path. This change coincides with the Understanding Airtable IDs throughout the rest of product. The change in record URL format will occur in these places:
- The url property of a record output by a trigger/action.
- The url property of a Table or View model retrieved by the Run Script action.
This may be a breaking change if your automation makes hard assumptions about the format of a URL property (e.g. by deconstructing the returned URL into its component parts or similar). No action should be needed if your automation only uses the URL for navigational purposes. Browser navigation will continue to work for the old-style Airtable URLs via redirects, although these redirects may not be supported indefinitely in the future. If your automation constructs Airtable URLs from scratch, we recommend updating the construction logic to also include the base ID.
How is the Automations scripting action different from the Scripting extension ?
Scripting extension scripts run in the foreground of the base. They’re triggered directly by a user, who is then able to see any output from the script and add any additional input the script asks for. They’re ideal for automating workflows that require some human involvement.
Automations scripting action scripts run in the background of the base. They’re triggered indirectly and run automatically behind the scenes while the user keeps working. They’re ideal as an escape hatch for when our other automation actions don’t support something, or for advanced automation use-cases.
Will the script run even if my base is not open?
Yes. Once enabled, the script will run when a record enters the view, regardless of whether the base is open. This allows you to schedule when a script runs, or trigger a script based on a form submission or by using the Airtable API to create or edit a record.
What APIs can I access using the Automations scripting action?
You can access any API – post a tweet on Twitter, or use the Airtable API to make a change in another Airtable base. Unlike the scripting extension, there is no core origin resources sharing (CORS) limitation.
Can I set up a script to run on a schedule using Automations?
Yes, it is possible to set a script to run on a schedule using the "At a scheduled time" trigger. Learn more here.