{
  "name": "Approvals waiting too long in Airtable",
  "nodes": [
    {
      "id": "a482cf3e-6194-4e48-8209-0fb6e821620f",
      "name": "Read me first",
      "type": "n8n-nodes-base.stickyNote",
      "typeVersion": 1,
      "position": [
        -688,
        -120
      ],
      "parameters": {
        "content": "## Approvals waiting too long in Airtable\nEvery hour it finds Airtable records that waited too long for a decision, marks them Overdue and sends you one reminder. It reminds again only after a day.\n\n### You need\n- An Airtable login and a table with: Status, Submitted At, SLA Status, Last Reminder, plus your own Title, Client and Owner fields\n- A Telegram bot and your chat ID\n\n### Set it up\n1. Add your Airtable login to **Find waiting records** and **Mark it overdue**. Pick your base and table in each.\n2. In **Settings**, set the hours a decision may take, how often to remind, and your chat ID.\n3. If your waiting status has another name, change it in **Find waiting records**.\n4. Failure alert: put your chat ID in **Write the failure alert** and your bot in **Send the failure alert**. Then in Workflow settings, set Error workflow to this workflow.\n\n### Test it\n- Set one record to Waiting approval with Submitted At two days ago. Click Test workflow.\n- The record gets SLA Status Overdue and you get a reminder. Run it again: no second reminder today.",
        "height": 780,
        "width": 560,
        "color": 4
      }
    },
    {
      "id": "1b7455dd-19d4-474e-822a-02b5962bf89a",
      "name": "Note: find",
      "type": "n8n-nodes-base.stickyNote",
      "typeVersion": 1,
      "position": [
        -48,
        -120
      ],
      "parameters": {
        "content": "## Find the slow ones\nThe wait counts from Submitted At, or from when the record was made. Records reminded in the last day are skipped.",
        "height": 560,
        "width": 1000,
        "color": 7
      }
    },
    {
      "id": "f04802eb-2d84-4627-8785-2752b6ceab56",
      "name": "Note: remind",
      "type": "n8n-nodes-base.stickyNote",
      "typeVersion": 1,
      "position": [
        1032,
        -120
      ],
      "parameters": {
        "content": "## Mark and remind\nThe record gets SLA Status Overdue and the time of the reminder. You get one message for all of them.",
        "height": 760,
        "width": 1000,
        "color": 7
      }
    },
    {
      "id": "321cb2db-0a1a-495e-afa4-9b0aa25981da",
      "name": "Every hour",
      "type": "n8n-nodes-base.scheduleTrigger",
      "typeVersion": 1.3,
      "position": [
        0,
        0
      ],
      "parameters": {
        "rule": {
          "interval": [
            {
              "field": "hours",
              "hoursInterval": 1
            }
          ]
        }
      }
    },
    {
      "id": "81401719-305d-40a8-9311-b1ed20d600ec",
      "name": "Check now",
      "type": "n8n-nodes-base.manualTrigger",
      "typeVersion": 1,
      "position": [
        0,
        200
      ],
      "parameters": {}
    },
    {
      "id": "cf1cc4e4-9cc6-4c6b-a13f-562e9faa4290",
      "name": "Settings",
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.4,
      "position": [
        240,
        100
      ],
      "parameters": {
        "assignments": {
          "assignments": [
            {
              "id": "f1",
              "name": "slaHours",
              "type": "number",
              "value": 24
            },
            {
              "id": "f2",
              "name": "remindEveryHours",
              "type": "number",
              "value": 24
            },
            {
              "id": "f3",
              "name": "telegramChatId",
              "type": "string",
              "value": "YOUR_CHAT_ID"
            }
          ]
        },
        "includeOtherFields": true,
        "options": {}
      }
    },
    {
      "id": "7685087d-cd07-4783-bca2-52bf07d3db81",
      "name": "Find waiting records",
      "type": "n8n-nodes-base.airtable",
      "typeVersion": 2.1,
      "position": [
        480,
        100
      ],
      "parameters": {
        "resource": "record",
        "operation": "search",
        "base": {
          "__rl": true,
          "value": "",
          "mode": "list"
        },
        "table": {
          "__rl": true,
          "value": "",
          "mode": "list"
        },
        "filterByFormula": "{Status} = 'Waiting approval'",
        "returnAll": true,
        "options": {}
      },
      "alwaysOutputData": true
    },
    {
      "id": "982c32fc-c44d-4de0-a18e-f2fd7b7691d0",
      "name": "Work out the wait",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        720,
        100
      ],
      "parameters": {
        "mode": "runOnceForAllItems",
        "jsCode": "const s = $('Settings').first().json;\nconst sla = Number(s.slaHours) || 24;\nconst every = Number(s.remindEveryHours) || 24;\nconst out = [];\nfor (const { json: r } of $input.all()) {\n  if (!r.id) continue;\n  const from = DateTime.fromISO(String(r['Submitted At'] || r.createdTime || ''));\n  if (!from.isValid) continue;\n  const hours = $now.diff(from, 'hours').hours;\n  if (hours <= sla) continue;\n  const last = DateTime.fromISO(String(r['Last Reminder'] || ''));\n  if (last.isValid && $now.diff(last, 'hours').hours < every) continue;\n  out.push({ json: { id: r.id, title: String(r.Title || r.Name || r.id), client: String(r.Client || ''), owner: String(r.Owner || ''), hours: Math.round(hours) } });\n}\nreturn out;"
      }
    },
    {
      "id": "af4bd610-df6f-487b-a2f9-cc07ceee0994",
      "name": "Mark it overdue",
      "type": "n8n-nodes-base.airtable",
      "typeVersion": 2.1,
      "position": [
        960,
        0
      ],
      "parameters": {
        "resource": "record",
        "operation": "update",
        "base": {
          "__rl": true,
          "value": "",
          "mode": "list"
        },
        "table": {
          "__rl": true,
          "value": "",
          "mode": "list"
        },
        "columns": {
          "mappingMode": "defineBelow",
          "value": {
            "id": "={{ $json.id }}",
            "SLA Status": "Overdue",
            "Last Reminder": "={{ $now.toISO() }}"
          },
          "matchingColumns": [
            "id"
          ],
          "schema": [
            {
              "id": "id",
              "displayName": "id",
              "required": false,
              "defaultMatch": true,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true,
              "removed": false,
              "readOnly": true
            },
            {
              "id": "SLA Status",
              "displayName": "SLA Status",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true,
              "removed": false
            },
            {
              "id": "Last Reminder",
              "displayName": "Last Reminder",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true,
              "removed": false
            }
          ],
          "attemptToConvertTypes": false,
          "convertFieldsToString": false
        },
        "options": {
          "typecast": true
        }
      }
    },
    {
      "id": "fdbd7158-e330-433a-925a-53c2d2f7aa78",
      "name": "Write the reminder",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        960,
        200
      ],
      "parameters": {
        "mode": "runOnceForAllItems",
        "jsCode": "const rows = $('Work out the wait').all().map(i => i.json).sort((a, b) => b.hours - a.hours);\nconst lines = rows.slice(0, 15).map(r => '- ' + r.title + (r.client ? ' (' + r.client + ')' : '') + ': waiting ' + r.hours + ' hours' + (r.owner ? ', ' + r.owner : ''));\nreturn [{ json: { message: 'Waiting too long for a decision: ' + rows.length + '\\n\\n' + lines.join('\\n') + (rows.length > 15 ? '\\n...and ' + (rows.length - 15) + ' more' : '') } }];"
      }
    },
    {
      "id": "98b93e56-3374-4aa6-b45d-8ec7685b4c62",
      "name": "Send the reminder",
      "type": "n8n-nodes-base.telegram",
      "typeVersion": 1.2,
      "position": [
        1200,
        200
      ],
      "parameters": {
        "chatId": "={{ $('Settings').first().json.telegramChatId }}",
        "text": "={{ $json.message }}",
        "additionalFields": {
          "appendAttribution": false
        }
      }
    },
    {
      "id": "5cb892e2-194a-4c89-8e21-80b1504cc1af",
      "name": "When this workflow fails",
      "type": "n8n-nodes-base.errorTrigger",
      "typeVersion": 1,
      "position": [
        -648,
        740
      ],
      "parameters": {}
    },
    {
      "id": "42c7c0fc-63e6-4303-82d9-d6ccfae5f6a9",
      "name": "Write the failure alert",
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.4,
      "position": [
        -428,
        740
      ],
      "parameters": {
        "assignments": {
          "assignments": [
            {
              "id": "f1",
              "name": "telegramChatId",
              "type": "string",
              "value": "YOUR_TELEGRAM_CHAT_ID"
            },
            {
              "id": "f2",
              "name": "message",
              "type": "string",
              "value": "={{ 'A workflow stopped.\\n\\nWorkflow: ' + (($json.workflow || {}).name || 'unknown') + '\\nFailed step: ' + (($json.execution || {}).lastNodeExecuted || 'unknown') + '\\nWhy: ' + String((($json.execution || {}).error || {}).message || 'n8n did not give a reason.').slice(0, 300) + (($json.execution || {}).url ? '\\nOpen the run: ' + $json.execution.url : '') }}"
            }
          ]
        },
        "includeOtherFields": false,
        "options": {}
      }
    },
    {
      "id": "251d9444-58be-4026-b48d-bfd2900a6569",
      "name": "Send the failure alert",
      "type": "n8n-nodes-base.telegram",
      "typeVersion": 1.2,
      "position": [
        -208,
        740
      ],
      "parameters": {
        "chatId": "={{ $json.telegramChatId }}",
        "text": "={{ $json.message }}",
        "additionalFields": {
          "appendAttribution": false
        }
      }
    }
  ],
  "connections": {
    "Every hour": {
      "main": [
        [
          {
            "node": "Settings",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Check now": {
      "main": [
        [
          {
            "node": "Settings",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Settings": {
      "main": [
        [
          {
            "node": "Find waiting records",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Find waiting records": {
      "main": [
        [
          {
            "node": "Work out the wait",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Work out the wait": {
      "main": [
        [
          {
            "node": "Mark it overdue",
            "type": "main",
            "index": 0
          },
          {
            "node": "Write the reminder",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Write the reminder": {
      "main": [
        [
          {
            "node": "Send the reminder",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "When this workflow fails": {
      "main": [
        [
          {
            "node": "Write the failure alert",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Write the failure alert": {
      "main": [
        [
          {
            "node": "Send the failure alert",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "pinData": {},
  "active": false,
  "settings": {
    "executionOrder": "v1"
  },
  "tags": []
}
