Skip to content

Custom Code action

Custom Code is a versatile tool in Stack that empowers you to create unique logic beyond the standard actions. This feature provides enhanced flexibility and control, allowing you to automate complex tasks and integrate with services that Stack doesn’t natively support. Please note, this is a Premium Action.

To get started, navigate to your workflow and click the “+” icon to add a new action. Then, search for “Custom Code” to select it.

By default, the code is written in JavaScript. This is the supported language for creating your custom logic in Stack.

If you need to use values from previous triggers or actions in your custom code, you can include them here. These values are added to a dictionary called InputData.

  • Enter a key in the “Key” input field.
  • Assign a value by selecting it through the custom value picker.
  • Add multiple properties by clicking “Add Property”.

For example, if a trigger gives customer information that you need to manipulate, add their name to InputData and reference it using inputData.keyName or inputData['keyName'].

Write your code directly in the Code Editor. A sample code snippet is provided for your reference. The output should be formatted as a JavaScript Object or an Array of Objects.

Stack captures and logs all console.log outputs from your code, making it easier to debug and monitor your scripts.

Custom code supports making external HTTP requests. Click the “HTTP Request” button above the code editor and choose from:

  • GET Method
  • POST Method
  • PUT Method
  • PATCH Method
  • DELETE Method
  • HEAD Method
  • OPTIONS Method

The selected request will appear at the bottom of the code editor.

Improve the readability of your code using the “Format Code” button at the bottom of the code editor. Proper formatting helps in understanding the structure and flow of your code.

Testing is crucial. Without testing, the output of your code won’t be available for subsequent steps. Click “Test your Code” to open a popup where you can run the test.

  • If properties are added, enter sample values in the “Test Setup” tab and test the response.
  • Without properties, directly check the response.

After testing, if the code is error-free, you’ll see “Test Result Success”. Otherwise, you’ll need to debug and test again.

You can review input values in the Execution logs. Go to the desired execution and click on “more details” for a deeper look.

Your custom code can directly return a response, streamlining the coding experience and enhancing efficiency in handling asynchronous operations.

Introducing Code with AI (Beta) in Stack! This feature simplifies writing custom code by generating snippets based on your descriptions.

  • AI-Powered Code Generation: Describe the functionality you need, and AI will generate the JavaScript code.
  • Automated Property Integration: AI intelligently incorporates relevant input properties.
  • One-Click Implementation: Quickly review and insert AI-generated code into your workflow.
  • Regenerate if Needed: Not satisfied with the result? Generate a new version with a click.

Pro Tip: Be specific in your prompts for optimal results!

  1. Select: Choose the Custom Code action in your workflow.
  2. Activate AI: Click “Build with AI” to open the AI assistant.
  3. Describe: Enter your desired functionality in plain language.
  4. Generate: Click “Generate” and let AI craft the code.
  5. Regenerate: Mention any changes in the text box and click “Regenerate” if needed.
  6. Implement: Review the output and click “Use Code” to insert it into your workflow.
  • Data Formatting: Automatically format data like phone numbers or dates.
  • API Integration: Connect with external services via HTTP requests.
  • Mathematical Calculations: Perform dynamic calculations.
  • String Manipulation: Extract details from text.
  • Conditional Logic: Create custom decision-making processes.
  • Custom values are not passed during code testing; only contact information is used.
  • Testing is mandatory for code output to be available in subsequent actions.
  • Use Property fields to map values from previous steps, accessing them with inputData.keyName or inputData['keyName'].

Custom Code

The Custom Code action lets you execute JavaScript code within a workflow. This capability allows users to process data and generate output that can be utilized in the following steps of the workflow.

  • Action Name: Assign a name to your custom code step that clearly reflects its purpose within the workflow.

  • Language: The default and currently only supported language is JavaScript.

  • Property to Include in Code: Define key-value pairs where the key will be referenced in your code. The value can be either hardcoded or dynamically mapped from previous workflow steps. Use these pairs in your code by referencing inputData.<key>. For example, if the key is number1, access it as inputData.number1.

  • Code: Write your JavaScript code to process the values from previous steps. Ensure that the output is structured as a JavaScript object or an array of objects. For instance, you might use output = { result: sum }.

  • Action Name: The name displayed for the action in the workflow. (Mandatory)

  • Language: The programming language for the custom code, which is JavaScript by default. (Mandatory)

  • Property To Include In Code: Fields to be used within the code, mapped from previous steps. Access these with inputData.keyName. (Mandatory)

  • Code: The JavaScript code that performs the desired operation. The code should output a JavaScript object. (Mandatory)

// This code is wrapped in an async function
const sum = inputData.number1 + inputData.number2;
// Return the result as a JavaScript object
output = { result: sum };

This example demonstrates adding two numbers from the mapped properties number1 and number2, then outputting their sum.

Can't find what you need? Call or text +1 787 665-9212 or email [email protected].© Stack · Privacy · Terms