Custom Code action
What is Custom Code?
Section titled “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
Section titled “How It Works”1. Add Action
Section titled “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
Section titled “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
Section titled “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
Section titled “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
Section titled “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
Section titled “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
Section titled “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
Section titled “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
Section titled “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
Section titled “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
Section titled “Custom Code AI”Overview
Section titled “Overview”Introducing Code with AI (Beta) in Stack! This feature simplifies writing custom code by generating snippets based on your descriptions.
What’s New?
Section titled “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
Section titled “How to Use It”- Select: Choose the Custom Code action in your workflow.
- Activate AI: Click “Build with AI” to open the AI assistant.
- Describe: Enter your desired functionality in plain language.
- Generate: Click “Generate” and let AI craft the code.
- Regenerate: Mention any changes in the text box and click “Regenerate” if needed.
- Implement: Review the output and click “Use Code” to insert it into your workflow.
Use Cases
Section titled “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
Section titled “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.keyNameorinputData['keyName'].
Action Name
Section titled “Action Name”Custom Code
Action Description
Section titled “Action Description”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 Details
Section titled “Action Details”How to Configure
Section titled “How to Configure”-
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 isnumber1, access it asinputData.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 }.
Field Descriptions
Section titled “Field Descriptions”-
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)
Example
Section titled “Example”// This code is wrapped in an async functionconst sum = inputData.number1 + inputData.number2;// Return the result as a JavaScript objectoutput = { result: sum };This example demonstrates adding two numbers from the mapped properties number1 and number2, then outputting their sum.