docs/versioned_docs/version-2.16.0/data-sources/custom-js.md
You can write custom JavaScript code to interact with components and queries. To do that, you just need to create a new query and select Run JavaScript Code from the default datasources section.
<div style={{textAlign: 'center'}}> </div>JS parameters in RunJS queries offer a convenient way to customize JavaScript code execution without altering the code directly. You can add parameters by clicking the Add button in the RunJS query editor.
Each parameter requires:
Syntax for calling the parameter: parameters.<name>
Let's create a new parameter named object1 and set the value as object {key1: 'value1'} and use the alert js method to show the value on the pop-up.
Syntax:
alert(parameters.object1)
When the query is triggered the alert will show the parameters value.
<div style={{textAlign: 'center'}}> </div>Let's demonstrate how to utilize parameters in RunJS queries and call one query from another by providing custom parameter values:
multiply. In this query, add the following parameters: num1 with a default value of 10 and num2 with a default value of 2. To display the result, place a text component on the canvas and set its text to {{queries.multiply.data}}. Save and Run the query.callMultiply, where we will invoke the multiply query created earlier using custom parameter values. Here's the code snippet for callMultiply:queries.multiply.run({num1: 20, num2: 20})
By executing this code within callMultiply, we trigger the multiply query with specific values for its parameters.
With this setup, the multiply query can be called from other queries, such as callMultiply, by providing custom parameter values. This allows you to reuse the multiply query with different inputs and display the results accordingly.
+ on the query panel to create a query and select Run JavaScript code from the available datasourcesconst a = Math.floor(Math.random() * (10 - 1)) + 1;
return a;
:::tip
The return statement is used to end the code and the value specified to the return statement will be stored in the data property of the query.
ex: {{queries.runjs1.data}}
You cannot use console.log in Run JavaScript code
:::
Let's edit the properties of widgets:
runjs1 query that we created. This will run the JavaScript code every time the button is clicked.{{queries.runjs1.data}}. It will display the output as Random number: result from JS codevar id = "id" + Math.random().toString(16).slice(2);
return id;
In this code, the resulting ID will have the format "id" followed by a sequence of random hexadecimal characters. For example, it could be something like "id2f4a1b".
return String(Date.now().toString(32) + Math.random().toString(16)).replace(/\./g, '');
In this code, the resulting ID will have the format "timestamp + randomHex", where "timestamp" is the current time in base-32 and "randomHex" is a random hexadecimal value. This ID will be longer than the one generated by Code 1, and it could look like "2g3h1d6a4h3".
Both code snippets will produce IDs that are highly likely to be unique. However, Code 1 generates shorter IDs and follows a more straightforward approach with a fixed prefix ("id"). On the other hand, Code 2 generates longer IDs by incorporating the current timestamp and using a combination of base-32 and hexadecimal representations. The choice between the two methods depends on the specific requirements of the application and the desired length of the generated IDs.
:::tip Resources
ToolJet allows you to internally utilize these libraries:
| Name | Documentation |
|---|---|
| Moment | https://momentjs.com/docs/ |
| Lodash | https://lodash.com/docs/ |
| Axios | https://axios-http.com/docs/intro |
:::info Issues with writing custom JavaScript code? Ask in our Slack Community. :::