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"
| Mode | Kapan | Input | Output |
|---|---|---|---|
| All Items | Batch processing | Array of all items | Array of all items |
| Each Item | Per-record transform | Single item | Single 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
| Need | Solution |
|---|---|
| Simple value mapping | Set Node (Edit Fields) |
| Conditional value | Expression with ternary |
| Simple logic per item | Code Node (Each Item) |
| Batch transform | Code Node (All Items) |
| Reusable logic across workflows | Sub-Workflow |
| External API call in logic | Code Node + $http helper |
⚠️ Common Mistakes
- ❌ Pakai Code Node untuk hal yang bisa di Set Node (overcomplicate)
- ❌ Lupa
returndi Code Node → no output → error - ❌ Sub-workflow tanpa error handling → parent ga tahu kalau gagal
- ❌ Circular sub-workflow calls → infinite loop