AutomationFlowsAI & RAG › AI Lead Scoring

AI Lead Scoring

03 - AI Lead Scoring. Uses mySql, httpRequest, chainLlm, lmChatOllama. Event-driven trigger; 10 nodes.

Event trigger★★★★☆ complexityAI-powered10 nodesMySQLHTTP RequestChain LlmOllama Chat
AI & RAG Trigger: Event Nodes: 10 Complexity: ★★★★☆ AI nodes: yes Added:

This workflow follows the Chainllm → HTTP Request recipe pattern — see all workflows that pair these two integrations.

The workflow JSON

Copy or download the full n8n JSON below. Paste it into a new n8n workflow, add your credentials, activate. Full import guide →

Download .json
{
  "name": "03 - AI Lead Scoring",
  "nodes": [
    {
      "parameters": {},
      "type": "n8n-nodes-base.manualTrigger",
      "typeVersion": 1,
      "position": [
        0,
        0
      ],
      "id": "64c1b257-13ab-43af-a14c-495877ec74ed",
      "name": "When clicking \u2018Execute workflow\u2019"
    },
    {
      "parameters": {
        "operation": "executeQuery",
        "query": "SELECT *\nFROM leads\nWHERE status = 'New'\nORDER BY id;",
        "options": {}
      },
      "type": "n8n-nodes-base.mySql",
      "typeVersion": 2.5,
      "position": [
        208,
        0
      ],
      "id": "4be95936-a530-42ba-8194-f2e2e7573f66",
      "name": "Execute a SQL query",
      "credentials": {
        "mySql": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "operation": "executeQuery",
        "query": "UPDATE leads\nSET\n  score = {{ $json.score }},\n  priority = '{{ $json.priority }}',\n  status = '{{ $json.status }}',\n  ai_reason = '{{ $json.reason }}'\nWHERE id = {{ $json.id }};",
        "options": {}
      },
      "type": "n8n-nodes-base.mySql",
      "typeVersion": 2.5,
      "position": [
        1056,
        288
      ],
      "id": "6503110d-7c0c-4e01-9a2c-95ecc6d4ffc2",
      "name": "Execute a SQL By AI",
      "credentials": {
        "mySql": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "options": {}
      },
      "type": "n8n-nodes-base.splitInBatches",
      "typeVersion": 3,
      "position": [
        432,
        -32
      ],
      "id": "a6d6cde7-e1de-4d6b-b325-6132fc34759b",
      "name": "Loop Over Items"
    },
    {
      "parameters": {
        "operation": "executeQuery",
        "query": "SELECT\n    (SELECT COUNT(*) FROM leads) AS totalLeads,\n    (SELECT COALESCE(SUM(score), 0) FROM leads) AS totalScore,\n\n    (SELECT COUNT(*) FROM leads WHERE status = 'Qualified') AS qualifiedCount,\n    (SELECT COUNT(*) FROM leads WHERE status = 'Review' OR status = 'New' ) AS reviewCount,\n    (SELECT COUNT(*) FROM leads WHERE status = 'Rejected') AS rejectedCount,\n\n    (SELECT COUNT(*) FROM leads WHERE `priority` = 'High') AS highCount,\n    (SELECT COUNT(*) FROM leads WHERE `priority` = 'Medium') AS mediumCount,\n    (SELECT COUNT(*) FROM leads WHERE `priority` = 'Low') AS lowCount;",
        "options": {}
      },
      "type": "n8n-nodes-base.mySql",
      "typeVersion": 2.5,
      "position": [
        640,
        -128
      ],
      "id": "e61cd1fa-6bec-4991-9c1d-44e58f792f7e",
      "name": "Execute a SQL if Done Loop",
      "credentials": {
        "mySql": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "method": "POST",
        "url": "http://crm-ollama:11434/api/generate",
        "sendBody": true,
        "contentType": "raw",
        "rawContentType": "application/json",
        "body": "={{ JSON.stringify({\n  model: \"dolphin-llama3:8b\",\n  stream: false,\n  prompt: `You are an AI CRM analyst.\n\nCreate a short business summary.\n\nStatistics\n\nTotal Leads: ${$json.totalLeads}\nQualified: ${$json.qualifiedCount}\nReview: ${$json.reviewCount}\nRejected: ${$json.rejectedCount}\nHigh Priority: ${$json.highCount}\nMedium Priority: ${$json.mediumCount}\nLow Priority: ${$json.lowCount}\n\nAverage Score: ${\n  Number($json.totalScore) / Number($json.totalLeads)\n}\n\nReturn ONLY plain text.`\n}) }}",
        "options": {}
      },
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.4,
      "position": [
        848,
        -128
      ],
      "id": "61744668-5e2c-4a0c-ba9c-a0e561fc1444",
      "name": "Connect Ai: Review"
    },
    {
      "parameters": {
        "operation": "executeQuery",
        "query": "INSERT INTO activity_logs\n(\n    lead_id,\n    activity\n)\nVALUES\n(\n    NULL,\n    '{{$json.response}}'\n);",
        "options": {}
      },
      "type": "n8n-nodes-base.mySql",
      "typeVersion": 2.5,
      "position": [
        1056,
        -128
      ],
      "id": "ffaa5c7e-01ac-448c-9ab2-31a7d0987f1d",
      "name": "Execute a SQL activity_logs",
      "credentials": {
        "mySql": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "promptType": "define",
        "text": "=You are an AI Lead Scoring Assistant.\n\nAnalyze the lead below.\n\nReturn ONLY valid JSON.\n\n{\n  \"id\": same id from the input,\n  \"score\": integer (0-100),\n  \"priority\": \"High\" | \"Medium\" | \"Low\",\n  \"status\": \"Qualified\" | \"Review\" | \"Rejected\",\n  \"reason\": \"One short sentence\"\n}\n\nLead:\n{{ JSON.stringify($json) }}",
        "batching": {}
      },
      "type": "@n8n/n8n-nodes-langchain.chainLlm",
      "typeVersion": 1.9,
      "position": [
        544,
        144
      ],
      "id": "bf8e4c26-bc4a-4789-9478-0c33c8d3175c",
      "name": "Basic LLM Chain"
    },
    {
      "parameters": {
        "model": "dolphin-llama3:8b",
        "options": {}
      },
      "type": "@n8n/n8n-nodes-langchain.lmChatOllama",
      "typeVersion": 1,
      "position": [
        544,
        352
      ],
      "id": "1ed00752-5d87-4e2a-bf28-931a8e2d9a34",
      "name": "Ollama Chat Model",
      "credentials": {
        "ollamaApi": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "jsCode": "const text = $json.text;\n\nconst match = text.match(/\\{[\\s\\S]*\\}/);\n\nif (!match) {\n  throw new Error(\"No JSON found\");\n}\n\nreturn [{\n  json: JSON.parse(match[0])\n}];"
      },
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        864,
        176
      ],
      "id": "3dcd2f66-7bab-49da-92a1-87186b749d43",
      "name": "Get JSON data"
    }
  ],
  "connections": {
    "When clicking \u2018Execute workflow\u2019": {
      "main": [
        [
          {
            "node": "Execute a SQL query",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Execute a SQL query": {
      "main": [
        [
          {
            "node": "Loop Over Items",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Loop Over Items": {
      "main": [
        [
          {
            "node": "Execute a SQL if Done Loop",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Basic LLM Chain",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Execute a SQL By AI": {
      "main": [
        [
          {
            "node": "Loop Over Items",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Execute a SQL if Done Loop": {
      "main": [
        [
          {
            "node": "Connect Ai: Review",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Connect Ai: Review": {
      "main": [
        [
          {
            "node": "Execute a SQL activity_logs",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Basic LLM Chain": {
      "main": [
        [
          {
            "node": "Get JSON data",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Ollama Chat Model": {
      "ai_languageModel": [
        [
          {
            "node": "Basic LLM Chain",
            "type": "ai_languageModel",
            "index": 0
          }
        ]
      ]
    },
    "Get JSON data": {
      "main": [
        [
          {
            "node": "Execute a SQL By AI",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "active": false,
  "settings": {
    "executionOrder": "v1",
    "binaryMode": "separate",
    "availableInMCP": false
  },
  "versionId": "b56c5f41-861e-4635-a000-d5d755e0d87c",
  "meta": {
    "templateCredsSetupCompleted": true
  },
  "nodeGroups": [],
  "id": "ej4IdJp62kRjHh4Y",
  "tags": []
}

Credentials you'll need

Each integration node will prompt for credentials when you import. We strip credential IDs before publishing — you'll add your own.

Pro

For the full experience including quality scoring and batch install features for each workflow upgrade to Pro

About this workflow

03 - AI Lead Scoring. Uses mySql, httpRequest, chainLlm, lmChatOllama. Event-driven trigger; 10 nodes.

Source: https://github.com/ibam28/ai-crm-automation/blob/main/workflows/ai-lead-scoring.json — original creator credit. Request a take-down →

More AI & RAG workflows → · Browse all categories →

Related workflows

Workflows that share integrations, category, or trigger type with this one. All free to copy and import.

AI & RAG

Episode 11: AI shorts factory app. Uses httpRequest, googleSheets, lmChatOpenAi, lmChatOllama. Event-driven trigger; 96 nodes.

HTTP Request, Google Sheets, OpenAI Chat +15
AI & RAG

My workflow 53. Uses formTrigger, httpRequest, lmChatOpenAi, form. Event-driven trigger; 74 nodes.

Form Trigger, HTTP Request, OpenAI Chat +15
AI & RAG

Episode 23: UGC with nanobanana. Uses lmChatOpenAi, lmChatOllama, lmChatDeepSeek, lmChatOpenRouter. Event-driven trigger; 74 nodes.

OpenAI Chat, Ollama Chat, Lm Chat Deep Seek +12
AI & RAG

Sourcing_Agent_LinkedIn_validator_production. Uses executeWorkflowTrigger, chainLlm, mySql, httpRequest. Event-driven trigger; 51 nodes.

Execute Workflow Trigger, Chain Llm, MySQL +9
AI & RAG

Sourcing_Agent_LinkedIn_validator_production. Uses executeWorkflowTrigger, chainLlm, mySql, httpRequest. Event-driven trigger; 51 nodes.

Execute Workflow Trigger, Chain Llm, MySQL +9