Execute Command repeat

I would like to execute execute command for each record of a function that gives me all records how do you do it?

The execute command node will execute automatically as many times as input items it receives. Just connect the node that returns multiple inputs to the execute command node.

2 Likes

great, solved, I have the same problem with a mysql update; basically i would like to update multiple records with mysql passed by a function

Hey @marconte!

That should be handled by n8n itself. You just have to make sure that you’re returning multiple items from the previous node.

1 Like

it returns me multiple register function but mysql does not update all records

Hey @marconte , concerning the mysql node it updates correctly based on the items number.
here is an example for creating and updating a set of records.

{
  "nodes": [
    {
      "parameters": {
        "table": "TestTable",
        "columns": "id,name",
        "options": {}
      },
      "name": "MySQL",
      "type": "n8n-nodes-base.mySql",
      "typeVersion": 1,
      "position": [
        940,
        430
      ],
      "credentials": {
        "mySql": "MySQL creds"
      }
    },
    {
      "parameters": {
        "operation": "update",
        "table": "TestTable",
        "columns": "id,name"
      },
      "name": "MySQL1",
      "type": "n8n-nodes-base.mySql",
      "typeVersion": 1,
      "position": [
        1510,
        430
      ],
      "credentials": {
        "mySql": "MySQL creds"
      }
    },
    {
      "parameters": {},
      "name": "NoOp",
      "type": "n8n-nodes-base.noOp",
      "typeVersion": 1,
      "position": [
        1110,
        370
      ]
    },
    {
      "parameters": {
        "functionCode": "let items = []\nfor(i=0;i<5;i++){\n\n  items.push({json:{id:1000+i,name:`name${i+1000}`}});\n}\n\nreturn items;"
      },
      "name": "create items",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        700,
        430
      ]
    },
    {
      "parameters": {
        "functionCode": "\nfor(i=0;i<items.length;i++){\n\n  items[i].json.name= `Updated${items[i].json.name}` \n}\n\nreturn items;"
      },
      "name": "Updates items",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        1300,
        430
      ]
    }
  ],
  "connections": {
    "MySQL": {
      "main": [
        [
          {
            "node": "NoOp",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "NoOp": {
      "main": [
        [
          {
            "node": "Updates items",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "create items": {
      "main": [
        [
          {
            "node": "MySQL",
            "type": "main",
            "index": 0
          },
          {
            "node": "Updates items",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Updates items": {
      "main": [
        [
          {
            "node": "MySQL1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}

Please let me know if that help, otherwise you can forward your workflow for better debugging.

2 Likes

Great, really interesting, thanks for your attention

this form is important? Updated${items[i].json.name}
it must always be inserted before the mysql update

That is an example of updating record (changing the ‘name’ field), you can use the same concept to apply changes for your items. Let me know if that helps.