Second Loop breaking the workflow and losing data

Hi, new here…

I have process that works well (shared below), it takes a list of companies, loops through each one after doing a google search to find the company’s ceo and writing it to a google sheet.

I want to add a step that uses openai to check if it thinks the ceo google returned is correct. If it is, continue and write to sheets then move onto the next company, if not to loop and try the second returned search from google.

The second loop is really screwing it up, after the second IF I seem to lose the data I want to hold and loop through in the Code node (if ceo not found) and it and it also seems to then break the original working loop.

Any help or pointers to a similar problem (with a worked solution) much appreciated!

This is the working that works…

…and this is the one that doesn’t

Information on your n8n setup

  • n8n version:
  • Database (default: SQLite):
  • n8n EXECUTIONS_PROCESS setting (default: own, main):
  • Running n8n via (Docker, npm, n8n cloud, desktop app):
  • Operating system:
1 Like

Hi @jmf,

It sounds like you are losing some of the data from the if statement. Any fields that you want to pass through the if statement need to be included. Pass the field as json field exists, so the if statement still works.

If you are still having trouble, add your json in a code block so I can download the workflow.

Best,

Robert

1 Like

hello @jmf

Nested loops wouldn’t work with default settings. Better to use a sub-workflow instead (move all the loop part inside a new workflow and call the workflow).

Thanks barn4k, how do I go about calling the sub-workflow (I take it it lives in the same project still)?

Are there non default settings I can use that would make it work?

TIA

Create new workflow like this (don’t forget to set the variables, which you need, for the first node)

And replace the loop part inside the parent workflow with the node “Execute Workflow”

it’s too complicated…

I have this kind of setup, I’m just unsure of the syntax to define the field name in the Workflow Input Schema (first node of sub-flow) and also how to be sure or define the right data is being past from the Execute Workflow node in the parent?

Define the fields in the trigger node (sub workflow), shich you will need. Then save the workflow. After that your execute workflow node in the parent workflow will contain the placeholders to fill

that means one of the nodes in the sub workflow can’t access the searchParameters. Seems you didn’t pass them to it

Thanks barn4k – I’ve tried passing it q but it’s saying it’s undefined, any ideas?


{
  "errorMessage": "Cannot read properties of undefined (reading 'q') [line 5]",
  "errorDescription": "TypeError",
  "errorDetails": {},
  "n8nDetails": {
    "n8nVersion": "1.84.3 (Cloud)",
    "binaryDataMode": "filesystem",
    "stackTrace": [
      "TypeError: Cannot read properties of undefined (reading 'q')",
      "    at VmCodeWrapper (evalmachine.<anonymous>:5:43)",
      "    at evalmachine.<anonymous>:38:2",
      "    at Script.runInContext (node:vm:149:12)",
      "    at runInContext (node:vm:301:6)",
      "    at result (/usr/local/lib/node_modules/n8n/node_modules/@n8n/task-runner/dist/js-task-runner/js-task-runner.js:162:63)",
      "    at new Promise (<anonymous>)",
      "    at JsTaskRunner.runForAllItems (/usr/local/lib/node_modules/n8n/node_modules/@n8n/task-runner/dist/js-task-runner/js-task-runner.js:156:34)",
      "    at JsTaskRunner.executeTask (/usr/local/lib/node_modules/n8n/node_modules/@n8n/task-runner/dist/js-task-runner/js-task-runner.js:117:26)",
      "    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)",
      "    at async Object.waitingForSettings (/usr/local/lib/node_modules/n8n/node_modules/@n8n/task-runner/dist/task-runner.js:256:17)"
    ]
  }
}

Please, share the workflow

Thanks, I’ve removed my serper api key – not sure how to create and call the credential from a POST node.

I tried creating q as a separate input field and it made no difference, along with a bunch of other changes but I just can’t get past that error.

That’s not the workflow with an issue. Please, share the sub-workflow :slight_smile:

Sorry barn4k – you can tell I’m new here…this workflow isn’t finished yet, but I’m just trying to get past this error first. Thank you.

It’s the Code node that’s causing the issue, I’ve been looking in the wrong place.

What I still don’t understand though is why does it appear that the input from the Find the Contact node (main workflow) does not appear to get sent to the first node on my sub-workflow ?

The issue was with the variable names. You were referring the wrong variables.

The correct code node

2 Likes

This has been insanely helpful.

It really wasn’t clear to me how you had to establish the variables in the sub-flow node (and map) in order to accept them.

The bigger revelation was that you can only see the data being passed in the input and output if you go into the “view sub-execution” view via the main workflow Execute Workflow node – I could then see all of the executions and the schema/table/JSON being in and output.

Thanks again!

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