Skip to content

Use custom code actions in workflows

Last updated: September 27, 2021

Applies to:

Operations Hub Professional

In workflows, you can use the Custom code action to write and execute JavaScript in your workflow. With custom code actions, you can extend workflow functionality within and outside of HubSpot. To see examples of common custom code actions, view HubSpot's Programmable Automation Use Cases.

Custom code actions support JavaScript using the Node 12.x runtime framework. When the action executes, the runtime compute is managed through a serverless function by HubSpot and AWS Lambda

For more technical information about building custom code actions, view HubSpot's developer documentation

Please note: custom code actions must finish running within 20 seconds, and can only use up to 128 MB of memory. If a custom code action exceeds these limits, HubSpot will reattempt to execute your action for up to three days, starting one minute after failure. Subsequent failures will be retried at increasing intervals, with a maximum gap of eight hours between tries.

Add a custom code action

To add a custom code action to your workflow:

  • In your HubSpot account, navigate to Automation > Workflows.
  • Click the name of a workflow, or create a new workflow
  • Click the plus icon + to add a workflow action.
  • In the right panel, select Custom code

  • To use a secret in your custom code, click the Secrets dropdown menu, then select an existing secret, or add a new secret:
    • To add a new secret, such as an API key, click Add secret. Learn more about how you can access and retrieve your HubSpot API key. You can also learn more about secrets in HubSpot's developer documentation.
    • In the dialog box, enter the Secret name and Secret value.
    • Click Save. You can now select this secret in future custom code actions.
    • To edit or delete existing secrets, click Manage secrets.

  • In the code field, enter your JavaScript. To learn more about coding your action, check out HubSpot's developer documentation.
    • To edit your code in dark mode, click the Light dropdown menu in the code editor, then select Dark

  • To define data outputs that can be used as inputs later in the workflow, for example with a Copy property value action:
    • Under Data outputs, click the Data type dropdown menu, and select a type of data.
    • In the Name field, enter a name for the data output.
    • To add multiple outputs, click Add output
  • Click Save.

Test the action

When adding a custom code action to a workflow, you can test the action to ensure that your code runs as expected before turning the workflow on.

When testing a custom code action, you'll start by selecting a record to test the code with, then run the code. This test will run only the code in your custom action, not any of the other actions in the workflow. When the code is finished running, you'll be able to view the code outputs and the log of your test.

To test a custom code action:

  • In the workflow timeline, click the custom code action.
  • At the bottom of the right sidebar, click Test action to expand the testing section. 

  • Select a record to test your code with by clicking the [Object] dropdown menu, then selecting a record


Please note: when testing your custom code, the code will run and any changes will apply to the selected test record. It's recommended to create a dedicated test record if you want to avoid updating your live records.

  • To run the code, click Test.
  • In the dialog box, confirm that you want to test your code against the selected record by clicking Test.
  • Once your code is done running, the sidebar will display the results of your test:
    • Status: the success or failure status of your custom code action.
    • Data outputs: the values that resulted for your defined data outputs. An alert will display next to any outputs that the code generated which weren't defined either in the Data outputs section or in the code editor. You'll need to add those outputs in order to use them later in the workflow.
    • Logs: information about the test itself, such as how much memory the action took to execute and the total runtime. 

  • To update your custom code action, click Create action to expand the action editor. Continue to update and test your code as needed.
  • When you're done testing the action, click Save to save your changes.

Supported libraries

The Custom code action supports the following JavaScript libraries and versions: