{
  "name": "NetSuite customers to Salesforce (paged, delta or full export)",
  "nodes": [
    {
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "leftValue": "",
            "typeValidation": "strict",
            "version": 2
          },
          "conditions": [
            {
              "id": "b551567c-df5c-4d02-86a0-82752d35df2b",
              "leftValue": "={{ $json.isPerson }}",
              "rightValue": "false",
              "operator": {
                "type": "boolean",
                "operation": "false",
                "singleValue": true
              }
            }
          ],
          "combinator": "and"
        },
        "options": {}
      },
      "type": "n8n-nodes-base.if",
      "typeVersion": 2.2,
      "position": [
        1056,
        -160
      ],
      "id": "517d49d4-bfb1-427e-98b2-1e45965c5311",
      "name": "Is Company?"
    },
    {
      "parameters": {
        "resource": "Customer",
        "operation": "get /customer/{id}",
        "id": "={{ $json.id }}",
        "additionalFields": {}
      },
      "type": "n8n-nodes-netsuite-rest.netSuiteRest",
      "typeVersion": 1,
      "position": [
        672,
        -160
      ],
      "id": "14529f4d-5bb7-492b-aefd-195d2541e002",
      "name": "NS: Customer - Get record",
      "credentials": {
        "netSuiteRestOAuth2Api": {
          "id": "REPLACE_WITH_YOUR_CREDENTIAL",
          "name": "NetSuite REST account"
        }
      }
    },
    {
      "parameters": {
        "resource": "account",
        "operation": "upsert",
        "externalId": "External_ID__c",
        "externalIdValue": "={{ $json.id }}",
        "name": "={{ $json.companyName }}",
        "additionalFields": {}
      },
      "type": "n8n-nodes-base.salesforce",
      "typeVersion": 1,
      "position": [
        1552,
        -256
      ],
      "id": "79637b70-9586-473b-a053-fc54bcf931c5",
      "name": "SF: Create or Update Company Account",
      "executeOnce": false,
      "retryOnFail": false,
      "credentials": {
        "salesforceOAuth2Api": {
          "id": "REPLACE_WITH_YOUR_CREDENTIAL",
          "name": "Salesforce account"
        }
      }
    },
    {
      "parameters": {
        "resource": "contact",
        "operation": "upsert",
        "externalId": "External_ID__c",
        "externalIdValue": "={{ $json.id }}",
        "lastname": "={{ $('Is Company?').item.json.lastName }}",
        "additionalFields": {
          "acconuntId": "={{ $json.id }}",
          "email": "={{ $('Is Company?').item.json.email }}",
          "firstName": "={{ $('Is Company?').item.json.firstName }}"
        }
      },
      "type": "n8n-nodes-base.salesforce",
      "typeVersion": 1,
      "position": [
        1664,
        -48
      ],
      "id": "03827337-fcd9-4c53-a427-0b65d21fce57",
      "name": "SF: Create or Update Contact",
      "executeOnce": false,
      "retryOnFail": false,
      "credentials": {
        "salesforceOAuth2Api": {
          "id": "REPLACE_WITH_YOUR_CREDENTIAL",
          "name": "Salesforce account"
        }
      }
    },
    {
      "parameters": {
        "resource": "account",
        "operation": "upsert",
        "externalId": "External_ID__c",
        "externalIdValue": "={{ $json.id }}",
        "name": "={{ $json.firstName + ' ' + $json.lastName}}",
        "additionalFields": {}
      },
      "type": "n8n-nodes-base.salesforce",
      "typeVersion": 1,
      "position": [
        1440,
        -48
      ],
      "id": "21e2e2f6-f9e6-4c66-a6a0-33cc5c8aa4c8",
      "name": "SF: Create or Update Person's Account",
      "executeOnce": false,
      "retryOnFail": false,
      "credentials": {
        "salesforceOAuth2Api": {
          "id": "REPLACE_WITH_YOUR_CREDENTIAL",
          "name": "Salesforce account"
        }
      }
    },
    {
      "parameters": {
        "fieldToSplitOut": "items",
        "options": {}
      },
      "type": "n8n-nodes-base.splitOut",
      "typeVersion": 1,
      "position": [
        448,
        -160
      ],
      "id": "3e3b7363-ac9b-44f1-900b-d493a9db597e",
      "name": "Split Customers Array"
    },
    {
      "parameters": {},
      "type": "n8n-nodes-base.merge",
      "typeVersion": 3.2,
      "position": [
        2048,
        -144
      ],
      "id": "b4e45f33-2385-472d-b619-b45b3bbad8c8",
      "name": "Merge Company/Contact Results"
    },
    {
      "parameters": {
        "content": "## Salesforce section\n\n### Records are inserted/updated into Salesforce here\n\nRequires a custom External ID field called External_ID__c on both Account and Contact.",
        "height": 560,
        "width": 592,
        "color": 5
      },
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        1312,
        -416
      ],
      "typeVersion": 1,
      "id": "a88430cf-850b-4ee7-bda3-035d65f160bf",
      "name": "Sticky Note"
    },
    {
      "parameters": {
        "content": "## NetSuite section\n\n### Records are pulled from NetSuite here\n\n### Connect ONE of the two list nodes to 'Split Customers Array':\n- 'NS: Customer - Get Delta records' = only records changed since the last run (connected)\n- 'NS: Customer - Get All records' = full export (not connected)\n\n### Feel free to filter with the Q parameter\nFor example - email START_WITH barbara\n\nRead about using Q parameter in [NetSuite Docs](https://docs.oracle.com/en/cloud/saas/netsuite/ns-online-help/section_1545222128.html)",
        "height": 496,
        "width": 784,
        "color": 7
      },
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        96,
        -416
      ],
      "typeVersion": 1,
      "id": "e4accef6-6a08-4a36-8100-32e831bffd59",
      "name": "Sticky Note1"
    },
    {
      "parameters": {},
      "type": "n8n-nodes-base.noOp",
      "typeVersion": 1,
      "position": [
        256,
        368
      ],
      "id": "19070186-030e-44bb-a9ff-8b5a474cee3f",
      "name": "Workflow is finished"
    },
    {
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "leftValue": "",
            "typeValidation": "strict",
            "version": 2
          },
          "conditions": [
            {
              "id": "da799032-1cc2-421d-91cd-4eb174744162",
              "leftValue": "={{ $json.hasMore }}",
              "rightValue": "",
              "operator": {
                "type": "boolean",
                "operation": "true",
                "singleValue": true
              }
            }
          ],
          "combinator": "and"
        },
        "options": {}
      },
      "type": "n8n-nodes-base.if",
      "typeVersion": 2.2,
      "position": [
        -320,
        -96
      ],
      "id": "7102959c-8f8b-4cd7-bfda-c33b150156ea",
      "name": "Has More Records?"
    },
    {
      "parameters": {
        "jsCode": "const workflowStaticData = $getWorkflowStaticData('global');\nconst lastExportDate = workflowStaticData.lastExportDate ? workflowStaticData.lastExportDate : $today.minus({days: 1}).toFormat('MM/dd/yyyy')\n\nreturn [\n  {\n      offset: workflowStaticData.offset,\n      lastExportDate: lastExportDate\n  }\n];"
      },
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        -96,
        -160
      ],
      "id": "7a88c79c-142b-47c1-b690-1b7ea4d30db8",
      "name": "Retrieve Paging Offset and LastExportDate"
    },
    {
      "parameters": {
        "jsCode": "const workflowStaticData = $getWorkflowStaticData('global');\n\nworkflowStaticData.offset = workflowStaticData.offset+20\nworkflowStaticData.hasMore = $('NS: Customer - Get All records').isExecuted ? $('NS: Customer - Get All records').first().json.hasMore: $('NS: Customer - Get Delta records').first().json.hasMore\n\nreturn [\n  {\n      offset: workflowStaticData.offset,\n      hasMore: workflowStaticData.hasMore\n  }\n];"
      },
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        2272,
        -80
      ],
      "id": "3eaa9173-d196-400f-853c-49f4a7f13847",
      "name": "Update Paging Offset and LastExportDate"
    },
    {
      "parameters": {
        "rule": {
          "interval": [
            {}
          ]
        }
      },
      "type": "n8n-nodes-base.scheduleTrigger",
      "typeVersion": 1.2,
      "position": [
        -768,
        -96
      ],
      "id": "fe7f6315-dcba-4e31-ad10-cf8856633b6a",
      "name": "Execute Workflow Daily"
    },
    {
      "parameters": {
        "jsCode": "// initialize staticData object\nconst workflowStaticData = $getWorkflowStaticData('global');\n\nworkflowStaticData.offset = 0\nworkflowStaticData.hasMore = true\n\nreturn [\n  {\n      offset: workflowStaticData.offset,\n      hasMore: workflowStaticData.hasMore\n  }\n];"
      },
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        -544,
        -96
      ],
      "id": "66e23779-e65f-4659-905f-c931167b8b0d",
      "name": "Init Offset"
    },
    {
      "parameters": {
        "resource": "Customer",
        "additionalFields": {
          "limit": 20,
          "offset": "={{ $json.offset }}",
          "q": "=lastModifiedDate ON_OR_AFTER \"{{ $json.lastExportDate }}\""
        }
      },
      "type": "n8n-nodes-netsuite-rest.netSuiteRest",
      "typeVersion": 1,
      "position": [
        192,
        -160
      ],
      "id": "d85c4562-19c4-4eaa-8486-207332facfe1",
      "name": "NS: Customer - Get Delta records",
      "credentials": {
        "netSuiteRestOAuth2Api": {
          "id": "REPLACE_WITH_YOUR_CREDENTIAL",
          "name": "NetSuite REST account"
        }
      }
    },
    {
      "parameters": {
        "resource": "Customer",
        "additionalFields": {
          "limit": 20,
          "offset": "={{ $json.offset }}"
        }
      },
      "type": "n8n-nodes-netsuite-rest.netSuiteRest",
      "typeVersion": 1,
      "position": [
        192,
        -20
      ],
      "id": "a42909a2-1cfd-49fe-8461-e42f665e744f",
      "name": "NS: Customer - Get All records",
      "credentials": {
        "netSuiteRestOAuth2Api": {
          "id": "REPLACE_WITH_YOUR_CREDENTIAL",
          "name": "NetSuite REST account"
        }
      }
    },
    {
      "parameters": {
        "jsCode": "const workflowStaticData = $getWorkflowStaticData('global');\n\nworkflowStaticData.lastExportDate = DateTime.now().toFormat('MM/dd/yyyy')\n\nreturn [\n  {\n      lastExportDate: workflowStaticData.lastExportDate\n  }\n];"
      },
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        48,
        368
      ],
      "id": "9cd4dfd7-b24b-4752-84d3-cb44ba02078e",
      "name": "Update LastExportDate"
    }
  ],
  "connections": {
    "Is Company?": {
      "main": [
        [
          {
            "node": "SF: Create or Update Company Account",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "SF: Create or Update Person's Account",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "NS: Customer - Get record": {
      "main": [
        [
          {
            "node": "Is Company?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "SF: Create or Update Company Account": {
      "main": [
        [
          {
            "node": "Merge Company/Contact Results",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "SF: Create or Update Contact": {
      "main": [
        [
          {
            "node": "Merge Company/Contact Results",
            "type": "main",
            "index": 1
          }
        ]
      ]
    },
    "SF: Create or Update Person's Account": {
      "main": [
        [
          {
            "node": "SF: Create or Update Contact",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Split Customers Array": {
      "main": [
        [
          {
            "node": "NS: Customer - Get record",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Merge Company/Contact Results": {
      "main": [
        [
          {
            "node": "Update Paging Offset and LastExportDate",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Has More Records?": {
      "main": [
        [
          {
            "node": "Retrieve Paging Offset and LastExportDate",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Update LastExportDate",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Retrieve Paging Offset and LastExportDate": {
      "main": [
        [
          {
            "node": "NS: Customer - Get Delta records",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Update Paging Offset and LastExportDate": {
      "main": [
        [
          {
            "node": "Has More Records?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Execute Workflow Daily": {
      "main": [
        [
          {
            "node": "Init Offset",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Init Offset": {
      "main": [
        [
          {
            "node": "Has More Records?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "NS: Customer - Get Delta records": {
      "main": [
        [
          {
            "node": "Split Customers Array",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Update LastExportDate": {
      "main": [
        [
          {
            "node": "Workflow is finished",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "pinData": {}
}
