{
  "name": "Weekly Salesforce Pipeline Report",
  "nodes": [
    {
      "parameters": {
        "rule": {
          "interval": [
            {
              "triggerAtDay": 1,
              "triggerAtHour": 8,
              "triggerAtMinute": 0
            }
          ]
        }
      },
      "id": "schedule-trigger",
      "name": "Schedule Trigger",
      "type": "n8n-nodes-base.scheduleTrigger",
      "typeVersion": 1.2,
      "position": [0, 0]
    },
    {
      "parameters": {
        "method": "GET",
        "url": "https://YOUR_INSTANCE.my.salesforce.com/services/data/v59.0/query",
        "authentication": "predefinedCredentialType",
        "nodeCredentialType": "salesforceOAuth2Api",
        "sendQuery": true,
        "queryParameters": {
          "parameters": [
            {
              "name": "q",
              "value": "SELECT StageName, COUNT(Id) deal_count, SUM(Amount) total_amount FROM Opportunity WHERE IsClosed = false AND Amount != null GROUP BY StageName ORDER BY SUM(Amount) DESC"
            }
          ]
        },
        "options": {}
      },
      "id": "open-pipeline",
      "name": "Open Pipeline",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [220, -120],
      "credentials": {
        "salesforceOAuth2Api": {
          "id": "credential-id",
          "name": "Salesforce OAuth2"
        }
      }
    },
    {
      "parameters": {
        "method": "GET",
        "url": "https://YOUR_INSTANCE.my.salesforce.com/services/data/v59.0/query",
        "authentication": "predefinedCredentialType",
        "nodeCredentialType": "salesforceOAuth2Api",
        "sendQuery": true,
        "queryParameters": {
          "parameters": [
            {
              "name": "q",
              "value": "SELECT COUNT(Id) closed_count, SUM(Amount) closed_amount FROM Opportunity WHERE CloseDate = THIS_WEEK AND IsWon = true"
            }
          ]
        },
        "options": {}
      },
      "id": "closed-this-week",
      "name": "Closed This Week",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [220, 0],
      "credentials": {
        "salesforceOAuth2Api": {
          "id": "credential-id",
          "name": "Salesforce OAuth2"
        }
      }
    },
    {
      "parameters": {
        "method": "GET",
        "url": "https://YOUR_INSTANCE.my.salesforce.com/services/data/v59.0/query",
        "authentication": "predefinedCredentialType",
        "nodeCredentialType": "salesforceOAuth2Api",
        "sendQuery": true,
        "queryParameters": {
          "parameters": [
            {
              "name": "q",
              "value": "SELECT COUNT(Id) new_count, SUM(Amount) new_amount FROM Opportunity WHERE CreatedDate = THIS_WEEK"
            }
          ]
        },
        "options": {}
      },
      "id": "new-this-week",
      "name": "New This Week",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [220, 120],
      "credentials": {
        "salesforceOAuth2Api": {
          "id": "credential-id",
          "name": "Salesforce OAuth2"
        }
      }
    },
    {
      "parameters": {
        "mode": "runOnceForAllItems",
        "jsCode": "const pipeline = $('Open Pipeline').first().json.records;\nconst closed = $('Closed This Week').first().json.records[0];\nconst newDeals = $('New This Week').first().json.records[0];\n\nconst totalPipeline = pipeline.reduce((sum, s) => sum + (s.total_amount || 0), 0);\nconst totalDeals = pipeline.reduce((sum, s) => sum + s.deal_count, 0);\nconst avgDealSize = totalDeals > 0 ? totalPipeline / totalDeals : 0;\n\nconst stageBreakdown = pipeline.map(s =>\n  `\u2022 *${s.StageName}*: ${s.deal_count} deals \u2014 $${(s.total_amount || 0).toLocaleString()}`\n).join('\\n');\n\nreturn [{\n  json: {\n    totalPipeline,\n    totalDeals,\n    avgDealSize,\n    closedCount: closed?.closed_count || 0,\n    closedAmount: closed?.closed_amount || 0,\n    newCount: newDeals?.new_count || 0,\n    newAmount: newDeals?.new_amount || 0,\n    stageBreakdown,\n  }\n}];"
      },
      "id": "calc-metrics",
      "name": "Calculate Metrics",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [440, 0]
    },
    {
      "parameters": {
        "select": "channel",
        "channelId": {
          "__rl": true,
          "value": "sales-pipeline",
          "mode": "name"
        },
        "messageType": "block",
        "blocksUi": "={\n  \"blocks\": [\n    {\n      \"type\": \"header\",\n      \"text\": {\n        \"type\": \"plain_text\",\n        \"text\": \"\ud83d\udcca Weekly Pipeline Report\"\n      }\n    },\n    {\n      \"type\": \"section\",\n      \"fields\": [\n        {\n          \"type\": \"mrkdwn\",\n          \"text\": \"*Total Pipeline*\\n$\" + $json.totalPipeline.toLocaleString()\n        },\n        {\n          \"type\": \"mrkdwn\",\n          \"text\": \"*Open Deals*\\n\" + $json.totalDeals\n        },\n        {\n          \"type\": \"mrkdwn\",\n          \"text\": \"*Avg Deal Size*\\n$\" + $json.avgDealSize.toLocaleString()\n        },\n        {\n          \"type\": \"mrkdwn\",\n          \"text\": \"*Closed This Week*\\n\" + $json.closedCount + \" \u2014 $\" + $json.closedAmount.toLocaleString()\n        },\n        {\n          \"type\": \"mrkdwn\",\n          \"text\": \"*New This Week*\\n\" + $json.newCount + \" \u2014 $\" + $json.newAmount.toLocaleString()\n        }\n      ]\n    },\n    {\n      \"type\": \"section\",\n      \"text\": {\n        \"type\": \"mrkdwn\",\n        \"text\": \"*By Stage*\\n\" + $json.stageBreakdown\n      }\n    }\n  ]\n}",
        "otherOptions": {
          "unfurl_links": false
        }
      },
      "id": "notify-slack",
      "name": "Post Report",
      "type": "n8n-nodes-base.slack",
      "typeVersion": 2.3,
      "position": [660, 0],
      "credentials": {
        "slackApi": {
          "id": "credential-id",
          "name": "Slack API"
        }
      }
    }
  ],
  "connections": {
    "Schedule Trigger": {
      "main": [
        [
          {
            "node": "Open Pipeline",
            "type": "main",
            "index": 0
          },
          {
            "node": "Closed This Week",
            "type": "main",
            "index": 0
          },
          {
            "node": "New This Week",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Open Pipeline": {
      "main": [
        [
          {
            "node": "Calculate Metrics",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Closed This Week": {
      "main": [
        [
          {
            "node": "Calculate Metrics",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "New This Week": {
      "main": [
        [
          {
            "node": "Calculate Metrics",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Calculate Metrics": {
      "main": [
        [
          {
            "node": "Post Report",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "pinData": {},
  "settings": {
    "executionOrder": "v1"
  },
  "staticData": null,
  "tags": [],
  "triggerCount": 0,
  "active": false,
  "meta": {
    "instanceId": "",
    "templateId": "revi-sf-pipeline-report"
  }
}
