Quick question on looping

Hi everyone, I need to get data for multiple clients, but I can’t do everything at once because I’ll lose track of the client ID in the workflow. So I tried to run it one client at a time, but the loop isn’t working.

Hey @Senthil1 hope all is well, welcome to the community.

Would you like to expand on your question and add some details to explain what the problem appears like. If you can, please embed your workflow here as well.

1 Like
{
  "nodes": [
    {
      "parameters": {},
      "type": "n8n-nodes-base.manualTrigger",
      "typeVersion": 1,
      "position": [
        -240,
        120
      ],
      "id": "32cc7a55-6972-43e5-9248-325a0c06d2c8",
      "name": "When clicking ‘Execute workflow’"
    },
    {
      "parameters": {
        "method": "POST",
        "url": "https://server.smartlead.ai/api/v1/campaigns/create?api_key=0968c628-3221-461d-bf6b-be487b0ff6ad_313tmzy",
        "sendBody": true,
        "bodyParameters": {
          "parameters": [
            {
              "name": "name",
              "value": "Test campaign"
            }
          ]
        },
        "options": {}
      },
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        440,
        120
      ],
      "id": "2f033ba7-da07-43ea-83ae-d73fee83d8f6",
      "name": "Create Campiagn",
      "executeOnce": false
    },
    {
      "parameters": {
        "method": "POST",
        "url": "=https://server.smartlead.ai/api/v1/campaigns/{{ $('Create Campiagn').item.json.id }}/sequences?api_key=0968c628-3221-461d-bf6b-be487b0ff6ad_313tmzy",
        "sendHeaders": true,
        "specifyHeaders": "json",
        "jsonHeaders": "{\n  \"Content-Type\": \"application/json\"\n}\n",
        "sendBody": true,
        "contentType": "raw",
        "rawContentType": "application/json",
        "body": "={\n  \"sequences\": [\n    {\n      \"seq_number\": 1,\n      \"seq_delay_details\": {\n        \"delay_in_days\": 1\n      },\n      \"variant_distribution_type\": \"MANUAL_EQUAL\",\n      \"lead_distribution_percentage\": 100,\n      \"winning_metric_property\": \"OPEN_RATE\",\n      \"seq_variants\": [\n        {\n          \"subject\": \"Quick Question\",\n          \"email_body\": \"<p>Hi,<br><br>Just wanted to ask you something interesting I noticed recently {{ $('Create Test').first().json.public_full_id }}.<br><br>Let me know if you have a minute.</p>\",\n          \"variant_label\": \"A\",\n          \"variant_distribution_percentage\": 100\n        }\n      ]\n    }\n  ]\n}\n",
        "options": {}
      },
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        980,
        -40
      ],
      "id": "db7df33e-d650-4771-b4d9-e028ef13702d",
      "name": "Sequence",
      "alwaysOutputData": true
    },
    {
      "parameters": {
        "method": "POST",
        "url": "=https://server.smartlead.ai/api/v1/campaigns/{{ $('Create Campiagn').item.json.id }}/settings?api_key=0968c628-3221-461d-bf6b-be487b0ff6ad_313tmzy",
        "sendHeaders": true,
        "specifyHeaders": "json",
        "jsonHeaders": "{\n  \"Content-Type\": \"application/json\"\n}\n",
        "sendBody": true,
        "contentType": "raw",
        "rawContentType": "application/json",
        "body": "={\n  \"name\": \"Siu\",\n  \"track_settings\": [\"DONT_TRACK_EMAIL_OPEN\", \"DONT_TRACK_LINK_CLICK\"],\n  \"stop_lead_settings\": \"REPLY_TO_AN_EMAIL\",\n  \"unsubscribe_text\": \"\",\n  \"send_as_plain_text\": true,\n  \"force_plain_text\": true,\n  \"enable_ai_esp_matching\": true,\n  \"follow_up_percentage\": 80,\n  \"client_id\": null,\n  \"add_unsubscribe_tag\": true,\n  \"auto_pause_domain_leads_on_reply\": true,\n  \"ignore_ss_mailbox_sending_limit\": false,\n  \"bounce_autopause_threshold\": \"5\",\n  \"out_of_office_detection_settings\": {\n    \"ignoreOOOasReply\": false,\n    \"autoReactivateOOO\": false,\n    \"reactivateOOOwithDelay\": null,\n    \"autoCategorizeOOO\": false\n  },\n  \"ai_categorisation_options\": [2, 7, 1]\n}\n",
        "options": {}
      },
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        1160,
        -40
      ],
      "id": "ba08e307-2d6c-40d4-85aa-882b513e3794",
      "name": "Settings"
    },
    {
      "parameters": {
        "method": "POST",
        "url": "=https://server.smartlead.ai/api/v1/campaigns/{{ $('Create Campiagn').item.json.id }}/schedule?api_key=0968c628-3221-461d-bf6b-be487b0ff6ad_313tmzy",
        "sendHeaders": true,
        "specifyHeaders": "json",
        "jsonHeaders": "{\n  \"Content-Type\": \"application/json\"\n}\n",
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "{\n  \"timezone\": \"Asia/Kolkata\",\n  \"days_of_the_week\": [3],\n  \"start_hour\": \"00:01\",\n  \"end_hour\": \"23:59\",\n  \"min_time_btw_emails\": 3,\n  \"max_new_leads_per_day\": 50,\n  \"schedule_start_time\": \"2025-06-18T18:42:00.000Z\"\n}\n",
        "options": {}
      },
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        1360,
        -40
      ],
      "id": "f896290f-a66d-4ab4-bf5e-e21726129b92",
      "name": "Schedule"
    },
    {
      "parameters": {
        "method": "POST",
        "url": "https://api.mailreach.co/api/v1/spam_tests",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "x-api-key",
              "value": "3MybTf4H8q9o9EgtjWr9zTtc"
            }
          ]
        },
        "sendBody": true,
        "bodyParameters": {
          "parameters": [
            {
              "name": "seedlist",
              "value": "1"
            }
          ]
        },
        "options": {}
      },
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        200,
        120
      ],
      "id": "43f0d75f-7470-4956-b542-7444bafb1e3e",
      "name": "Create Test"
    },
    {
      "parameters": {
        "jsCode": "const testResults = $('Create Test').first().json.results || [];\n\nconst lead_list = testResults.map(result => {\n  return {\n    email: result.email || '',\n    first_name: result.first_name || '',\n    last_name: result.last_name || ''\n  };\n});\n\nreturn [{\n  json: {\n    lead_list\n  }\n}];\n"
      },
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        600,
        240
      ],
      "id": "a992e01f-171c-4964-9048-2c8b3a191c80",
      "name": "Code"
    },
    {
      "parameters": {
        "amount": 3,
        "unit": "seconds"
      },
      "id": "ecc26f3c-024b-45d2-b258-80df71bb14fe",
      "name": "Wait Delay",
      "type": "n8n-nodes-base.wait",
      "typeVersion": 1,
      "position": [
        740,
        40
      ],
      "webhookId": "c3b001e3-fb83-4957-81be-bcaef9dd3fdc"
    },
    {
      "parameters": {
        "method": "POST",
        "url": "=https://server.smartlead.ai/api/v1/campaigns/{{ $('Create Campiagn').item.json.id }}/leads?api_key=0968c628-3221-461d-bf6b-be487b0ff6ad_313tmzy",
        "sendBody": true,
        "bodyParameters": {
          "parameters": [
            {
              "name": "lead_list",
              "value": "={{ $json.lead_list }}"
            },
            {
              "name": "settings",
              "value": "={{ $json.settings }}"
            }
          ]
        },
        "options": {}
      },
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        1180,
        240
      ],
      "id": "fda05a27-0de4-48a6-bf47-f55c307c024a",
      "name": "Adding Leads"
    },
    {
      "parameters": {
        "method": "POST",
        "url": "=https://server.smartlead.ai/api/v1/campaigns/{{ $('Create Campiagn').item.json.id }}/email-accounts?api_key=0968c628-3221-461d-bf6b-be487b0ff6ad_313tmzy",
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={\n  \"email_account_ids\": [{{ $('Get row(s) in sheet').item.json.id }}]\n}\n ",
        "options": {}
      },
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        1540,
        -40
      ],
      "id": "bae2c370-3a8d-429d-8ba2-24bc9643af79",
      "name": "Adding Email Acc"
    },
    {
      "parameters": {
        "method": "POST",
        "url": "=https://server.smartlead.ai/api/v1/campaigns/{{ $('Create Campiagn').item.json.id }}/status?api_key=0968c628-3221-461d-bf6b-be487b0ff6ad_313tmzy",
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "{\n  \"status\": \"START\"\n}\n",
        "options": {}
      },
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        1400,
        240
      ],
      "id": "873b1f0f-52bd-459d-ac02-2f8c294aa43b",
      "name": "HTTP Request"
    },
    {
      "parameters": {
        "documentId": {
          "__rl": true,
          "value": "https://docs.google.com/spreadsheets/d/1y4M7USC-bSuD1EIlEt4ckukkWbQBSQrVS_WYuy0O3c0/edit?gid=177470616#gid=177470616",
          "mode": "url"
        },
        "sheetName": {
          "__rl": true,
          "value": 177470616,
          "mode": "list",
          "cachedResultName": "Email_test",
          "cachedResultUrl": "https://docs.google.com/spreadsheets/d/1y4M7USC-bSuD1EIlEt4ckukkWbQBSQrVS_WYuy0O3c0/edit#gid=177470616"
        },
        "options": {}
      },
      "type": "n8n-nodes-base.googleSheets",
      "typeVersion": 4.6,
      "position": [
        -20,
        120
      ],
      "id": "6b55f2f5-acd5-4424-a353-f908265e1df8",
      "name": "Get row(s) in sheet",
      "credentials": {
        "googleSheetsOAuth2Api": {
          "id": "f5AGcEtUAzugCOfE",
          "name": "Google Sheets account"
        }
      }
    }
  ],
  "connections": {
    "When clicking ‘Execute workflow’": {
      "main": [
        [
          {
            "node": "Get row(s) in sheet",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Create Campiagn": {
      "main": [
        [
          {
            "node": "Code",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Sequence": {
      "main": [
        [
          {
            "node": "Settings",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Settings": {
      "main": [
        [
          {
            "node": "Schedule",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Schedule": {
      "main": [
        [
          {
            "node": "Adding Email Acc",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Create Test": {
      "main": [
        [
          {
            "node": "Create Campiagn",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Code": {
      "main": [
        [
          {
            "node": "Wait Delay",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Wait Delay": {
      "main": [
        [
          {
            "node": "Sequence",
            "type": "main",
            "index": 0
          },
          {
            "node": "Adding Leads",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Adding Leads": {
      "main": [
        [
          {
            "node": "HTTP Request",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Get row(s) in sheet": {
      "main": [
        [
          {
            "node": "Create Test",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "pinData": {},
  "meta": {
    "templateCredsSetupCompleted": true,
    "instanceId": "4aa381aa80cf54630eb383addc95fe30efe69c35335090c5cff199b5f9285b15"
  }
}```

This is my workflow: it creates an inbox placement test and campaign to get results for reseller or outsourced email accounts.

Problem: It only runs for one email account. When I add multiple email accounts, it breaks and shows an error saying it can’t decide which data to pick.

Goal: I want this workflow to test multiple email accounts at the same time.

Hey @Senthil1,
it’s a bit hard to test your workflow without proper credentials but from what I can see, your Code node is set only return a single item which will make the rest of the workflow execute only once. It’s tricky because it looks like you are assembling a list but this list ends up as a property of a single item. If you want your Code node to output each lead as a separate n8n item, then they have to have their own json property. So something like this would work:

return lead_list.map(item => ({
  json: item
}));

Alternatively, you can use Split Out node to split your list into individual items.

1 Like

What the workflow does:

  • Takes multiple email accounts from a Google Sheet
  • Creates an inbox placement test for each email
  • Gets back a test ID and seed list
  • Creates a campaign for each test
  • Updates the campaign with the email, seed list, and a unique key

The problem:
It only works for one email. When I add more, it breaks during the campaign update step. n8n can’t match the right campaign ID, email, and seed list together.

What I want to do:

  • Process one test and one campaign at a time (looping)
  • Or use a better method if looping isn’t the best approach

Question:
How can I set up the loop in n8n, or what’s the best way to fix this?

Respond

If everything in the workflow happens once for each item in the sheet including the “Create campaign” and “Code” nodes, then you probably want to change the mode of the Code node form “Run Once for All Items” to “Run Once for Each Item”.

Got it, i’ll check.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.