Skip to main content

Create functions to reuse queries and logic in workflows

Learn how to create reusable queries or logic in a workflow.

Functions enable you to create reusable queries or complex logic. You can call functions anywhere in a workflow as function blocks, using JavaScript, or within a Loop block. Functions support optional parameters and have access to the global workflow scope.

Single-step and multi-step functions

You can choose whether a function is single-step or multi-step. The type you choose depends on the complexity or functionality required.

Multi-Step Functions are currently in public beta on Retool Cloud and on Self-hosted Retool.

  • Single-step: The function contains a single query that runs with optional parameters when called. Single-step functions are useful for simple logic, such as retrieving database records or transforming results.
  • Multi-step: The function operates as a self-contained workflow with optional parameters. It has its own control flow and is assembled in the same way as its parent workflow. It supports most available block types, including logic and response blocks, and emits block-level logs. Multi-step functions are useful for complex operations with many steps.

Multi-step functions do not support user task blocks.

1. Add a function

First, click Functions in the left panel, then create a function. Then, configure the function.

  1. Define any optional parameters to pass into a function when called. Click + Add parameter and set the parameter name. You can optionally provide a test value that is used only when the function is tested.
  2. Select whether to use a single-step or multi-step function type.

Changing a function from multi-step to single-step permanently deletes the multi-step function logic.

2. Configure function logic

The Query section contains the query or code that runs when the function is called. You can select a resource to query, Run JS Code or Run Python Code to execute JavaScript or Python code, or Retool Workflow to trigger another workflow.

For example, you could create a function that sends a trial expiration notification to customers when the trial is three days and one day from expiration. Instead of adding two separate blocks to send emails, you can create a single function and reuse it. The workflow first filters the customer data to determine which trials expire in 24 hours or in three days, then calls the same function. The function receives the customer's name and email address as parameters, along with either tomorrow or the trial expiration date.

Configure the resource or code that the function runs when called.

4. Test the function

Click ▶︎ Test to run the function. If your function makes use of parameters, any test values you provided are used.

5. Call the function from the workflow

You can call a function using the following methods:

  • Using the Function block to the workflow.
  • Within a JavaScript Code block.
  • As the loop runner for a Loop block.

Loop blocks and Function blocks that call multi-step functions do not support custom timeouts. Instead, these blocks use a total timeout that is calculated by adding the timeouts of each block in the multi-step function.

If a timeout is necessary for a multi-step function, you can call the function within a Code block and set a timeout on the Code block itself.

The workflow timeout always takes precedence over timeouts set within blocks or functions.

Click in the left panel to open the Blocks tab, then drag the Function block type to the canvas. You can also right-click anywhere on the canvas to display the Add block contextual menu.

If you defined any parameters for the selected function, the block displays input fields for you to populate. You can then reference any data using {{ }} expressions, such as {{ getCustomersOnTrial.data }}.

A streamlined workflow using a multi-step function.

The Function block returns its output back to the block from which it was called, allowing the output to be used in the rest of the workflow.

The Run History menu panel shows the workflow run. Click multi-step functions to expand them and see the run logs of the blocks within them.

Run History that includes a multi-step function.

Convert a group of blocks to a multi-step function

You can convert a group of blocks within an existing workflow to a multi-step function. First, press and hold shift while click-and-dragging on the canvas to select a group of blocks. Next, right-click on the selection area and click Create function.

Convert a group of blocks to a function.

The selected blocks are then replaced in the workflow by a Function block that references the newly created function.