LogoLogo
Documentation
Documentation
  • Getting Started
    • Introduction
    • Sign up to Developer Edition
    • Build Your First Agent
    • Developer Support
  • Core Concepts
    • Agent
      • Knowledge
      • Webhook
    • PII Masking
    • Sub-Agent
    • Intent
    • Workflow
      • Node
        • Input
        • Output
        • Loader
        • Display
        • API Node
        • Web Crawler
        • Table Write
        • Table Read
        • Ruleset
        • Upload Document
        • Javascript
        • Workflow
        • Loop
        • Document To Image
        • External Database
        • Storage Write
        • Storage Read
        • Fetch Document
        • Prompt
        • RAG Query
        • Vector Search
        • Emit Event
    • RAG
    • Model Hub
      • Entity Recognizers
    • Data Gateway
    • Rulesets
    • Code Snippets
    • Tables
    • Storage
    • Widget
  • Overview of GenAI
    • Introduction
    • Key concepts
      • Intent Classification
      • Inference
      • Generative AI Models
      • Large Language Models (LLMs)
      • Prompt Engineering
      • AI Agents
      • RAG (Retrieval Augmented Generation)
      • AI Workflow Automation
      • AI Agents vs LLM-based APPs
Powered by GitBook
On this page
  • Overview
  • Configurations
  • Example Use-Cases
  • Best Practices & Key Takeaways for Developers
Export as PDF
  1. Core Concepts
  2. Workflow
  3. Node

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

Field
Description

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

const userName = input.user_name; // Data from the previous node
const apiKey = secret.apiKey; // Secure API Key from Secret Variables
const agentType = agent.type; // Retrieve agent-level variable
const sessionId = context.sessionId; // Workflow-wide session data

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)

{
  "user_id": 123,
  "name": "John Doe",
  "email": "john.doe@example.com",
  "address": {
    "street": "123 Main St",
    "city": "New York",
    "zip": "10001"
  }
}

JavaScript Node Code Snippet

javascriptCopyEditconst main = () => {
  const { name, email, address } = input;
  const location = `${address.street}, ${address.city}, ${address.zip}`;
  return {
    fullName: name,
    emailAddress: email,
    location,
  };
};
main();

Output Passed to Next Node

jsonCopyEdit{
  "fullName": "John Doe",
  "emailAddress": "john.doe@example.com",
  "location": "123 Main St, New York, 10001"
}

🔹 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

const main = () => {
  const { creditScore } = input;
  return {
    creditScore,
    reviewRequired: creditScore < 650 ? true : false
  };
};
main();

Output Passed to Next Node

jsonCopyEdit{
  "creditScore": 620,
  "reviewRequired": true
}

🔹 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

const main = () => {
  const { orderTotal } = input;
  const discount = orderTotal > 500 ? 0.1 : 0.05;
  return {
    orderTotal,
    discountAmount: orderTotal * discount
  };
};
main();

Output Passed to Next Node

jsonCopyEdit{
  "orderTotal": 600,
  "discountAmount": 60
}

🔹 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

javascriptCopyEditconst main = () => {
  try {
    const { amount } = input;
    if (!amount) throw new Error("Amount is required");
    
    return { amount, tax: amount * 0.1 };
  } catch (error) {
    return { error: error.message };
  }
};
main();

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

PreviousUpload DocumentNextWorkflow

Last updated 3 months ago