Using the Custom Code Action in Workflows
What is the Custom Code Action?
The Custom Code action is a premium feature that gives you the power to extend your CRM's automation capabilities. When the built-in actions don't cover your specific needs, you can use this action to write and execute your own JavaScript code directly within a workflow. This allows you to create complex logic, perform custom data manipulations, and integrate with external services.
How to Configure the Custom Code Action
Adding the Action
To get started, edit your workflow and click the "+" icon to add a new action. Search for "Custom Code" and select it to add it to your workflow sequence.
Setting Up Input Properties
Often, you will need to use data from previous steps in your workflow, such as a contact's name or email address from a trigger. You can pass this information into your custom code by defining input properties. In the action settings, you can add a key (a name for the value) and use the value picker to assign data from your workflow. Click "Add Property" to include multiple values. Inside your code, you can access these values using inputData.keyName or inputData['keyName'].
Writing Your Code
The code is written in JavaScript within the provided code editor. A sample code snippet is provided to help you get started. Your code must return its output as a JavaScript object or an array of objects. The editor includes an "Enhanced console support" feature that captures console.log statements, which is invaluable for debugging.
Making External HTTP Requests
Your custom code can interact with external APIs. Above the code editor, click the "HTTP Request" button. You can then select a request method (GET, POST, PUT, PATCH, DELETE, HEAD, or OPTIONS). The basic structure for the selected request will be automatically inserted into the code editor for you to customize.
Formatting and Testing Your Code
Use the "Format Code" button at the bottom of the editor to automatically adjust the indentation and spacing of your code, making it easier to read. Before you can use the output of your code in subsequent workflow steps, you must test it. Click the "Test your Code" button to open a testing panel.
- If you have defined input properties, you can enter sample values in the "Test Setup" tab.
- Click "Test your Code" to run it. A "Test Result Success" message means your code ran without errors. A "Test Result Failed" message indicates an error that needs to be fixed.
After a workflow runs, you can also check the execution logs to see the input values that were passed to your custom code.
Generating Code with AI (Beta)
You can now use AI to generate code snippets, making the process faster and more accessible.
How to Use AI-Assisted Code Generation
- Add the Custom Code action to your workflow.
- Click the "Build with AI" button to open the AI assistant.
- In the text box, describe what you want the code to do in plain language. For example, "Convert a date from DD-MM-YYYY to YYYY-MM-DD." Being specific will yield better results.
- Click "Generate" to let the AI create the code for you.
- If you are not satisfied with the result, you can describe the changes you want and click "Regenerate."
- Once you are happy with the code, click "Use Code" to insert it into the action.
Common Use Cases
- Data Formatting: Standardize phone numbers, dates, or other contact information.
- API Integration: Connect to external services that are not natively supported by the CRM to send or retrieve data.
- Custom Calculations: Perform complex mathematical operations or business logic on your data.