Skip to content

Durasi: 15 menit | Block: 3


💻 Code Node = JavaScript di Dalam Workflow

Kapan pakai Code Node:

  • Transformasi data yang ga bisa di Set Node
  • Array manipulation (filter, map, sort, reduce)
  • Conditional logic yang complex
  • Format data custom

🔣 Code Node Basics

Mode: "Run Once for All Items" vs "Run Once for Each Item"

ModeKapanInputOutput
All ItemsBatch processingArray of all itemsArray of all items
Each ItemPer-record transformSingle itemSingle item

Contoh 1: Format Phone Number (Each Item)

javascript
// Input: { telepon: "081234567890" }
const raw = $input.item.json.telepon;

// Normalize ke format internasional
let phone = raw.replace(/\D/g, ''); // hapus non-digit
if (phone.startsWith('0')) {
  phone = '62' + phone.substring(1);
}

return {
  json: {
    ...$input.item.json,
    telepon_formatted: phone
  }
};

Contoh 2: Filter & Sort (All Items)

javascript
// Ambil semua item, filter & sort
const items = $input.all();

const filtered = items
  .filter(item => item.json.amount > 100000) // hanya order > 100K
  .sort((a, b) => b.json.amount - a.json.amount) // sort descending
  .slice(0, 10); // top 10

return filtered;

Contoh 3: Aggregate / Summary (All Items)

javascript
const items = $input.all();

const summary = {
  total_orders: items.length,
  total_revenue: items.reduce((sum, i) => sum + (i.json.amount || 0), 0),
  avg_order: items.reduce((sum, i) => sum + (i.json.amount || 0), 0) / items.length,
  unique_customers: [...new Set(items.map(i => i.json.email))].length
};

return [{ json: summary }];

Contoh 4: Date Manipulation

javascript
const now = new Date();
const dueDate = new Date(now.getTime() + 7 * 24 * 60 * 60 * 1000); // +7 hari

return {
  json: {
    ...$input.item.json,
    created_at: now.toISOString(),
    due_date: dueDate.toISOString(),
    due_date_formatted: dueDate.toLocaleDateString('id-ID', { 
      day: 'numeric', month: 'long', year: 'numeric' 
    })
  }
};

🧩 Sub-Workflows = Modular Design

Kenapa Sub-Workflows?

Tanpa sub-workflow:
Workflow A = 50 node → susah maintain, susah debug, susah reuse

Dengan sub-workflow:
Workflow A → [Call Sub-Workflow: Format Phone] → continue

Workflow B → [Call Sub-Workflow: Format Phone] → continue
            (reuse logic yang sama)

Build Sub-Workflow

Sub-Workflow: "Format Phone Number"

[Webhook Trigger (called by parent)]
    → [Code Node: normalize phone]
    → [Respond to Webhook: return formatted data]

Parent Workflow:

[Trigger]
    → [Execute Workflow Node: "Format Phone Number"]
        → input: pass $json
        → output: formatted data
    → [Continue with formatted phone]

Execute Workflow Node Config

Node: Execute Workflow
- Source: Database
- Workflow: "Format Phone Number"
- Mode: Wait for Sub-Workflow to Complete

📊 When to Use What

NeedSolution
Simple value mappingSet Node (Edit Fields)
Conditional valueExpression with ternary
Simple logic per itemCode Node (Each Item)
Batch transformCode Node (All Items)
Reusable logic across workflowsSub-Workflow
External API call in logicCode Node + $http helper

⚠️ Common Mistakes

  1. ❌ Pakai Code Node untuk hal yang bisa di Set Node (overcomplicate)
  2. ❌ Lupa return di Code Node → no output → error
  3. ❌ Sub-workflow tanpa error handling → parent ga tahu kalau gagal
  4. ❌ Circular sub-workflow calls → infinite loop

Bootcamp AI Automation — akala.id