Reuse queries and logic in workflows
Learn how to create reusable blocks using Functions in Retool Workflows.
Functions are reusable blocks that run queries in a headless state. They operate outside of the control flow and do not appear on the canvas. You call Function blocks from JavaScript queries and can pass data as parameters. This reduces the need for query duplication and enables you to perform certain tasks only when necessary, not for every workflow run.
Add a Function block
To create a Function block, click + in the Functions section of the left panel. You can configure optional parameters for each block that you can reference within the block itself. Each parameter must have a test value so it can perform a test run without being called.
You can also create Function blocks directly from a block's code editor. Type /
on a new line and enter the name of a resource, then press Enter. You can also reference an existing function in the same way by entering the name of an existing function.
The resource name refers to the name used by your Retool organization (e.g., onboarding_db), not the name of the service used (e.g., PostgreSQL).
Call functions
You call Function blocks using await
and include values for each parameter, if set. For example:
const subs = getExpiringSubscriptions.data;
const message =
workflowContext.name +
": You have " +
subs.length +
" subscription(s) expiring this week!";
await sendMessage(message);
The Function block returns its output back to the block from which it was called, allowing it to be used in the rest of the workflow.