Check if object exists else create new

Hello,
how can I check if an object exists and if create a new one if it doesn’t?

The workflow should look something like this:

List of objects from google sheet → Search for the object in a database via api (httpRequest) ==>
If object doesn’t exists → Create it → Go on to the next object in the list
If object exists → Go on to the next object in the list

My main problem is to push the search query parameters to next node…

Hi @Silas_Hundhausen, welcome to the community, I hope you’re having a good day?

Based on your post it seems you already got your list of items from Google Sheets and are now having problems using the item data as query parameters in the HTTP Request node, did I get that right? Could you elaborate a bit on what exactly you are trying and what error you are getting here?

Thank you!

Hey thank you for the quick reply.

Yes I have already got the HTTP Request node and the google sheet node setup.

My workflow:

Google sheet Node (name, url)

HTTP Request node (API get request with url)
→ If request is successfull: API request returns url, name and more information of the object.
→ If request is NOT successfull: API/Node returns error (if this is the case: get the name and url from the google sheet and send a post request through the API)

My problem is described in the following screenshot:

Hope this description helps.

Thank you for your help!

Kind regards
Silas

Many thanks for sharing the screenshot of your workflow!

From looking into this, you might not need the Split in Batches node here as the HTTP request node will by default already run once for each item it receives from the Google Sheet.

So a workflow like this will run once for each item it receives from the Google Sheet:

{
  "nodes": [
    {
      "parameters": {},
      "name": "Start",
      "type": "n8n-nodes-base.start",
      "typeVersion": 1,
      "position": [
        250,
        300
      ]
    },
    {
      "parameters": {
        "authentication": "oAuth2",
        "sheetId": "1wPcsZ7LLkLyBFeVUOnmuKhbUlky3uxOsxUqKp0Pc8Ec",
        "range": "A:B",
        "options": {}
      },
      "name": "Google Sheets",
      "type": "n8n-nodes-base.googleSheets",
      "typeVersion": 1,
      "position": [
        450,
        300
      ],
      "credentials": {
        "googleSheetsOAuth2Api": "Google Sheets account"
      }
    },
    {
      "parameters": {
        "url": "={{$json[\"url\"]}}",
        "options": {}
      },
      "name": "HTTP Request",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 1,
      "position": [
        650,
        300
      ]
    }
  ],
  "connections": {
    "Start": {
      "main": [
        [
          {
            "node": "Google Sheets",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Google Sheets": {
      "main": [
        [
          {
            "node": "HTTP Request",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}

In this example my Google Sheet looks like this:
image

The HTTP Request then runs once for each item:

Seeing your first HTTP might throw an error, you probably want to continue the execution for error cases as described here. You could then check handle the error with a subsequent IF node.

If an “item does not exist” error was thrown, you could then make an additional HTTP request as required. A recent post describing how to access data from more than one node ago can be found at How to pass data from 1 to 3? - #3 by mcnaveen in case it’s needed.

I hope this helps with getting started :slight_smile: