Skip to main content

Run code

ComponentTypeDescription
imgRun Code🔀 actionrun a snippet of Javascript code

The Run Code component allows you to run a snippet of code as part of a workflow. You can pass in placeholders that can then be used as variables in the code, and then pass values generated by the code as placeholders into the next component.

img

Technical details​

Behind the scenes, the Run Code component runs on Amazon Web Services Lambda, and has the following technical details

  • runs in a node.js 16 environment or python 3.9 environment
  • execution time for the code cannot exceed 2 seconds
  • size of the code cannot exceed 50MB
  • cannot save any files (does not have access to any storage)
  • does not have any access to additional libraries
  • is not connected to the internet and does not run in a browser environment

If you need to run a snipped of code with libraries and/or a connection to the internet, this is available in the Advanced Run Code component.

note

We recommend using the built-in AI component builder and editor if your account has been provided access.

Step 1: Setting input variables​

To pass in placeholders as variables you can access in the code, select them from the Set Input Variable field.

img

img

Once selected, these fields can be referenced in the body of the code itself. You can also bring up a list of input placeholders by typing in "$":

img

warning

Not setting the input placeholders properly will result in errors​

If a placeholder is not set as an input variable, it will not be properly substituted in when the workflow and the run code component is run. You may still reference it, but the code may throw an error that the value is undefined.

Step 2: Write your code and map the outputs​

Write the code you want to run, with the output specified in an object mapping it to placeholders like this:

For example:

// This is a simple JavaScript code that outputs a 'Hello World' message along with the current timestamp
// Define the 'Hello World' message
let helloWorldMessage = 'Hello World';
// Get the current timestamp
let currentTimestamp = new Date();
// Return the message and timestamp
return { helloWorldMessage: helloWorldMessage, currentTimestamp: currentTimestamp };
tip

At the start of your code, map the placeholders to variables you will use e.g. ${placeholder} = placeholder. Placeholders are replaced with the actual values before the code is run. However, the $ notation required for placeholders inside of Workflow86 may make the raw code more difficult to debug outside of Workflow86.

Note that the output of the function must be returned in an object mapping it to placeholders like this.

// Return the message and timestamp
return { helloWorldMessage: helloWorldMessage, currentTimestamp: currentTimestamp };

The key (left) will be used to set the placeholder name and the value (right) should be the internal variable name inside of the code. The key and value can be the same in this mapping i.e. "helloWorldMessage:helloWorldMessage".

important

This return mapping is required to access any values from the code in downstream components in your workflow.

Step 3: Setting the placeholders to export​

The final step is to set the values you want to export as placeholders to the next component. To do this, you will reference the key in the key value pair you created in Step 2. In the example below, we will export output1 and output 2 as placeholders:

img

You can also set the data type for the placeholders here as well, otherwise they will be exported as strings/text by default.

Step 4: Perform a test run​

Click the Test button to perform a test run of the code to make sure everything is working properly.

Debugging errors in your code​

The Run Code component will return error messages if it encounters issues when running the code.

img

The tables below show the main code errors you may encounter.

tip

To troubleshoot code errors, you can try copying and pasting the code error into Workflow86 AI via a component edit action. Workflow86 AI should return a configuration with its attempt to address the code error.

Python​

  • AssertionError: This error occurs when the statements in the assert function do not satisfy the condition. An example of an AssertionError is when an assertion fails intentionally:
# AssertionError
assert False, "This assertion failed intentionally."
  • AttributeError: This error occurs when a non-existent attribute has been called. An example of an AttributeError is attempting to access an attribute that does not exist:
# AttributeError
class MyClass:
pass

obj = MyClass()
print(obj.nonexistent_attribute)
  • FloatingPointError: This error occurs when there is a calculation error that involves floats. An example of a FloatingPointError is dividing a float by zero:
# FloatingPointError
result = 1.0 / 0.0
  • ImportError: This error occurs when you try to import a non-existent module. An example of an ImportError is attempting to import a module that does not exist:
# ImportError
import nonexistent_module
  • IndentationError: This error occurs when there is an indentation that doesn’t satisfy Python’s syntax standards. An example of an IndentationError is incorrect indentation:
# IndentationError
def func():
print("Indented correctly")
print("This will raise IndentationError")
  • IndexError: This error occurs when you try to reference an index that is out of bounds or does not exist. An example of an IndexError is accessing an index that is not present in the list:
# IndexError
my_list = [1, 2, 3]
print(my_list[5])
  • KeyError: This error occurs when you try to call a key in a dictionary that doesn’t exist. An example of a KeyError is attempting to access a non-existent key in a dictionary:
# KeyError
my_dict = {"key": "value"}
print(my_dict["nonexistent_key"])
  • MemoryError: This error occurs when the program runs out of memory. An example of a MemoryError is trying to create a list that is too large:
# MemoryError
big_list = [0] * 10**10
  • NameError: This error occurs when a non-existent variable has been called. An example of a NameError is attempting to print a variable that has not been defined:
# NameError
print(nonexistent_variable)
  • OverflowError: This error occurs when a calculation is done on numbers that are too big. An example of an OverflowError is performing an operation that results in a number larger than what is supported:
# OverflowError
result = 2 ** 1000
  • SyntaxError: This error occurs when there are any syntax errors. An example of a SyntaxError is missing a parenthesis:
# SyntaxError
print("hello world"
  • ZeroDivisionError: This error occurs when you try to divide a number by 0. An example of a ZeroDivisionError is dividing any number by zero:
# ZeroDivisionError
result = 1 / 0

Node.js​

  • RangeError: This error is thrown when the index is out of bounds or non-existent. An example of a RangeError is when you try to create an array with a negative length:
// RangeError
var arr = new Array(-1);
  • ReferenceError: This error occurs when the variable you are trying to reference does not exist. For instance, trying to log a non-existent variable to the console will throw a ReferenceError:
// ReferenceError
console.log(nonexistentVariable);
  • SyntaxError: This error is thrown when there are any syntax errors. An example of a SyntaxError is missing a closing parenthesis in an eval statement:
// SyntaxError
eval("alert('Hello, world!'"
  • TypeError: This error is thrown when you run a function that is incompatible with the variable you are running from. For example, attempting to call toUpperCase on a number will result in a TypeError:
// TypeError
var x = 1;
x.toUpperCase();
  • URIError: This error is thrown when you use illegal characters in a URI function. An example of a URIError can be seen when trying to decode a standalone percent symbol in a URI, which is not valid:
// URIError
try {
decodeURI("%");
} catch (error) {
if (error instanceof URIError) {
console.error("URIError occurred: " + error.message);
}
}

Instructions for AI​

If you are using an external AI product or service to generate code such as ChatGPT, use the following instructions in your request or prompt to generate code that can be run inside of this component.

- Code cannot require internet access or package installation to execute properly in designated environment. 
- Choose javascript (node.js 16) or python (python 3.9) based on best fit to execute the requirements or instructions.
- If you select python, remember that proper indentation is critical for the source code to run properly.
- placeholders must be referenced with \$\{}.
- do not wrap placeholders in the code with single or double quotes.
- Return javascript or python objects depending on the selected language.
- Map the output values or variables from the source code to output placeholders in an object in this format: return { <outputPlaceholder1>: <variable2>, <outputPlaceholder2>: <variable2> }