Skip to main content
Automation API

custom_code

Use this action to run your own JavaScript in an automation.

Tip

Read the live config shape of each action type from GET /v1/automation/meta/action before you build on it.

In the automation editor, this action is Execute a script in the Code & variables family. Its group is action, and it works with every trigger.

Send it in the actions array of Create an automation or Update an automation. An update replaces the whole array, so include the other actions.

Example​

This action reads the deal value of a sales record and stores a 3 % commission in the variable Commission:

{
"type": "custom_code",
"config": {
"code": [
"const dealValue = Number(",
{ "kind": "variable", "source": "field", "field_id": 601, "field_type": "number", "previous": false, "collection": false, "triggering": false },
");\n",
{ "kind": "variable", "source": "custom", "custom_type": "any", "label": "Commission" },
" = Math.round(dealValue * 0.03);"
],
"custom_variable_defs": [
{ "custom_type": "any", "label": "Commission" }
]
}
}

Config​

PropertyTypeDescriptionExample value
codecode valueRequired. The JavaScript the action runs. String literals mixed with variable references. A plain string works too.["const total = 1 + 1;"]
custom_variable_defsarray of variable declarationsCustom variables this action declares. Its own code and later actions can use them. Empty when left out.[{ "custom_type": "any", "label": "Commission" }]

Variable types​

Each entry of custom_variable_defs needs a label and a custom_type. custom_type takes one of these lower-case tokens:

  • any
  • single_file
  • multi_file
  • single_link
  • html_table_rows

Use the variable in a later action​

Assign the result to a declared variable, as in the example. A later action reads it with a custom reference that has the same custom_type and label:

{ "kind": "variable", "source": "custom", "custom_type": "any", "label": "Commission" }

General behavior​

  • code is the only required key.
  • code is a code value. It holds string literals and variable references only. Tape doesn't resolve template values in it.
  • Tape runs code as a block of statements inside the automation's script. It isn't a single expression like the assignment_code of custom_variable.
  • The write path doesn't check the syntax. A broken script saves and passes validate. The error shows up only in the run log.
  • custom_variable_defs defaults to []. On read, Tape leaves it out when the action declares no variables.
  • A custom reference resolves only when this action or an earlier one declares a variable with the same custom_type and label. Otherwise validation reports action_variable_missing. See Errors & broken reasons.

Requirements​

  • To add or change this action through the API, your token needs the automations:edit capability. See Capabilities.
  • You must administer the workspace that the automation's database app belongs to.

Next steps​