{
  "id": "1yJTPMbE7n3YfDDf",
  "meta": {
    "templateCredsSetupCompleted": true
  },
  "name": "Automated CRM enrichment: enrich companies in your CRM with CompanyEnrich",
  "tags": [],
  "nodes": [
    {
      "id": "482e8b85-415f-43f7-bace-14215bd01fee",
      "name": "Schedule Trigger",
      "type": "n8n-nodes-base.scheduleTrigger",
      "position": [
        -96,
        336
      ],
      "parameters": {
        "rule": {
          "interval": [
            {
              "field": "weeks"
            }
          ]
        }
      },
      "typeVersion": 1.2
    },
    {
      "id": "cee018f0-2095-4bd9-bd96-bc21b419efa5",
      "name": "Extract Domain1",
      "type": "n8n-nodes-base.function",
      "position": [
        432,
        336
      ],
      "parameters": {
        "functionCode": "let results = [];\n\n// n8n'den gelen her bir ana \u00f6\u011feyi d\u00f6n\u00fcyoruz\nfor (const item of items) {\n  const raw = item.json;\n\n  // Close CRM veriyi 'data' array'i i\u00e7inde g\u00f6nderir.\n  // Di\u011ferleri (Salesforce/HubSpot) direkt liste g\u00f6nderir.\n  // E\u011fer 'data' varsa onu kullan, yoksa \u00f6\u011feyi bir liste i\u00e7ine al.\n  const records = (raw.data && Array.isArray(raw.data)) ? raw.data : [raw];\n\n  for (const src of records) {\n    const p = src.properties || {};\n\n    // De\u011fer bir objeyse (HubSpot) .value k\u0131sm\u0131n\u0131 al, de\u011filse direkt d\u00f6nd\u00fcr\n    const getValue = (val) => {\n      if (val && typeof val === 'object' && val.hasOwnProperty('value')) {\n        return val.value;\n      }\n      return val;\n    };\n\n    // DOMAIN TESP\u0130T\u0130\n    const domain =\n      getValue(src.Website) ||      // Salesforce\n      getValue(src.url) ||          // Close CRM\n      getValue(p[\"domain\"]) ||      // HubSpot\n      getValue(p[\"website\"]) ||     // HubSpot Alternatif\n      getValue(src.domain) ||       // Genel\n      getValue(src.website) ||      // Genel\n      \"\";\n\n    // ID TESP\u0130T\u0130\n    const companyId =\n      getValue(src.Id) ||           // Salesforce\n      getValue(src.id) ||           // Close & HubSpot (Yeni)\n      getValue(src.objectId) ||     // HubSpot (Eski)\n      getValue(p[\"hs_object_id\"]) ||\n      \"\";\n\n    // E\u011fer ID veya Domain ikisi de bo\u015fsa bu kayd\u0131 ekleme (temizlik)\n    if (companyId || domain) {\n      results.push({\n        json: {\n          companyId: String(companyId),\n          domain: domain\n        }\n      });\n    }\n  }\n}\n\nreturn results;"
      },
      "retryOnFail": false,
      "typeVersion": 1,
      "alwaysOutputData": false
    },
    {
      "id": "cae6aae0-ce28-4aec-bb8b-075c72dbe824",
      "name": "CompanyEnrich - Enrich by Domain1",
      "type": "n8n-nodes-base.httpRequest",
      "position": [
        800,
        400
      ],
      "parameters": {
        "url": "https://api.companyenrich.com/companies/enrich",
        "options": {},
        "sendQuery": true,
        "jsonHeaders": "{\n  \"Authorization\": \"Bearer FO0SxxBFPd6rCQLcyKoTSJ\",        \n  \"Accept\": \"application/json\"\n}\n",
        "sendHeaders": true,
        "specifyHeaders": "json",
        "queryParameters": {
          "parameters": [
            {
              "name": "domain",
              "value": "={{ $json.domain }}"
            }
          ]
        }
      },
      "typeVersion": 4
    },
    {
      "id": "82197139-da1c-4634-a1cf-5caf36e7001b",
      "name": "HubSpot - Update Company1",
      "type": "n8n-nodes-base.hubspot",
      "disabled": true,
      "position": [
        1152,
        352
      ],
      "parameters": {
        "resource": "company",
        "companyId": "={{ $json.companyId }}",
        "operation": "update",
        "updateFields": {
          "city": "={{ $json.location.city.name }}",
          "name": "={{ $json.name }}",
          "postalCode": "={{ $json.location.postal_code }}",
          "websiteUrl": "={{ $json.website }}",
          "description": "={{ $json.description }}",
          "phoneNumber": "={{ $json.location.phone }}",
          "stateRegion": "={{ $json.location.state.name }}",
          "yearFounded": "={{ $json.founded_year }}",
          "countryRegion": "={{ $json.location.country.name }}",
          "streetAddress": "={{ $json.location.address }}",
          "twitterHandle": "={{ $json.socials.twitter_url }}",
          "companyDomainName": "={{ $json.domain }}",
          "linkedInCompanyPage": "={{ $json.socials.linkedin_url }}"
        },
        "authentication": "appToken"
      },
      "credentials": {
        "hubspotAppToken": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 1
    },
    {
      "id": "90bce036-f97a-4ac8-8e57-74e96a471436",
      "name": "Loop Over Items1",
      "type": "n8n-nodes-base.splitInBatches",
      "position": [
        608,
        336
      ],
      "parameters": {
        "options": {
          "reset": false
        }
      },
      "typeVersion": 3,
      "alwaysOutputData": false
    },
    {
      "id": "b5baf50d-5f97-4698-94d8-c1af42417726",
      "name": "Merge1",
      "type": "n8n-nodes-base.merge",
      "position": [
        944,
        352
      ],
      "parameters": {
        "mode": "combine",
        "options": {},
        "combineBy": "combineByPosition"
      },
      "typeVersion": 3.2
    },
    {
      "id": "4ff252cf-471a-4f5c-b522-4c146b1c0847",
      "name": "Sticky Note4",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -752,
        -160
      ],
      "parameters": {
        "width": 608,
        "height": 864,
        "content": "## AI COMPANY ENRICHMENT FOR NEW OR UPDATED HUBSPOT/SALESFORCE COMMPANIES\n\n### \ud83d\udccc How This Workflow Works?\n1. **Schedule Trigger:** Finds the newly created or updated companies in a set time period.\n2. **Extract Domain:** Extracts the domain seeds of the companies for CompanyEnrich enrichment API.\n3. **Hubspot/SalesForce/Close CRM Update node:** Updates the corresponding company with enriched data if enrichment success is false we log the error\n\n### \ud83d\udee0\ufe0f Before using\nFor HUBSPOT:\n\u2714 Create an Hubspot private app with company read and write scopes, add your app's credentials for both hubspot nodes.  \n\nFor SALESFORCE:\n\u2714 Connect your Salesforce account on all the **Salesforce** nodes.\n\nFor Close CRM\n\u2714 In the Close CRM node, create a Basic Auth credential.\n\u2714 Enter your Close CRM API key in the username field.\n\u2714 Add a colon \":\" at the end of the API key.\n\u2714 Leave the password field empty.\n\u26a0\ufe0f Important:\nYou must append : to the end of your API key and keep the password field blank, otherwise authentication will fail.\n\n\u2714 Ensure companies have a valid domain (required for enrichment)  \n\u2714 Create an Company Enrich account and enter your api key to HTTP Request node's header where it says `YOUR_API_KEY`.  \n\u2714 Map the enriched datas in Hubspot\\Salesforce\\Close CRM Update nodes.\n\n\n### CUSTOMIZATION\n1. **Filters:** Change the time period in schedule trigger and get recently updated companies nodes  \n2. **Fields to Update:** Map the custom company properties in Update companies node\n\n\nFor more customization instructions, check the respective node\u2019s sticky notes."
      },
      "typeVersion": 1
    },
    {
      "id": "d7495e07-e349-4e64-b1d2-b13d3f168ee3",
      "name": "Sticky Note5",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -96,
        -352
      ],
      "parameters": {
        "color": 7,
        "width": 640,
        "height": 1056,
        "content": "## Schedule Trigger & Extract Domain\n\nControl how far back companies are enriched:\n\u26a0\ufe0f Disable the Crm nodes you do not use\n\u26a0\ufe0f Delete the connection to the EXTRACT DOMAIN NODE for the crm you do not use\n\n1. **Schedule Trigger:**  \n   Set how often the workflow runs.\n\n2. **HubSpot Get Companies node:**  \n   Change the number in `7*24*60*60*1000`.  \n   - `7` = last 7 days  \n   -  Change this number to any value you want (e.g. `1` = last 1 day, `14` = last 14 days)\n\n3. **Salesforce Get Companies node:**  \n   Change the number in `({ days: 7 })`.  \n   - `7` = last 7 days  \n   -  Change this number to any value you want (e.g. `1` = last 1 day, `14` = last 14 days)\n\n4. **Close CRM node:**  \n   Change the number in BODY which is `7*24*60*60*1000`.  \n   - `7` = last 7 days  \n   -  Change this number to any value you want (e.g. `1` = last 1 day, `14` = last 14 days)"
      },
      "typeVersion": 1
    },
    {
      "id": "85f63476-9e70-4d9e-af55-9c38a5346961",
      "name": "Sticky Note6",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        944,
        64
      ],
      "parameters": {
        "color": 7,
        "width": 704,
        "height": 480,
        "content": "## Update Companies\n\nControl which properties are sent to HubSpot/Salesforce by mapping them in the **HubSpot/Salesforce Update Company** node.\n\n\u26a0\ufe0f Disable the Crm you do not use\n\u26a0\ufe0f Be careful to map **only one value per field** to avoid HubSpot API errors.\n \n\ud83d\udcd8 Close CRM Mapping Guide\nSince Close CRM is managed via an HTTP Request node, you must map properties manually within the request body using keys like display_name or url. To map Custom Fields, locate their specific API Keys (e.g., custom.cf_XXXX) in Close Settings > Custom Fields and add them to the body JSON and map CompanyEnrich api response to those custom fields key you added.\n\n"
      },
      "typeVersion": 1
    },
    {
      "id": "6dc4ac02-5b53-44da-a000-c2211b13e684",
      "name": "Sticky Note7",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        608,
        176
      ],
      "parameters": {
        "color": 7,
        "width": 288,
        "height": 368,
        "content": "## Enrichment Loop\n\nEnter your CompanyEnrich API key in the **HTTP Request** node where it says `YOUR_API_KEY`.\n"
      },
      "typeVersion": 1
    },
    {
      "id": "418263f3-7005-408a-a036-2c3c766e837d",
      "name": "Get many accounts",
      "type": "n8n-nodes-base.salesforce",
      "position": [
        112,
        480
      ],
      "parameters": {
        "options": {
          "conditionsUi": {
            "conditionValues": [
              {
                "field": "CreatedDate",
                "value": "={{ $json.timestamp.toDateTime().minus({ days: 7 }).toISO() }}",
                "operation": ">="
              }
            ]
          }
        },
        "resource": "account",
        "operation": "getAll",
        "returnAll": true
      },
      "credentials": {
        "salesforceOAuth2Api": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 1
    },
    {
      "id": "22cb1134-f52e-4534-a346-ff7325324ffe",
      "name": "Get an account",
      "type": "n8n-nodes-base.salesforce",
      "position": [
        448,
        608
      ],
      "parameters": {
        "resource": "account",
        "accountId": "={{ $json.Id }}",
        "operation": "get"
      },
      "credentials": {
        "salesforceOAuth2Api": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 1
    },
    {
      "id": "af2856ae-bbff-440d-892a-d687d057774e",
      "name": "Loop Over Items",
      "type": "n8n-nodes-base.splitInBatches",
      "position": [
        272,
        480
      ],
      "parameters": {
        "options": {}
      },
      "typeVersion": 3
    },
    {
      "id": "52d81ddf-9176-42d6-bb35-f2d65c783c42",
      "name": "Get recently created/updated companies",
      "type": "n8n-nodes-base.hubspot",
      "position": [
        112,
        336
      ],
      "parameters": {
        "resource": "company",
        "operation": "getRecentlyCreatedUpdated",
        "returnAll": true,
        "authentication": "appToken",
        "additionalFields": {
          "since": "={{ new Date(new Date($json.timestamp).getTime() - 7*24*60*60*1000)\n    .toISOString()\n    .slice(0,10) }}\n"
        }
      },
      "credentials": {
        "hubspotAppToken": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 2.2
    },
    {
      "id": "1916face-8796-45ff-9f85-413b781bf926",
      "name": "Create or update an account",
      "type": "n8n-nodes-base.salesforce",
      "disabled": true,
      "position": [
        1328,
        352
      ],
      "parameters": {
        "name": "={{ $json.name }}",
        "resource": "account",
        "operation": "upsert",
        "externalId": "Id",
        "externalIdValue": "={{ $json.companyId }}",
        "additionalFields": {}
      },
      "credentials": {
        "salesforceOAuth2Api": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 1
    },
    {
      "id": "9c571d4e-0ff2-4cf8-b2fb-0faf8cf584d9",
      "name": "Close CRM",
      "type": "n8n-nodes-base.httpRequest",
      "position": [
        112,
        176
      ],
      "parameters": {
        "url": "https://api.close.com/api/v1/data/search/",
        "method": "POST",
        "options": {},
        "jsonBody": "={\n  \"query\": {\n    \"type\": \"and\",\n    \"queries\": [\n      {\n        \"type\": \"object_type\",\n        \"object_type\": \"lead\"\n      },\n      {\n        \"type\": \"field_condition\",\n        \"field\": {\n          \"type\": \"regular_field\",\n          \"object_type\": \"lead\",\n          \"field_name\": \"date_created\"\n        },\n        \"condition\": {\n          \"type\": \"moment_range\",\n          \"gte\": \"{{ $now.minus({ days: 7 }).toISO() }}\",\n          \"lte\": \"{{ $now.toISO() }}\"\n        }\n      }\n    ]\n  },\n  \"_fields\": {\n    \"lead\": [\"id\", \"display_name\", \"url\"]\n  },\n  \"limit\": 100\n}",
        "sendBody": true,
        "specifyBody": "json",
        "authentication": "genericCredentialType",
        "genericAuthType": "httpBasicAuth"
      },
      "credentials": {
        "httpBasicAuth": {
          "name": "<your credential>"
        },
        "httpBearerAuth": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 4.3
    },
    {
      "id": "6791efcf-b56e-44ff-8049-12aab7b556ed",
      "name": "Close Crm Update",
      "type": "n8n-nodes-base.httpRequest",
      "disabled": true,
      "position": [
        1504,
        352
      ],
      "parameters": {
        "url": "=https://api.close.com/api/v1/lead/{{ $json.companyId }}/",
        "method": "PUT",
        "options": {},
        "jsonBody": "={\n  \"display_name\": \"{{ $json.name || '' }}\",\n  \"url\": \"{{ $json.website || '' }}\",\n  \"description\": \"{{ $json.description || '' }}\",\n  \"external_id\": \"{{ $json.companyId || '' }}\",\n  \"addresses\": [\n    {\n      \"label\": \"office\",\n      \"address_1\": \"{{ $json.location.address || '' }}\",\n      \"city\": \"{{ $json.location.city.name || '' }}\",\n      \"state\": \"{{ $json.location.state.name || '' }}\",\n      \"zip\": \"{{ $json.location.postal_code || '' }}\",\n      \"country\": \"{{ $json.location.country.name || '' }}\"\n    }\n  ]\n}",
        "sendBody": true,
        "specifyBody": "json",
        "authentication": "genericCredentialType",
        "genericAuthType": "httpBasicAuth"
      },
      "credentials": {
        "httpBasicAuth": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 4.3
    }
  ],
  "active": false,
  "settings": {
    "executionOrder": "v1"
  },
  "versionId": "f6365f28-8bdf-4d59-992b-0e13eb1b1d95",
  "connections": {
    "Merge1": {
      "main": [
        [
          {
            "node": "HubSpot - Update Company1",
            "type": "main",
            "index": 0
          },
          {
            "node": "Loop Over Items1",
            "type": "main",
            "index": 0
          },
          {
            "node": "Create or update an account",
            "type": "main",
            "index": 0
          },
          {
            "node": "Close Crm Update",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Close CRM": {
      "main": [
        [
          {
            "node": "Extract Domain1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Get an account": {
      "main": [
        [
          {
            "node": "Loop Over Items",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Extract Domain1": {
      "main": [
        [
          {
            "node": "Loop Over Items1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Loop Over Items": {
      "main": [
        [
          {
            "node": "Extract Domain1",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Get an account",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Close Crm Update": {
      "main": [
        [
          {
            "node": "Loop Over Items1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Loop Over Items1": {
      "main": [
        [],
        [
          {
            "node": "CompanyEnrich - Enrich by Domain1",
            "type": "main",
            "index": 0
          },
          {
            "node": "Merge1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Schedule Trigger": {
      "main": [
        [
          {
            "node": "Get many accounts",
            "type": "main",
            "index": 0
          },
          {
            "node": "Get recently created/updated companies",
            "type": "main",
            "index": 0
          },
          {
            "node": "Close CRM",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Get many accounts": {
      "main": [
        [
          {
            "node": "Loop Over Items",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "HubSpot - Update Company1": {
      "main": [
        [
          {
            "node": "Loop Over Items1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Create or update an account": {
      "main": [
        [
          {
            "node": "Loop Over Items1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "CompanyEnrich - Enrich by Domain1": {
      "main": [
        [
          {
            "node": "Merge1",
            "type": "main",
            "index": 1
          }
        ]
      ]
    },
    "Get recently created/updated companies": {
      "main": [
        [
          {
            "node": "Extract Domain1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}