{
  "meta": {
    "templateCredsSetupCompleted": true
  },
  "nodes": [
    {
      "id": "2a5fe080-6be9-48f5-bf4b-170e710bc6fe",
      "name": "Schedule trigger (every minute)",
      "type": "n8n-nodes-base.scheduleTrigger",
      "position": [
        -1056,
        800
      ],
      "parameters": {
        "rule": {
          "interval": [
            {
              "field": "minutes"
            }
          ]
        }
      },
      "typeVersion": 1.3
    },
    {
      "id": "d7e99f65-8311-416c-b6c4-ff295f2255f2",
      "name": "Google Sheets: Get leads with status CREATED",
      "type": "n8n-nodes-base.googleSheets",
      "position": [
        -848,
        800
      ],
      "parameters": {
        "options": {},
        "filtersUI": {
          "values": [
            {
              "lookupValue": "CREATED",
              "lookupColumn": "lead_status"
            }
          ]
        },
        "sheetName": {
          "__rl": true,
          "mode": "list",
          "value": 78844591,
          "cachedResultUrl": "https://docs.google.com/spreadsheets/d/1lrcwNWGWFHBAYuOwwXco6DFPYv1PMSwuR2zYv93wF0o/edit#gid=78844591",
          "cachedResultName": "Sheet1"
        },
        "documentId": {
          "__rl": true,
          "mode": "url",
          "value": "your facebook leads google sheets url"
        }
      },
      "credentials": {
        "googleSheetsOAuth2Api": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 4.7
    },
    {
      "id": "c04659e3-0f2b-499e-9a99-974037f33925",
      "name": "Split in batches",
      "type": "n8n-nodes-base.splitInBatches",
      "position": [
        -624,
        800
      ],
      "parameters": {
        "options": {}
      },
      "typeVersion": 3
    },
    {
      "id": "ae44b21a-bd16-4ae8-963a-0f947188d0f1",
      "name": "Perfex: Search lead by email",
      "type": "n8n-nodes-base.httpRequest",
      "onError": "continueErrorOutput",
      "position": [
        -400,
        800
      ],
      "parameters": {
        "url": "=https://crm.example.com/api/leads/search/{{ $json.email }}",
        "options": {},
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "accept",
              "value": "application/json"
            },
            {
              "name": "authtoken",
              "value": "your api key"
            }
          ]
        }
      },
      "typeVersion": 4.3,
      "alwaysOutputData": false
    },
    {
      "id": "0fb10fa6-069f-47c5-af3b-3daa232dd36f",
      "name": "IF: Lead exists",
      "type": "n8n-nodes-base.if",
      "position": [
        -176,
        800
      ],
      "parameters": {
        "options": {},
        "conditions": {
          "options": {
            "version": 3,
            "leftValue": "",
            "caseSensitive": true,
            "typeValidation": "strict"
          },
          "combinator": "and",
          "conditions": [
            {
              "id": "ed6d22c3-b3f5-4720-865e-6a1ea40407e3",
              "operator": {
                "type": "string",
                "operation": "exists",
                "singleValue": true
              },
              "leftValue": "={{ $json.id }}",
              "rightValue": ""
            }
          ]
        }
      },
      "typeVersion": 2.3
    },
    {
      "id": "f48cc717-5d22-41e7-9148-26440e42de39",
      "name": "IF: Lead not found (404)",
      "type": "n8n-nodes-base.if",
      "position": [
        -544,
        1056
      ],
      "parameters": {
        "options": {},
        "conditions": {
          "options": {
            "version": 3,
            "leftValue": "",
            "caseSensitive": true,
            "typeValidation": "strict"
          },
          "combinator": "and",
          "conditions": [
            {
              "id": "30486a45-d591-4dec-a580-52be797b4b3c",
              "operator": {
                "type": "number",
                "operation": "equals"
              },
              "leftValue": "={{ $json.error.status }}",
              "rightValue": 404
            }
          ]
        }
      },
      "typeVersion": 2.3
    },
    {
      "id": "7e0eb121-540d-4147-9147-4bd3ec079a2f",
      "name": "Perfex: Create lead",
      "type": "n8n-nodes-base.httpRequest",
      "position": [
        -304,
        1040
      ],
      "parameters": {
        "url": "https://crm.example.com/api/leads",
        "method": "POST",
        "options": {},
        "sendBody": true,
        "contentType": "multipart-form-data",
        "sendHeaders": true,
        "bodyParameters": {
          "parameters": [
            {
              "name": "custom_fields",
              "value": "={\n  \"note\": true   \n}"
            },
            {
              "name": "name",
              "value": "={{ $json.full_name }}"
            },
            {
              "name": "assigned",
              "value": "5"
            },
            {
              "name": "status",
              "value": "1"
            },
            {
              "name": "phonenumber",
              "value": "={{$json.phone_number.replace('p:', '')}}"
            },
            {
              "name": "tags",
              "value": "={{ $json['what_are_you_looking_for?'] }}"
            },
            {
              "name": "source",
              "value": "={{ $json.platform === 'fb' ? 2 : 3 }}"
            },
            {
              "name": "description",
              "value": "={{ $json.Note }}"
            },
            {
              "name": "custom_contact_date",
              "value": "="
            },
            {
              "name": "file"
            },
            {
              "name": "email",
              "value": "={{ $json.email }}"
            },
            {
              "name": "company",
              "value": "={{ $json.company_name }}"
            }
          ]
        },
        "headerParameters": {
          "parameters": [
            {
              "name": "accept",
              "value": "application/json"
            },
            {
              "name": "authtoken",
              "value": "your api key"
            }
          ]
        }
      },
      "typeVersion": 4.3
    },
    {
      "id": "016bcf2a-6db8-4973-8a86-9af7c6ef8a79",
      "name": "Google Sheets: Mark as ADDED (existing)",
      "type": "n8n-nodes-base.googleSheets",
      "position": [
        48,
        800
      ],
      "parameters": {
        "columns": {
          "value": {
            "crm_id": "==HYPERLINK(\"https://crm.example.com/admin/leads/index/{{ $json.id }}\",\"{{ $json.id }}\")",
            "row_number": "={{ $('Split in batches').item.json.row_number }}",
            "lead_status": "ADDED"
          },
          "schema": [
            {
              "id": "id",
              "type": "string",
              "display": true,
              "removed": true,
              "required": false,
              "displayName": "id",
              "defaultMatch": true,
              "canBeUsedToMatch": true
            },
            {
              "id": "created_time",
              "type": "string",
              "display": true,
              "removed": true,
              "required": false,
              "displayName": "created_time",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "ad_id",
              "type": "string",
              "display": true,
              "removed": true,
              "required": false,
              "displayName": "ad_id",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "ad_name",
              "type": "string",
              "display": true,
              "removed": true,
              "required": false,
              "displayName": "ad_name",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "adset_id",
              "type": "string",
              "display": true,
              "removed": true,
              "required": false,
              "displayName": "adset_id",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "adset_name",
              "type": "string",
              "display": true,
              "removed": true,
              "required": false,
              "displayName": "adset_name",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "campaign_id",
              "type": "string",
              "display": true,
              "removed": true,
              "required": false,
              "displayName": "campaign_id",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "campaign_name",
              "type": "string",
              "display": true,
              "removed": true,
              "required": false,
              "displayName": "campaign_name",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "form_id",
              "type": "string",
              "display": true,
              "removed": true,
              "required": false,
              "displayName": "form_id",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "form_name",
              "type": "string",
              "display": true,
              "removed": true,
              "required": false,
              "displayName": "form_name",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "is_organic",
              "type": "string",
              "display": true,
              "removed": true,
              "required": false,
              "displayName": "is_organic",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "platform",
              "type": "string",
              "display": true,
              "removed": true,
              "required": false,
              "displayName": "platform",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "what_are_you_looking_for?",
              "type": "string",
              "display": true,
              "removed": true,
              "required": false,
              "displayName": "what_are_you_looking_for?",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "full_name",
              "type": "string",
              "display": true,
              "removed": true,
              "required": false,
              "displayName": "full_name",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "company_name",
              "type": "string",
              "display": true,
              "removed": true,
              "required": false,
              "displayName": "company_name",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "email",
              "type": "string",
              "display": true,
              "removed": true,
              "required": false,
              "displayName": "email",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "phone_number",
              "type": "string",
              "display": true,
              "removed": true,
              "required": false,
              "displayName": "phone_number",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "lead_status",
              "type": "string",
              "display": true,
              "removed": false,
              "required": false,
              "displayName": "lead_status",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "crm_id",
              "type": "string",
              "display": true,
              "removed": false,
              "required": false,
              "displayName": "crm_id",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "Note",
              "type": "string",
              "display": true,
              "removed": true,
              "required": false,
              "displayName": "Note",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "row_number",
              "type": "number",
              "display": true,
              "removed": false,
              "readOnly": true,
              "required": false,
              "displayName": "row_number",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            }
          ],
          "mappingMode": "defineBelow",
          "matchingColumns": [
            "row_number"
          ],
          "attemptToConvertTypes": false,
          "convertFieldsToString": false
        },
        "options": {},
        "operation": "update",
        "sheetName": {
          "__rl": true,
          "mode": "list",
          "value": 78844591,
          "cachedResultUrl": "your facebook leads google sheets url",
          "cachedResultName": "Sheet1"
        },
        "documentId": {
          "__rl": true,
          "mode": "url",
          "value": "your facebook leads google sheets url"
        }
      },
      "credentials": {
        "googleSheetsOAuth2Api": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 4.7
    },
    {
      "id": "3c52fcc3-3d3a-44e2-9c1a-e83410508453",
      "name": "Google Sheets: Mark as ADDED (new)",
      "type": "n8n-nodes-base.googleSheets",
      "position": [
        -96,
        1040
      ],
      "parameters": {
        "columns": {
          "value": {
            "crm_id": "==HYPERLINK(\"https://crm.example.com/admin/leads/index/{{ $json.record_id }}\",\"{{ $json.record_id }}\")",
            "row_number": "={{ $('Split in batches').item.json.row_number }}",
            "lead_status": "ADDED"
          },
          "schema": [
            {
              "id": "id",
              "type": "string",
              "display": true,
              "removed": true,
              "required": false,
              "displayName": "id",
              "defaultMatch": true,
              "canBeUsedToMatch": true
            },
            {
              "id": "created_time",
              "type": "string",
              "display": true,
              "removed": true,
              "required": false,
              "displayName": "created_time",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "ad_id",
              "type": "string",
              "display": true,
              "removed": true,
              "required": false,
              "displayName": "ad_id",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "ad_name",
              "type": "string",
              "display": true,
              "removed": true,
              "required": false,
              "displayName": "ad_name",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "adset_id",
              "type": "string",
              "display": true,
              "removed": true,
              "required": false,
              "displayName": "adset_id",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "adset_name",
              "type": "string",
              "display": true,
              "removed": true,
              "required": false,
              "displayName": "adset_name",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "campaign_id",
              "type": "string",
              "display": true,
              "removed": true,
              "required": false,
              "displayName": "campaign_id",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "campaign_name",
              "type": "string",
              "display": true,
              "removed": true,
              "required": false,
              "displayName": "campaign_name",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "form_id",
              "type": "string",
              "display": true,
              "removed": true,
              "required": false,
              "displayName": "form_id",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "form_name",
              "type": "string",
              "display": true,
              "removed": true,
              "required": false,
              "displayName": "form_name",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "is_organic",
              "type": "string",
              "display": true,
              "removed": true,
              "required": false,
              "displayName": "is_organic",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "platform",
              "type": "string",
              "display": true,
              "removed": true,
              "required": false,
              "displayName": "platform",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "what_are_you_looking_for?",
              "type": "string",
              "display": true,
              "removed": true,
              "required": false,
              "displayName": "what_are_you_looking_for?",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "full_name",
              "type": "string",
              "display": true,
              "removed": true,
              "required": false,
              "displayName": "full_name",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "company_name",
              "type": "string",
              "display": true,
              "removed": true,
              "required": false,
              "displayName": "company_name",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "email",
              "type": "string",
              "display": true,
              "removed": true,
              "required": false,
              "displayName": "email",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "phone_number",
              "type": "string",
              "display": true,
              "removed": true,
              "required": false,
              "displayName": "phone_number",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "lead_status",
              "type": "string",
              "display": true,
              "removed": false,
              "required": false,
              "displayName": "lead_status",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "crm_id",
              "type": "string",
              "display": true,
              "removed": false,
              "required": false,
              "displayName": "crm_id",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "Note",
              "type": "string",
              "display": true,
              "removed": true,
              "required": false,
              "displayName": "Note",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "row_number",
              "type": "number",
              "display": true,
              "removed": false,
              "readOnly": true,
              "required": false,
              "displayName": "row_number",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            }
          ],
          "mappingMode": "defineBelow",
          "matchingColumns": [
            "row_number"
          ],
          "attemptToConvertTypes": false,
          "convertFieldsToString": false
        },
        "options": {},
        "operation": "update",
        "sheetName": {
          "__rl": true,
          "mode": "list",
          "value": 78844591,
          "cachedResultUrl": "",
          "cachedResultName": "Sheet1"
        },
        "documentId": {
          "__rl": true,
          "mode": "url",
          "value": ""
        }
      },
      "credentials": {
        "googleSheetsOAuth2Api": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 4.7
    },
    {
      "id": "d2e1139d-6214-4e81-abe2-1cd1881c8fe6",
      "name": "Wait (rate limit buffer)",
      "type": "n8n-nodes-base.wait",
      "position": [
        272,
        800
      ],
      "parameters": {},
      "typeVersion": 1.1
    },
    {
      "id": "94dd7d44-a98f-4876-9189-ed2f2cefd1a3",
      "name": "Sticky Note",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -1728,
        608
      ],
      "parameters": {
        "color": 3,
        "width": 576,
        "height": 736,
        "content": "## Who\u2019s it for\nThis workflow is for sales and support teams who collect leads from Facebook Lead Ads into Google Sheets and want to automatically sync those leads into Perfex CRM without creating duplicates.\n\n## What it does\nEvery minute, the workflow reads new rows from a Google Sheet where `lead_status` is `CREATED`. For each lead, it searches Perfex CRM by email. If a lead already exists, it updates the Google Sheet row to `ADDED` and stores a clickable CRM link. If the lead is not found (404), it creates a new lead in Perfex CRM and then marks the Sheet row as `ADDED` with the new CRM link.\n\n## How to set up\n1. Connect your Google Sheets credentials.\n2. Add your Perfex CRM API token using n8n credentials or environment variables (do not hardcode tokens in the HTTP node).\n3. In the \u201cConfig (Edit these)\u201d node, set your Perfex base URL, assignee ID, and Sheet details.\n4. Ensure your Google Sheet has at least: `email`, `full_name`, `phone_number`, and `lead_status`.\n\n## Requirements\n- Google Sheets access\n- Perfex CRM REST API token (Perfex \u201cRest API module\u201d)\n\n## How to customize\nMap additional fields (tags, source, description, custom fields) in the \u201cPerfex: Create lead\u201d node and adjust the batch size or schedule interval for your volume.\n"
      },
      "typeVersion": 1
    },
    {
      "id": "695f41a5-0e72-40ee-8271-4d5653a92763",
      "name": "Sticky Note1",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -944,
        688
      ],
      "parameters": {
        "color": 7,
        "width": 304,
        "height": 80,
        "content": "Expected columns + example values + lead_status logic."
      },
      "typeVersion": 1
    },
    {
      "id": "2cd9145a-f813-4a01-be77-1eafe3fe5930",
      "name": "Sticky Note2",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -512,
        688
      ],
      "parameters": {
        "color": 7,
        "width": 304,
        "height": 80,
        "content": "How to generate API token, where to put it (credentials/env), what 404 means."
      },
      "typeVersion": 1
    },
    {
      "id": "0d066ea4-be01-4dba-96bf-994db1a37455",
      "name": "Sticky Note3",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        80,
        1024
      ],
      "parameters": {
        "color": 7,
        "width": 150,
        "height": 112,
        "content": "Writes back ADDED + CRM hyperlink, prevents duplicates."
      },
      "typeVersion": 1
    }
  ],
  "connections": {
    "IF: Lead exists": {
      "main": [
        [
          {
            "node": "Google Sheets: Mark as ADDED (existing)",
            "type": "main",
            "index": 0
          }
        ],
        []
      ]
    },
    "Split in batches": {
      "main": [
        [],
        [
          {
            "node": "Perfex: Search lead by email",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Perfex: Create lead": {
      "main": [
        [
          {
            "node": "Google Sheets: Mark as ADDED (new)",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "IF: Lead not found (404)": {
      "main": [
        [
          {
            "node": "Perfex: Create lead",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Wait (rate limit buffer)": {
      "main": [
        [
          {
            "node": "Split in batches",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Perfex: Search lead by email": {
      "main": [
        [
          {
            "node": "IF: Lead exists",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "IF: Lead not found (404)",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Schedule trigger (every minute)": {
      "main": [
        [
          {
            "node": "Google Sheets: Get leads with status CREATED",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Google Sheets: Mark as ADDED (new)": {
      "main": [
        [
          {
            "node": "Split in batches",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Google Sheets: Mark as ADDED (existing)": {
      "main": [
        [
          {
            "node": "Wait (rate limit buffer)",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Google Sheets: Get leads with status CREATED": {
      "main": [
        [
          {
            "node": "Split in batches",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}