{
  "id": "WBzBEJLy6v8Ow7il",
  "name": "Advanced AI based next step generator for salesforce opportunities",
  "tags": [],
  "nodes": [
    {
      "id": "f755c065-87fc-473a-8c98-e5a5b0b4ed29",
      "name": "Reverse opportunities",
      "type": "n8n-nodes-base.code",
      "position": [
        -672,
        224
      ],
      "parameters": {
        "jsCode": "let res = $input.all().reverse()\n\nreturn res"
      },
      "typeVersion": 2
    },
    {
      "id": "952f061c-d6c0-4cb3-b3b4-6705330e9944",
      "name": "Get required fields",
      "type": "n8n-nodes-base.set",
      "position": [
        112,
        208
      ],
      "parameters": {
        "options": {},
        "assignments": {
          "assignments": [
            {
              "id": "68f731ed-3c35-496a-9ff9-ac58e768b0c2",
              "name": "Id",
              "type": "string",
              "value": "={{ $json.Id }}"
            },
            {
              "id": "db962d5c-138f-49d0-ba6e-418c54c40476",
              "name": "Name",
              "type": "string",
              "value": "={{ $json.Name }}"
            },
            {
              "id": "3fb8b711-c71d-4bd0-a5b6-3e54890c2ca5",
              "name": "StageName",
              "type": "string",
              "value": "={{ $json.StageName }}"
            },
            {
              "id": "afd30a5d-163b-4917-8c5b-2bfb6635cb05",
              "name": "Amount",
              "type": "number",
              "value": "={{ $json.Amount }}"
            },
            {
              "id": "29388606-74dc-4022-b5d0-ddff3468aad2",
              "name": "Probability",
              "type": "number",
              "value": "={{ $json.Probability }}"
            },
            {
              "id": "9e55b80f-a34c-4876-b71b-9e8b2a877b93",
              "name": "CloseDate",
              "type": "string",
              "value": "={{ $json.CloseDate }}"
            },
            {
              "id": "3f3f0c8c-9f77-431d-a66c-c24a3d51efb1",
              "name": "Description",
              "type": "string",
              "value": "={{ $json.Description }}"
            },
            {
              "id": "1b7b75f4-2fa9-4b7a-8b4a-941b2ea17853",
              "name": "OwnerId",
              "type": "string",
              "value": "={{ $json.OwnerId }}"
            },
            {
              "id": "a0511b9a-6dc8-4711-9095-216ed62c3ed8",
              "name": "ExpectedRevenue",
              "type": "number",
              "value": "={{ $json.ExpectedRevenue }}"
            },
            {
              "id": "39243984-2442-402b-a827-a119d7aa1409",
              "name": "Type",
              "type": "string",
              "value": "={{ $json.Type }}"
            },
            {
              "id": "25bdc9f9-913b-4f9c-b6a4-b09896aef4e7",
              "name": "LeadSource",
              "type": "string",
              "value": "={{ $json.LeadSource }}"
            },
            {
              "id": "cf36f9cb-3896-4217-88a4-7032e27a8f0b",
              "name": "FiscalYear",
              "type": "number",
              "value": "={{ $json.FiscalYear }}"
            },
            {
              "id": "c7c37ca3-b101-4651-a92d-8a0f440a0aa6",
              "name": "DeliveryInstallationStatus__c",
              "type": "string",
              "value": "={{ $json.DeliveryInstallationStatus__c }}"
            },
            {
              "id": "607b621d-267d-4ee9-9603-21a99dbd158a",
              "name": "NextStep",
              "type": "string",
              "value": "={{ $json.NextStep }}"
            }
          ]
        }
      },
      "typeVersion": 3.4
    },
    {
      "id": "a42621a9-9a39-4d7d-975a-7b48e42ba86c",
      "name": "Get the open opportunities",
      "type": "n8n-nodes-base.filter",
      "position": [
        336,
        208
      ],
      "parameters": {
        "options": {},
        "conditions": {
          "options": {
            "version": 3,
            "leftValue": "",
            "caseSensitive": true,
            "typeValidation": "strict"
          },
          "combinator": "and",
          "conditions": [
            {
              "id": "1b902645-3886-4bc2-b6ca-e8286a6787d7",
              "operator": {
                "type": "dateTime",
                "operation": "afterOrEquals"
              },
              "leftValue": "={{ $json.CloseDate }}",
              "rightValue": "={{ $now.format('yyyy-MM-dd') }}"
            },
            {
              "id": "a6cdaa9f-6a60-47cb-841e-f6d140da6202",
              "operator": {
                "type": "string",
                "operation": "notEquals"
              },
              "leftValue": "={{ $json.StageName }}",
              "rightValue": "Closed Won"
            },
            {
              "id": "d39d5a2c-9f8c-420d-a52e-3fe6142d1d5f",
              "operator": {
                "type": "string",
                "operation": "notEquals"
              },
              "leftValue": "={{ $json.StageName }}",
              "rightValue": "Closed Lost"
            },
            {
              "id": "f15da9e8-17d0-4bd7-958a-bd17caccfcbe",
              "operator": {
                "type": "string",
                "operation": "equals"
              },
              "leftValue": "={{ $json.NextStep }}",
              "rightValue": "={{ null }}"
            }
          ]
        }
      },
      "typeVersion": 2.3
    },
    {
      "id": "3f3ea8d9-3b23-477b-880c-20fef8fb1e34",
      "name": "Create Prompt for each opportunity",
      "type": "n8n-nodes-base.code",
      "position": [
        704,
        208
      ],
      "parameters": {
        "jsCode": "// Prepare data for AI\nreturn $input.all().map(item => {\n    const opp = item.json;\n    \n    // Create a prompt for OpenAI\n    const prompt = `\n      Act as an AI sales assistant. Based on the following Salesforce opportunity details, suggest the next best action in 1-2 sentences and format it as a message ready to send via Slack:\n\n      Opportunity Id: ${opp.Id}\n      Opportunity Name: ${opp.Name}\n      StageName: ${opp.StageName}\n      Amount: ${opp.Amount}\n      Close Date: ${opp.CloseDate || 'N/A'}\n      Owner ID: ${opp.OwnerId}\n      Description: ${opp.Description}\n      Probability: ${opp.Probability}\n      ExpectedRevenue: ${opp.ExpectedRevenue}\n      Type: ${opp.Type}\n      DeliveryInstallationStatus__c: ${opp.DeliveryInstallationStatus__c}\n      LeadSource: ${opp.LeadSource}\n      \n      Provide your recommendation in 1-2 sentences.\n      Use slack tool to send message\n      Use salesforce tool to update the next step by using the Opportunity id.\n`;\n\n    return {\n        json: {\n            id: opp.Id,\n            prompt: prompt\n        }\n    };\n});"
      },
      "typeVersion": 2
    },
    {
      "id": "999a1bef-3b65-4340-ad09-320adf919b77",
      "name": "Fetch opportunity data in batch of 10",
      "type": "n8n-nodes-base.splitInBatches",
      "position": [
        -448,
        224
      ],
      "parameters": {
        "options": {},
        "batchSize": 10
      },
      "typeVersion": 3
    },
    {
      "id": "83fcd45c-a399-4204-a16e-7a754a941723",
      "name": "Get an opportunity data",
      "type": "n8n-nodes-base.salesforce",
      "position": [
        -176,
        304
      ],
      "parameters": {
        "resource": "opportunity",
        "operation": "get",
        "opportunityId": "={{ $json.Id }}"
      },
      "credentials": {
        "salesforceOAuth2Api": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 1
    },
    {
      "id": "3b82514b-daf9-42fe-9c2b-b126bfaaf914",
      "name": "Get & update next step in batch of 1",
      "type": "n8n-nodes-base.splitInBatches",
      "position": [
        1216,
        208
      ],
      "parameters": {
        "options": {}
      },
      "typeVersion": 3
    },
    {
      "id": "3ee2020e-9691-4a88-a083-b9a75daa48e5",
      "name": "Execute every day - 10AM",
      "type": "n8n-nodes-base.scheduleTrigger",
      "position": [
        -1216,
        224
      ],
      "parameters": {
        "rule": {
          "interval": [
            {
              "triggerAtHour": 10
            }
          ]
        }
      },
      "typeVersion": 1.3
    },
    {
      "id": "50185f38-46d2-4f86-92d2-45d1af172f7a",
      "name": "Fetch all opportunities",
      "type": "n8n-nodes-base.salesforce",
      "position": [
        -976,
        224
      ],
      "parameters": {
        "options": {},
        "resource": "opportunity",
        "operation": "getAll",
        "returnAll": true
      },
      "credentials": {
        "salesforceOAuth2Api": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 1
    },
    {
      "id": "db849217-7b4a-43e9-a56b-f56bb7c858fd",
      "name": "Next step update - Agent",
      "type": "@n8n/n8n-nodes-langchain.agent",
      "position": [
        1760,
        224
      ],
      "parameters": {
        "text": "={{ $json.prompt }}",
        "options": {},
        "promptType": "define"
      },
      "typeVersion": 3.1
    },
    {
      "id": "887d2b57-f51f-4f1e-9e7c-ca57dcffae26",
      "name": "Gemini",
      "type": "@n8n/n8n-nodes-langchain.lmChatGoogleGemini",
      "position": [
        1728,
        448
      ],
      "parameters": {
        "options": {},
        "modelName": "models/gemini-3.1-flash-lite-preview"
      },
      "credentials": {
        "googlePalmApi": {
          "name": "<your credential>"
        }
      },
      "executeOnce": true,
      "typeVersion": 1
    },
    {
      "id": "ba4006a7-2090-4d0b-b8b6-05bc4e9e943f",
      "name": "Slack",
      "type": "n8n-nodes-base.slackTool",
      "position": [
        1888,
        448
      ],
      "parameters": {
        "text": "={{ /*n8n-auto-generated-fromAI-override*/ $fromAI('Message_Text', ``, 'string') }}",
        "select": "channel",
        "channelId": {
          "__rl": true,
          "mode": "list",
          "value": "C0ARSMU9QHL",
          "cachedResultName": "ai-based-next-best-action-recommender"
        },
        "otherOptions": {}
      },
      "credentials": {
        "slackApi": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 2.4
    },
    {
      "id": "bf883d30-e55d-438f-a7a1-cd8c33652a97",
      "name": "Update an opportunity in Salesforce",
      "type": "n8n-nodes-base.salesforceTool",
      "position": [
        2048,
        432
      ],
      "parameters": {
        "resource": "opportunity",
        "operation": "update",
        "updateFields": {
          "nextStep": "={{ /*n8n-auto-generated-fromAI-override*/ $fromAI('Next_Step', ``, 'string') }}"
        },
        "opportunityId": "={{ /*n8n-auto-generated-fromAI-override*/ $fromAI('Opportunity_ID', ``, 'string') }}"
      },
      "credentials": {
        "salesforceOAuth2Api": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 1
    },
    {
      "id": "02afbe13-d7ae-4482-a287-7ab6db23d21d",
      "name": "Sticky Note",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -1296,
        64
      ],
      "parameters": {
        "color": 7,
        "width": 528,
        "height": 544,
        "content": "## Daily Opportunity Sync\n- Triggers the workflow automatically every day at 10 AM\n- Fetches all opportunity records from Salesforce for processing"
      },
      "typeVersion": 1
    },
    {
      "id": "dd2ed48a-bfa8-4ae7-a331-963d2739b953",
      "name": "Sticky Note1",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -736,
        64
      ],
      "parameters": {
        "color": 7,
        "width": 752,
        "height": 544,
        "content": "## Batch Processing & Data Enrichment\n- Reverses opportunity list and processes them in batches of 10\n- Fetches detailed data for each opportunity individually"
      },
      "typeVersion": 1
    },
    {
      "id": "ca0274b7-f712-4f2f-ab95-76a1d24daeeb",
      "name": "Sticky Note2",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        48,
        64
      ],
      "parameters": {
        "color": 7,
        "width": 480,
        "height": 544,
        "content": "## Filter Relevant Opportunities\n- Extracts only required fields from opportunity data\n- Filters only open opportunities with future close dates and no next step"
      },
      "typeVersion": 1
    },
    {
      "id": "a4788318-421a-4e49-b3af-4cfbf52e6743",
      "name": "Sticky Note3",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        560,
        64
      ],
      "parameters": {
        "color": 7,
        "width": 464,
        "height": 544,
        "content": "## AI Prompt Generation\n- Converts each opportunity into a structured AI prompt\n- Includes all key sales details for generating next best action"
      },
      "typeVersion": 1
    },
    {
      "id": "52c8bcf2-1687-4092-84d6-3fb69b793388",
      "name": "Sticky Note4",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        1056,
        64
      ],
      "parameters": {
        "color": 7,
        "width": 512,
        "height": 544,
        "content": "## Controlled Sequential Execution\n- Processes opportunities one at a time to avoid rate limits\n- Ensures reliable execution of AI and update operations"
      },
      "typeVersion": 1
    },
    {
      "id": "29d4ec85-5e8a-43dc-9f37-bcb14955ca72",
      "name": "Sticky Note5",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        1600,
        64
      ],
      "parameters": {
        "color": 7,
        "width": 672,
        "height": 544,
        "content": "## AI Recommendation & Action\n- Generates next best action using AI and sends message to Slack\n- Updates the opportunity\u2019s next step in Salesforce automatically"
      },
      "typeVersion": 1
    },
    {
      "id": "e5858bc0-1b78-4cb8-8c51-b6ef1c0346f6",
      "name": "Sticky Note6",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -2144,
        -496
      ],
      "parameters": {
        "width": 832,
        "height": 544,
        "content": "# AI-Powered Salesforce Opportunity Next-Step Generator\n\nThis workflow automatically analyzes Salesforce Opportunities and generates AI-driven next-step recommendations, then updates Salesforce and notifies the sales team via Slack.\n\n### How it works\n\nEvery day at 10 AM, the workflow fetches all Salesforce Opportunities, reverses the list for balanced processing, and processes them in batches of 10. Each Opportunity is enriched with full details, then filtered to include only active records (not Closed Won/Lost), with future close dates and missing Next Step values. For each valid Opportunity, a structured prompt is generated and sent to a Gemini AI agent. The agent produces a concise 1\u20132 sentence recommendation, which is then posted to a Slack channel and used to update the Opportunity\u2019s Next Step field in Salesforce.\n\n### Setup steps\n\n1. Connect Salesforce OAuth credentials with read/write access to Opportunities\n2. Configure Slack credentials and select the target channel\n3. Add and validate Gemini (Google AI) API credentials\n4. Ensure Opportunity object permissions allow updates to the Next Step field\n5. Activate the workflow schedule trigger (daily at 10 AM)\n\nOnce enabled, this automation continuously improves pipeline visibility, ensures timely follow-ups, and reduces missed sales actions.\n"
      },
      "typeVersion": 1
    }
  ],
  "active": false,
  "settings": {
    "availableInMCP": false,
    "executionOrder": "v1"
  },
  "versionId": "3916e663-5305-4e5e-a881-d820e0ca714e",
  "connections": {
    "Slack": {
      "ai_tool": [
        [
          {
            "node": "Next step update - Agent",
            "type": "ai_tool",
            "index": 0
          }
        ]
      ]
    },
    "Gemini": {
      "ai_languageModel": [
        [
          {
            "node": "Next step update - Agent",
            "type": "ai_languageModel",
            "index": 0
          }
        ]
      ]
    },
    "Get required fields": {
      "main": [
        [
          {
            "node": "Get the open opportunities",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Reverse opportunities": {
      "main": [
        [
          {
            "node": "Fetch opportunity data in batch of 10",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Fetch all opportunities": {
      "main": [
        [
          {
            "node": "Reverse opportunities",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Get an opportunity data": {
      "main": [
        [
          {
            "node": "Fetch opportunity data in batch of 10",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Execute every day - 10AM": {
      "main": [
        [
          {
            "node": "Fetch all opportunities",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Next step update - Agent": {
      "main": [
        [
          {
            "node": "Get & update next step in batch of 1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Get the open opportunities": {
      "main": [
        [
          {
            "node": "Create Prompt for each opportunity",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Create Prompt for each opportunity": {
      "main": [
        [
          {
            "node": "Get & update next step in batch of 1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Update an opportunity in Salesforce": {
      "ai_tool": [
        [
          {
            "node": "Next step update - Agent",
            "type": "ai_tool",
            "index": 0
          }
        ]
      ]
    },
    "Get & update next step in batch of 1": {
      "main": [
        [],
        [
          {
            "node": "Next step update - Agent",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Fetch opportunity data in batch of 10": {
      "main": [
        [
          {
            "node": "Get required fields",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Get an opportunity data",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}