The Code step allows you to execute custom JavaScript code within your workflow for data processing, transformation, and complex logic implementation. It provides a flexible way to manipulate data, perform calculations, and execute custom logic.

Key features and limitations:

  • Execute JavaScript code (ES6+)
  • Rich editor with syntax highlighting, auto-completion, and error checking
  • Cannot import external libraries
  • No async/await support
  • The code is executed in an isolated environment, so no access to browser APIs like window, document, etc.
  • No access to Node.js modules like fs, path, etc.
  • No network access (functions like fetch are not available)
  • Returns any data type (string, number, object, array, boolean, null, undefined, etc.)

Concept

The Code step wraps your code in a JavaScript function behind the scenes. You only need to write the code that would go inside the function body. Whatever value you return from your code will be used as the output of the step.

For example, instead of writing:

CODE_STEP
function myFunction() {
  const message = 'Hello World';
  return message;
}

You simply write:

CODE_STEP
const message = 'Hello World';
return message;

The Code step handles creating and executing the function for you automatically.

Error

When an error occurs in your code block, whether it’s a syntax error, runtime error, or type error, the workflow will fail and display the error message. The error message will be shown in the step’s output section.

To ensure reliable workflow execution, implement proper error handling in your code. You can throw custom errors with user-friendly messages using the throw statement. These error messages will be displayed as the reason for the workflow failure.

Here’s an example of throwing a custom error:

CODE_STEP
// Throwing a custom error
throw 'Invalid input: Please provide a positive number';
// Result:
// - Step status will be marked as 'FAILED'
// - Error message will be displayed in red in the output section
// - Workflow execution will stop

Troubleshooting

Here are some common issues you might encounter when using the Code step and their solutions:

  • Syntax Errors: Make sure your JavaScript code follows proper syntax. The editor will highlight syntax errors in red.
  • Missing Return Statement: Your code must explicitly return a value to be used by subsequent steps.
  • Undefined Variables: All variables must be declared before use. Check for typos in variable names.
  • Invalid Operations: Ensure you’re performing valid operations on the correct data types.
  • Infinite Loops: Avoid infinite loops as they will cause the step to timeout and fail.

Code not returning any output

CODE_STEP
// Incorrect - Function is defined but never called
function greet() {
  return 'Hello, World!';
}

// Correct - Return the value directly
return 'Hello, World!';

Security Considerations

When using the Code step, keep these security best practices in mind:

  • Avoid Sensitive Data: Never hardcode sensitive information like API keys, passwords, or tokens directly in your code. Instead, use the Variables feature in the workflow to store and reference your secrets securely in code steps or other steps.