Skip to main content

Workflow Action: Custom Code in Stack

What is Custom Code?

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.

How It Works

1. Add 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.

2. Programming Language

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

3. Property to Be Included in Code

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'].

4. Code Editor

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.

5. Enhanced Console Support

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

6. External HTTP Request

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.

7. Format Code

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.

8. Test 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.

9. Check Input Values in Execution Logs

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

10. Return a Response

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

Custom Code AI

Overview

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

What’s New?

  • 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!

How to Use It

  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.

Use Cases

  • 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.

Points to Remember

  • 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'].