2024-03-14 15:01:10 +00:00
---
id: run-actions-from-runjs
title: Run Actions from RunJS query
---
ToolJet allows you to execute various [actions ](/docs/actions/show-alert ) within RunJS queries. This guide outlines the syntax and examples for each action.
< div style = {{paddingTop:'24px', paddingBottom: ' 24px ' } } >
2024-04-05 12:09:03 +00:00
### Run Query
2024-03-14 15:01:10 +00:00
2024-04-05 12:09:03 +00:00
To trigger a query, you can use the below functions:
2024-03-14 15:01:10 +00:00
```js
queries.getSalesData.run()
// replace getSalesData with your query name
```
or
```js
await actions.runQuery('getSalesData')
// replace getSalesData with your query name
```
**Example:**
2024-04-05 12:09:03 +00:00
In the screenshot below, we are triggering two different queries using two different syntax available for `Run Query` action.
2024-03-14 15:01:10 +00:00
< div style = {{textAlign: ' center ' } } >
2024-04-05 12:09:03 +00:00
< img style = {{ border: ' 0 ' , marginBottom: ' 15px ' , borderRadius: ' 5px ' , boxShadow: ' 0px 1px 3px rgba ( 0 , 0 , 0 , 0 . 2 ) ' } } className = "screenshot-full" src = "/img/how-to/run-actions-from-runjs/runquery-v3.png" alt = "Print data from multiple tabs" / >
2024-03-14 15:01:10 +00:00
< / div >
< / div >
< div style = {{paddingTop:'24px', paddingBottom: ' 24px ' } } >
2024-04-05 12:09:03 +00:00
### Get Query Data
2024-03-14 15:01:10 +00:00
2024-04-05 12:09:03 +00:00
In the previous section, we saw how we can trigger queries. Once the queries are triggered, if you want to immediately use the data returned by the query inside the RunJS query, you can use the `getData()` , `getRawData()` and `getLoadingState()` functions:
2024-03-14 15:01:10 +00:00
2024-04-11 06:07:05 +00:00
#### Trigger a query and retrieve its data:
2024-03-14 15:01:10 +00:00
2024-04-05 12:09:03 +00:00
```js
2024-04-11 06:07:05 +00:00
await queries.getSalesData.run();
// replace getSalesData with your query name
2024-04-05 12:09:03 +00:00
let value = queries.getSalesData.getData();
// replace getSalesData with your query name
```
2024-03-14 15:01:10 +00:00
2024-04-11 06:07:05 +00:00
#### Trigger a query and retrieve its raw data:
2024-03-14 15:01:10 +00:00
2024-04-05 12:09:03 +00:00
```js
2024-04-11 06:07:05 +00:00
await queries.getCustomerData.run();
2024-04-05 12:09:03 +00:00
//replace getCustomerData with your query name
2024-03-14 15:01:10 +00:00
2024-04-05 12:09:03 +00:00
let value = queries.getCustomerData.getRawData();
// replace getCustomerData your with query name
2024-03-14 15:01:10 +00:00
```
2024-04-11 06:07:05 +00:00
#### Trigger a query and retrieve its loading state:
2024-03-14 15:01:10 +00:00
2024-04-05 12:09:03 +00:00
```js
2024-04-11 06:07:05 +00:00
await queries.getTodos.run()
2024-04-05 12:09:03 +00:00
//replace getTodos with your query name
2024-03-14 15:01:10 +00:00
2024-04-05 12:09:03 +00:00
let value = queries.getTodos.getLoadingState();
//replace getTodos with your query name
```
2024-03-14 15:01:10 +00:00
< / div >
< div style = {{paddingTop:'24px', paddingBottom: ' 24px ' } } >
2024-04-05 12:09:03 +00:00
### Set Variables
2024-03-14 15:01:10 +00:00
2024-04-05 12:09:03 +00:00
To create a variable, you can use the below function:
2024-03-14 15:01:10 +00:00
```javascript
2024-04-05 12:09:03 +00:00
actions.setVariable('< variableName > ', `<variableValue>` )
2024-03-14 15:01:10 +00:00
```
< / div >
< div style = {{paddingTop:'24px', paddingBottom: ' 24px ' } } >
2024-04-05 12:09:03 +00:00
### Unset Variable
To delete a created variable, you can use the below function:
2024-03-14 15:01:10 +00:00
**Syntax:**
```javascript
2024-04-05 12:09:03 +00:00
actions.unSetVariable('< variableName > ')
2024-03-14 15:01:10 +00:00
```
< / div >
< div style = {{paddingTop:'24px', paddingBottom: ' 24px ' } } >
2024-04-05 12:09:03 +00:00
### Get Variables
2024-03-14 15:01:10 +00:00
2024-04-11 06:07:05 +00:00
To access variables immediately after setting them in a RunJS query, you can use the `getVariable` and `getPageVariable` functions:
2024-03-14 15:01:10 +00:00
2024-04-11 06:07:05 +00:00
#### Set and retrieve a variable:
2024-03-14 15:01:10 +00:00
2024-04-05 12:09:03 +00:00
```js
actions.setVariable('mode','dark');
//replace mode with your desired variable name
2024-03-14 15:01:10 +00:00
2024-04-05 12:09:03 +00:00
return actions.getVariable('mode');
```
2024-03-14 15:01:10 +00:00
2024-04-11 06:07:05 +00:00
#### Set and retrieve a page-specific variable:
2024-04-05 12:09:03 +00:00
```js
actions.setPageVariable('number',1);
//replace number with your desired variable name
return actions.getPageVariable('number');
```
2024-03-14 15:01:10 +00:00
< / div >
< div style = {{paddingTop:'24px', paddingBottom: ' 24px ' } } >
2024-04-05 12:09:03 +00:00
### Logout
2024-03-14 15:01:10 +00:00
2024-04-05 12:09:03 +00:00
To log out the current logged-in user from the ToolJet, use the below function:
2024-03-14 15:01:10 +00:00
```javascript
2024-04-05 12:09:03 +00:00
actions.logout();
2024-03-14 15:01:10 +00:00
```
< / div >
< div style = {{paddingTop:'24px', paddingBottom: ' 24px ' } } >
2024-04-05 12:09:03 +00:00
### Show Modal
2024-03-14 15:01:10 +00:00
2024-04-05 12:09:03 +00:00
To open a modal using RunJS query, use the below function:
2024-03-14 15:01:10 +00:00
```javascript
2024-04-05 12:09:03 +00:00
actions.showModal('< modalName > ')
2024-03-14 15:01:10 +00:00
```
< / div >
< div style = {{paddingTop:'24px', paddingBottom: ' 24px ' } } >
2024-04-05 12:09:03 +00:00
### Close Modal
2024-03-14 15:01:10 +00:00
2024-04-05 12:09:03 +00:00
To close a modal using RunJS query, use the below function:
2024-03-14 15:01:10 +00:00
2024-04-05 12:09:03 +00:00
```javascript
actions.closeModal('< modalName > ')
2024-03-14 15:01:10 +00:00
```
2024-04-05 12:09:03 +00:00
< / div >
2024-03-14 15:01:10 +00:00
2024-04-05 12:09:03 +00:00
< div style = {{paddingTop:'24px', paddingBottom: ' 24px ' } } >
2024-03-14 15:01:10 +00:00
2024-04-05 12:09:03 +00:00
### Set Local Storage
2024-03-14 15:01:10 +00:00
2024-04-05 12:09:03 +00:00
Set a value in local storage using the below code:
2024-03-14 15:01:10 +00:00
2024-04-05 12:09:03 +00:00
**Syntax:**
2024-03-14 15:01:10 +00:00
2024-04-05 12:09:03 +00:00
```javascript
actions.setLocalStorage('key', 'value');
2024-03-14 15:01:10 +00:00
```
< / div >
< div style = {{paddingTop:'24px', paddingBottom: ' 24px ' } } >
2024-04-05 12:09:03 +00:00
### Copy to Clipboard
2024-03-14 15:01:10 +00:00
2024-04-05 12:09:03 +00:00
Use the below code to copy content to the clipboard:
2024-03-14 15:01:10 +00:00
```javascript
2024-04-05 12:09:03 +00:00
actions.copyToClipboard('< contentToCopy > ')
2024-03-14 15:01:10 +00:00
```
< / div >
< div style = {{paddingTop:'24px', paddingBottom: ' 24px ' } } >
2024-04-05 12:09:03 +00:00
### Generate File
2024-03-14 15:01:10 +00:00
2024-04-05 12:09:03 +00:00
The below action can be used to generate a file.
2024-03-14 15:01:10 +00:00
```js
2024-04-05 12:09:03 +00:00
actions.generateFile('< fileName > ', '< fileType > ', '< data > ')
2024-03-14 15:01:10 +00:00
```
2024-04-05 12:09:03 +00:00
`fileName` is the name that you want to give the file(string), `fileType` can be **csv** , **plaintext** , or **pdf** and `data` is the data that you want to store in the file.
Example for generating CSV file:
2024-03-14 15:01:10 +00:00
```js
2024-04-05 12:09:03 +00:00
actions.generateFile('csvfile1', 'csv', '{{components.table1.currentPageData}}') // generate a csv file named csvfile1 with the data from the current page of table
2024-03-14 15:01:10 +00:00
```
2024-04-05 12:09:03 +00:00
Example for generating Text file:
2024-03-14 15:01:10 +00:00
2024-04-05 12:09:03 +00:00
```js
actions.generateFile('textfile1', 'plaintext', '{{JSON.stringify(components.table1.currentPageData)}}') // generate a text file named textfile1 with the data from the current page of table (stringified)
```
2024-03-14 15:01:10 +00:00
2024-04-05 12:09:03 +00:00
Example for generating PDF file:
2024-03-14 15:01:10 +00:00
```js
2024-04-05 12:09:03 +00:00
actions.generateFile('Pdffile1', 'pdf', '{{components.table1.currentPageData}}') // generate a text file named Pdffile1 with the data from the current page of table
2024-03-14 15:01:10 +00:00
```
< / div >
< div style = {{paddingTop:'24px', paddingBottom: ' 24px ' } } >
2024-04-05 12:09:03 +00:00
### Go to App
2024-03-14 15:01:10 +00:00
2024-04-05 12:09:03 +00:00
You can switch to a different application using the below action:
2024-03-14 15:01:10 +00:00
2024-04-05 12:09:03 +00:00
```javascript
actions.goToApp('slug',queryparams)
2024-03-14 15:01:10 +00:00
```
2024-04-05 12:09:03 +00:00
- `slug` can be found in URL of the released app after `application/` or in the share modal that opens up when you click on the `Share` button on the top-right of the app-builder
2024-08-09 10:37:24 +00:00
- `queryparams` can be provided in this format - `[ ['key1','value1' ], ['key2','value2'] ]`
2024-04-05 12:09:03 +00:00
2024-03-14 15:01:10 +00:00
< / div >
< div style = {{paddingTop:'24px', paddingBottom: ' 24px ' } } >
2024-04-05 12:09:03 +00:00
### Show Alert
2024-03-14 15:01:10 +00:00
2024-04-05 12:09:03 +00:00
To show an alert using RunJS query, use the below code:
2024-03-14 15:01:10 +00:00
2024-04-05 12:09:03 +00:00
```js
actions.showAlert('< alert type > ' , '< message > ' )
```
Available alert types are `info` , `success` , `warning` , and `danger` .
2024-03-14 15:01:10 +00:00
2024-04-05 12:09:03 +00:00
Example:
2024-03-14 15:01:10 +00:00
```js
2024-04-05 12:09:03 +00:00
actions.showAlert('error' , 'This is an error' )
2024-03-14 15:01:10 +00:00
```
< / div >
< div style = {{paddingTop:'24px', paddingBottom: ' 24px ' } } >
2024-04-05 12:09:03 +00:00
### Run Multiple Actions From RunJS Query
2024-03-14 15:01:10 +00:00
2024-04-05 12:09:03 +00:00
To run multiple actions from a RunJS query, you'll have to use **async-await** in the function.
2024-03-14 15:01:10 +00:00
2024-04-05 12:09:03 +00:00
Here is a example code snippet for running the queries and showing alert after specific intervals. Check the complete guide on running queries at specified intervals ** [here ](/docs/how-to/run-query-at-specified-intervals )**.
2024-03-14 15:01:10 +00:00
```js
2024-04-05 12:09:03 +00:00
actions.setVariable('interval',setInterval(countdown, 5000));
async function countdown(){
await queries.restapi1.run()
await queries.restapi2.run()
await actions.showAlert('info','This is an information')
}
2024-03-14 15:01:10 +00:00
```
< / div >