Skip to content

In n8n, copy this file and paste it onto the canvas, or use Import from File.

{
  "name": "04 - Divide (sub-workflow)",
  "nodes": [
    {
      "parameters": {
        "workflowInputs": {
          "values": [
            { "name": "a", "type": "number" },
            { "name": "b", "type": "number" }
          ]
        }
      },
      "id": "m04s-trigger",
      "name": "When Called",
      "type": "n8n-nodes-base.executeWorkflowTrigger",
      "typeVersion": 1.1,
      "position": [0, 100]
    },
    {
      "parameters": {
        "conditions": {
          "options": { "caseSensitive": true, "leftValue": "", "typeValidation": "strict", "version": 2 },
          "conditions": [
            {
              "id": "z1",
              "leftValue": "={{ $json.b }}",
              "rightValue": 0,
              "operator": { "type": "number", "operation": "equals" }
            }
          ],
          "combinator": "and"
        },
        "options": {}
      },
      "id": "m04s-if",
      "name": "Dividing by Zero?",
      "type": "n8n-nodes-base.if",
      "typeVersion": 2.2,
      "position": [220, 100]
    },
    {
      "parameters": { "errorMessage": "Cannot divide by zero" },
      "id": "m04s-stop",
      "name": "Fail: Division by Zero",
      "type": "n8n-nodes-base.stopAndError",
      "typeVersion": 1,
      "position": [440, 0]
    },
    {
      "parameters": {
        "assignments": {
          "assignments": [
            { "id": "r1", "name": "a", "value": "={{ $json.a }}", "type": "number" },
            { "id": "r2", "name": "b", "value": "={{ $json.b }}", "type": "number" },
            { "id": "r3", "name": "result", "value": "={{ Math.round(($json.a / $json.b) * 1e6) / 1e6 }}", "type": "number" }
          ]
        },
        "options": {}
      },
      "id": "m04s-calc",
      "name": "Calculate",
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.4,
      "position": [440, 200]
    }
  ],
  "connections": {
    "When Called": { "main": [[{ "node": "Dividing by Zero?", "type": "main", "index": 0 }]] },
    "Dividing by Zero?": {
      "main": [
        [{ "node": "Fail: Division by Zero", "type": "main", "index": 0 }],
        [{ "node": "Calculate", "type": "main", "index": 0 }]
      ]
    }
  },
  "pinData": {},
  "settings": { "executionOrder": "v1", "callerPolicy": "workflowsFromSameOwner" }
}