{
  "name": "Deal Stage Change Alert — Salesforce → Slack",
  "nodes": [
    {
      "parameters": {
        "triggerOn": "opportunityUpdated"
      },
      "id": "salesforce-trigger",
      "name": "Salesforce Trigger",
      "type": "n8n-nodes-base.salesforceTrigger",
      "typeVersion": 1,
      "position": [0, 0],
      "credentials": {
        "salesforceOAuth2Api": {
          "id": "credential-id",
          "name": "Salesforce OAuth2"
        }
      }
    },
    {
      "parameters": {
        "conditions": {
          "string": [
            {
              "value1": "={{ $json.StageName }}",
              "operation": "isNotEmpty"
            }
          ]
        }
      },
      "id": "filter-stage-change",
      "name": "Stage Changed?",
      "type": "n8n-nodes-base.if",
      "typeVersion": 2,
      "position": [220, 0]
    },
    {
      "parameters": {
        "method": "GET",
        "url": "={{ $('Salesforce Trigger').first().json.attributes?.url ? $('Salesforce Trigger').first().json.attributes.url : '' }}",
        "authentication": "predefinedCredentialType",
        "nodeCredentialType": "salesforceOAuth2Api",
        "sendQuery": true,
        "queryParameters": {
          "parameters": [
            {
              "name": "q",
              "value": "=SELECT Id, Name, Amount, StageName, Account.Name, Owner.Name, LastModifiedDate FROM Opportunity WHERE Id = '{{ $('Salesforce Trigger').first().json.Id }}'"
            }
          ]
        },
        "options": {}
      },
      "id": "fetch-opportunity",
      "name": "Fetch Opportunity",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [440, 0],
      "credentials": {
        "salesforceOAuth2Api": {
          "id": "credential-id",
          "name": "Salesforce OAuth2"
        }
      }
    },
    {
      "parameters": {
        "mode": "runOnceForAllItems",
        "jsCode": "const records = $('Fetch Opportunity').first().json.records || [];\nconst opp = records[0] || $('Salesforce Trigger').first().json;\n\nreturn [{\n  json: {\n    id: opp.Id,\n    name: opp.Name,\n    amount: parseFloat(opp.Amount || '0'),\n    stageName: opp.StageName,\n    accountName: opp.Account?.Name || 'Unknown',\n    ownerName: opp.Owner?.Name || 'Unknown',\n  }\n}];"
      },
      "id": "format-data",
      "name": "Format Data",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [660, 0]
    },
    {
      "parameters": {
        "select": "channel",
        "channelId": {
          "__rl": true,
          "value": "sales-pipeline",
          "mode": "name"
        },
        "messageType": "block",
        "blocksUi": "={\n  \"blocks\": [\n    {\n      \"type\": \"section\",\n      \"text\": {\n        \"type\": \"mrkdwn\",\n        \"text\": \":arrows_counterclockwise: *Deal Stage Changed*\\n*\" + $json.name + \"* moved to *\" + $json.stageName + \"*\\nAmount: $\" + $json.amount.toLocaleString() + \"\\nAccount: \" + $json.accountName + \"\\nOwner: \" + $json.ownerName\n      }\n    },\n    {\n      \"type\": \"context\",\n      \"elements\": [\n        {\n          \"type\": \"mrkdwn\",\n          \"text\": \"<https://YOUR_INSTANCE.my.salesforce.com/\" + $json.id + \"|View in Salesforce>\"\n        }\n      ]\n    }\n  ]\n}",
        "otherOptions": {
          "unfurl_links": false
        }
      },
      "id": "post-to-slack",
      "name": "Post to Slack",
      "type": "n8n-nodes-base.slack",
      "typeVersion": 2.3,
      "position": [880, 0],
      "credentials": {
        "slackApi": {
          "id": "credential-id",
          "name": "Slack API"
        }
      }
    }
  ],
  "connections": {
    "Salesforce Trigger": {
      "main": [
        [
          {
            "node": "Stage Changed?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Stage Changed?": {
      "main": [
        [
          {
            "node": "Fetch Opportunity",
            "type": "main",
            "index": 0
          }
        ],
        []
      ]
    },
    "Fetch Opportunity": {
      "main": [
        [
          {
            "node": "Format Data",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Format Data": {
      "main": [
        [
          {
            "node": "Post to Slack",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "pinData": {},
  "settings": {
    "executionOrder": "v1"
  },
  "staticData": null,
  "tags": [],
  "triggerCount": 0,
  "active": false,
  "meta": {
    "instanceId": "",
    "templateId": "revi-sf-deal-stage-alert"
  }
}
