Looping in a JavaScript block and returning specified output
The JavaScript block in a Cortex Workflow allows you to write custom scripts directly in your Workflow logic, giving you the flexibility to streamline tasks that would otherwise require additional tooling or manual intervention. Common use cases include:
Transforming data: Use JavaScript to convert API responses or output from another block into something easier to use downstream.
Filtering or searching inside data: Narrow down results from a previous block, like selecting high-priority items or matching a tag.
Math and aggregation: Run calculations across arrays or values, including handling time-based logic.
Defaults and fallbacks: Avoid runtime errors by checking if values exist before using them elsewhere in the Workflow.
Configure a JavaScript block to return user names and company names
The example below demonstrates how to fetch and work with example user data from a public API. In this example, you call an external API, parse and loop through the results, then return useful output into the Workflow's evolving state.
It performs the following actions:
Sends a GET request to
jsonplaceholder.typicode.com/users
Sets a typical
Content-Type
headerUses
await
to pause execution until the response is receivedTurns the data into an array of objects and creates a flat array of strings
Once the response is received, it parses the JSON body and extracts just the user names and their company names
The final output includes the HTTP status code and a simplified list of users. This output becomes part of the actions
object. Both the responseStatus
and the parsed users
array are now available as action context, which can be referenced in subsequent steps of the Workflow.
Add a JavaScript block to the Workflow
Follow the instructions to create a Workflow and configure its settings.
Add a JavaScript block to your Workflow.
In the block configuration side panel, add a block name and a unique slug.
Under the Javascript header, in the text editor, enter your JavaScript expression.
In this example, we use the following:
// Example base URL
const baseUrl = `https://jsonplaceholder.typicode.com/users`; // Public API that returns fake user data
const myHeaders = {
"Content-Type": "application/json" // Typical header for JSON APIs
};
// Send the HTTP request and wait for the response to come back
const response = await fetch(baseUrl, {
headers: myHeaders // Makes a GET request with the specified headers
});
// Pauses the function until the fetch call finishes (asynchronously)
// Parse the response body as JSON and wait for it to complete
const responseBody = await response.json();
// Create a simplified array of objects with name and company
const simplifiedUsers = responseBody.map(user => ({
name: user.name, // Pulls out the user's full name
company: user.company.name // Pulls out the name of the user's company
}));
// Create a flat array of strings like "Leanne Graham (Romaguera-Crona)"
// This is required for use in dropdowns or multiselect inputs
const usersFormatted = simplifiedUsers.map(user => `${user.name} (${user.company})`);
return {
responseStatus: response.status, // Returns the HTTP status code (e.g., 200 for success)
users: simplifiedUsers, // Cleaned-up list of name + company pairs (objects)
usersFormatted: usersFormatted // Flat string array — works in User Input blocks
};
Click Save at the bottom of the block configuration side panel.
Run the Workflow
After you have finished creating your Workflow, click Run in the upper right corner while viewing the Workflow.
After the run completes, go to the Outputs tab of the run to view the users
and responseStatus
values returned by the JavaScript block:

Referencing the output later in the Workflow
It is possible to reference the outputs of the JavaScript block later in the Workflow, using the following:
actions.<your-block-slug>.outputs.result.usersFormatted
actions.<your-block-slug>.outputs.result.responseStatus
Example: Add a User input block after the JavaScript block
You might later want to include a User input block where the person running the Workflow can select a name from the JavaScript block's output:
In your Workflow, add a User input block.
In the block configuration side panel, include a name and a unique slug for the block, then click Add user input.
Configure the input:
Enter a name and key for the input.
In this example, the name is "Choose name."
Select a type. This example uses the
Select
type.In the Path to override value field, include a reference to the list of
users
that came from the output of the JavaScript block (which uses the slugjavascript
):actions.javascript.outputs.result.usersFormatted
Click Save at the bottom of the block configuration side panel.
Run the Workflow.
After the JavaScript block runs, you will be prompted to select a name. After selecting, click Submit.
After you run the Workflow and select a choice, you will see the choice in the Outputs tab at the bottom of the page:

Next steps
After a user makes a select from your User input block, you can use the collected input in many different ways in the next steps of your Workflow. For example:
Branch the Workflow based on the user's selection: You could add a Branch block. This can use a conditional expression (in CEL syntax) that references the user's selection to determine the path the Workflow should take next.
For example, you might set a path expression like
actions["user-input"].outputs.selection == "Leanne Graham (Romaguera-Crona)"
to run a specific set of steps if the user chose the nameLeanne Graham (Romaguera-Crona)
from the dropdown during the User input step.
Trigger integration blocks: The user's selection could be used to drive actions in third-party integrations such as creating a Pull Request in GitHub, sending a notification to Slack or Microsoft Teams, and more. Cortex offers a block library with over 200 actions to choose from.
Last updated
Was this helpful?