Split Updating Same Item

Hi, I am trying to use the SPLIT and IF nodes to loop through multiple records but only a single record is updating even though the correct number of items are being processed.

This is my workflow:

First I need to get a list of products from my DB that need to be updated. Then for each product (9 in my tests) I need to make an API call to fetch the price and then I use the price from the response to update each product. When looking at the data, each iteration on the SPLIT and IF nodes show a different row of data from my SQL query but the actual Price fetch API call, gets the price for a single product 9 times. I am not sure, what else I need to do.

Any help will be greatly appreciated.

Hi @harshil1712, any ideas on what I should check for here?

Hi @Suratwala, no need to mention folks directly. Community members and the n8n team alike will usually take a look at your post as soon as they have time :slight_smile:

As for your actual question: n8n will loop through input items automatically for most nodes. So for the scenario you have described (making an HTTP Request for each input item, then performing updates) you wouldn’t need the Split in Batches node. For example:

Example Workflow
{
  "nodes": [
    {
      "parameters": {},
      "name": "Start",
      "type": "n8n-nodes-base.start",
      "typeVersion": 1,
      "position": [
        250,
        300
      ]
    },
    {
      "parameters": {
        "operation": "executeQuery",
        "query": "SELECT id, name FROM foo;"
      },
      "name": "Microsoft SQL",
      "type": "n8n-nodes-base.microsoftSql",
      "typeVersion": 1,
      "position": [
        450,
        300
      ],
      "credentials": {
        "microsoftSql": {
          "id": "19",
          "name": "Microsoft SQL account"
        }
      }
    },
    {
      "parameters": {
        "url": "=https://reqres.in/api/users/ {{$json[\"id\"]}}",
        "options": {}
      },
      "name": "HTTP Request",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 1,
      "position": [
        650,
        300
      ]
    },
    {
      "parameters": {
        "keepOnlySet": true,
        "values": {
          "number": [
            {
              "name": "id",
              "value": "={{$json[\"data\"][\"id\"]}}"
            }
          ],
          "string": [
            {
              "name": "name",
              "value": "={{$json[\"data\"][\"first_name\"]}} {{$json[\"data\"][\"last_name\"]}} (updated at {{new Date().toISOString()}})"
            }
          ]
        },
        "options": {}
      },
      "name": "Set",
      "type": "n8n-nodes-base.set",
      "typeVersion": 1,
      "position": [
        850,
        300
      ]
    },
    {
      "parameters": {
        "operation": "update",
        "table": "foo",
        "columns": "name"
      },
      "name": "Microsoft SQL1",
      "type": "n8n-nodes-base.microsoftSql",
      "typeVersion": 1,
      "position": [
        1050,
        300
      ],
      "credentials": {
        "microsoftSql": {
          "id": "19",
          "name": "Microsoft SQL account"
        }
      }
    }
  ],
  "connections": {
    "Start": {
      "main": [
        [
          {
            "node": "Microsoft SQL",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Microsoft SQL": {
      "main": [
        [
          {
            "node": "HTTP Request",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "HTTP Request": {
      "main": [
        [
          {
            "node": "Set",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Set": {
      "main": [
        [
          {
            "node": "Microsoft SQL1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}

This example workflow updates all items retrieved in step 1 based on their ID:

1 Like