How to use additional scope (additionalScope) in your queries

Why use additionalScope

When writing queries you'd like to reuse with different input value(s), Retool's additionalScope might be helpful! See our docs here and some more in depth examples below.

How to use additionalScope

  1. The additionalScope option allows you to pass variables to the query that aren't already defined in the global scope (e.g. text1.value). In order to use additionalScope, you must first trigger a query in an event handler or using the .trigger() method in a JS query.

  2. You can now pass the additionalScope argument to the .trigger() method

  3. The additionalScope argument takes an object as its value. Within this object, you can add key:value pairs where the key is the name you'd like to give the variable and the value is the value of the variable.

  4. You can now use the additionalScope data within the query editor.

Common Use Cases

The most common use case I've seen for additionalScope is triggering the same query for each item in an array (e.g. a list of selected IDs), and then returning all results together. Here is a community post that walks through it.

Another use case! You might want to execute the same query with a different filter. You can have 2 separate buttons that pass in a different value to filter by to the query using additionalScope.

  1. Here, we have a query with a WHERE clause checking if the isChecked column is True or False based on the additionalScope.

  2. Then we create a Table to display the data and 2 buttons. One to filter by Checked and the other to filter by Unchecked.

  3. We then add a click event handler to each button to run a script that triggers the query with the additionalScope variable, where isChecked is set to either true or false.

  4. You can now run the same query with different filters by clicking the buttons!

Updates

  • 12/23/24: As of stable version 3.33, the Retool UI provides the option to specify the subset of variables in the query definition that will be provided via additionalScope upon query execution.

    This allows you to utilize additionalScope when triggering queries via event handlers!

Keep in mind

  • The editor will highlight the additionalScope variable in red because the variable does not exist until the JS query runs, triggers the query and passes in the variable. This is expected and will still execute correctly.

  • Multiple variables can be used within the additionalScope argument

  • "additionalScope" won't work, additionalScope will

14 Likes
I'm Having difficulties passing additional scope variables
Sql variable inside retool variable
Not able to access variable from JS query to the REST api query
How can I get the control that invoke the query?
Help writing JS code for pagination
Passing additional scope into the body of an API call
How to trigger query manually with dynamic data?
Additional Scope. Passing in value in to url action type for REST API
Jira pagination using a transformer
Address js Array Variables in SQL
Issues with Bulk Deletion
Adding several rows to posgres retool db
AdditionalScope in table's data source
Calling a RestQuery with an input
Table editable save handler how to pass a path parameter to a rest api query
Update function in workflow does not work
Going to the next page of an API call
Triggering queries inside a mapped value
Expandable Tables - PostGreSQL
Run multiple queries in JS query and only return if query does not equal 0
How to pass dynamic parameters to a route that I have in my backend
Trigger Query from SelectedQuery Variable Name
Additional Scope. Passing in value in to url action type for REST API
Log to Standard Out in JS query (Self-hosted Retool)
How to save comments from the Comment Thread component in RetoolDB
How to dynamically set resource Id from JS?
Interpolating input fields inside JSON request body
Run one of two queries depending on condition
Make any query accept parameters
Using JS Query to iterate through URL array with Rest API
How to use the query "Additional Scope" field?
Addtional scope post is outdated, please replace with newer
Addtional scope post is outdated, please replace with newer
Can't set query params on click
Queries and additional scope
New Table - how to know which action button is clicked
Efficient Nested Tables in Retool: Expand-on-demand with per-row queries
Setting the value of state using function setValue()
Workflow Issue - Server Side Only
Create SQL Insert Statement from a form input
Linklist component event handlers
New Table - Custom Column with Button?
Set GraphQL query variables from Javascript Query
On change value of text field trigger a query
Offline Image Uploads with Local Storage and JavaScript
I want to run a query looping through rows, but how do I reference the current loop row field in another query?
Inserting an array into postgresdb using a JS query
Getting Invalid URL when triggering S3 upload query from script
ListView component value in rest API
PostGre SQL - boucle for in a query
Chart animation