Javascript
Overview
The JavaScript Node in UPTIQ Workbench enables developers to execute custom JavaScript code within a workflow. While built-in workflow nodes handle many automation tasks, there are scenarios where custom logic, data transformation, or conditional operations are required. The JavaScript Node provides the flexibility to manipulate, filter, or format data dynamically before passing it to the next step in the workflow.
With this node, developers can: ✅ Perform data transformation by modifying, formatting, or restructuring JSON objects. ✅ Execute mathematical computations such as tax calculations or discount applications. ✅ Implement conditional logic to alter workflow paths based on input values. ✅ Merge, clean, or reformat API responses before passing them to the next node. ✅ Work with context variables, agent-level data, and secret variables for secure and dynamic processing.
Configurations
JavaScript Snippet (Required)
A JavaScript code snippet that will be executed. It must return a value that will be passed to the next node.
Input Variables
Receives data from previous workflow steps via input.<var_name>
.
Agent Variables
Access agent-level variables via agent.<var_name>
.
Secret Variables
Securely retrieve secret values via secret.<var_name>
.
Context Variables
Use context.<var_name>
for workflow-wide data persistence.
Execution Flow
1️⃣ The JavaScript Node receives input from previous workflow steps. 2️⃣ It executes custom JavaScript logic, transforming or processing the input. 3️⃣ The output of the script is passed to the next node in the workflow.
Example Syntax for Variable Usage
Example Use-Cases
Use-Case 1: Formatting API Response Data
A workflow fetches user details from an external API, but the response includes unnecessary fields. The JavaScript Node is used to extract and reformat the required fields while also deriving a new field (location).
API Response (From Previous Node)
JavaScript Node Code Snippet
Output Passed to Next Node
🔹 How It Helps: ✔ Removes unnecessary fields. ✔ Combines multiple fields into a structured response. ✔ Prepares the data for the next workflow step.
Use-Case 2: Applying Conditional Business Logic
A loan eligibility check requires that if a user's credit score is below 650, a flag should be set for manual review.
JavaScript Node Code Snippet
Output Passed to Next Node
🔹 How It Helps: ✔ Automates eligibility checks without requiring a separate ruleset. ✔ Streamlines manual review processes based on conditions.
Use-Case 3: Calculating Discounts Based on Order Total
A workflow calculates a discount percentage based on an order’s total amount.
JavaScript Node Code Snippet
Output Passed to Next Node
🔹 How It Helps: ✔ Implements dynamic business logic for discount calculation. ✔ Reduces dependency on external services for simple calculations.
Best Practices & Key Takeaways for Developers
✅ Always Return an Output – Ensure the script returns a valid JavaScript object or primitive value, as this is passed to the next node.
✅ Validate Inputs – Use checks to avoid undefined values or workflow failures.
✅ Use Secret Variables Securely – When working with API keys or sensitive data, store them in secret.<var_name>
instead of hardcoding values.
✅ Optimize Performance – Keep scripts lightweight to avoid workflow execution delays.
✅ Error Handling is Essential – Use try...catch
to gracefully handle failures within the script.
Example Error Handling Pattern
By integrating the JavaScript Node, developers can extend workflow functionality beyond built-in nodes, allowing for custom logic execution, data transformation, and dynamic workflow adaptability. 🚀
Last updated