Either/Or logic for collecting data into a node

Hi - I am trying to find a simple way to collect a gmail thread id from either of 2 gmail triggers in a separate node to then use that thread id later in my workflow.

With multiple gmail triggers, when I am only interested in one of them firing at a time, I can’t refer back to the thread id in the original trigger node because if that specific account didn’t fire, the thread id isn’t available and I get an error.

I have tried Merge node, If, Switch…nothing works and I am tearing my hair out with frustration. Even custom JS code node. Each time whatever happens EACH trigger input runs vs EITHER.

Any ideas? Basically I just need an either/or node which passes through the thread id of whichever Gmail trigger fired, but doesn’t wait for both. It’s that simple.

Thanks in advance for any support!

  "nodes": [
    {
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "leftValue": "",
            "typeValidation": "strict",
            "version": 1
          },
          "conditions": [
            {
              "id": "53849246-ad32-4845-9976-9f9688f5a6f2",
              "operator": {
                "type": "boolean",
                "operation": "true",
                "singleValue": true
              },
              "leftValue": "={{ $json.needsReply }}",
              "rightValue": "true"
            }
          ],
          "combinator": "and"
        },
        "options": {}
      },
      "id": "3ef14615-0045-404f-a21b-2c65a52f4be8",
      "name": "If Needs Reply",
      "type": "n8n-nodes-base.if",
      "position": [
        1040,
        340
      ],
      "typeVersion": 2
    },
    {
      "parameters": {
        "jsonSchema": "{\n  \"type\": \"object\",\n  \"properties\": {\n    \"needsReply\": {\n      \"type\": \"boolean\"\n      }\n  },\n  \"required\": [\"needsReply\"]\n}\n"
      },
      "id": "36968dd5-8d51-4184-a05a-587b6c95aa82",
      "name": "JSON Parser",
      "type": "@n8n/n8n-nodes-langchain.outputParserStructured",
      "position": [
        909.809175564116,
        498.91545624656004
      ],
      "typeVersion": 1
    },
    {
      "parameters": {
        "model": "gpt-4o",
        "options": {
          "responseFormat": "json_object",
          "temperature": 0
        }
      },
      "id": "2a64dce8-e2f0-475e-a366-a02084293aad",
      "name": "OpenAI Chat",
      "type": "@n8n/n8n-nodes-langchain.lmChatOpenAi",
      "position": [
        717,
        504.99999999999994
      ],
      "typeVersion": 1,
      "credentials": {
        "openAiApi": {
          "id": "VGShjcNu2xFylc7i",
          "name": "OpenAi account"
        }
      }
    },
    {
      "parameters": {
        "content": "## When I receive an Email\n",
        "height": 394.42512272977456,
        "width": 304.10628068244364
      },
      "id": "be892ff8-0981-4b34-9c93-7674ddd90360",
      "name": "Sticky Note",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -260,
        240
      ],
      "typeVersion": 1
    },
    {
      "parameters": {
        "content": "## ... that Needs a Reply\n",
        "height": 397,
        "width": 556
      },
      "id": "9d92839a-9ff2-436c-8abb-2f43e07c1ace",
      "name": "Sticky Note1",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        697,
        239
      ],
      "typeVersion": 1
    },
    {
      "parameters": {
        "content": "## Generate a Reply",
        "height": 400.08454375343996,
        "width": 333.19082443588354
      },
      "id": "3cd77609-684c-44e2-9cdc-9479cfd836bd",
      "name": "Sticky Note2",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        1269.809175564116,
        238.91545624656004
      ],
      "typeVersion": 1
    },
    {
      "parameters": {
        "content": "## generate a draft email in the conversation",
        "height": 395,
        "width": 326
      },
      "id": "b123cf31-767d-48bb-a0ba-79a69f6da585",
      "name": "Sticky Note3",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        1920,
        240
      ],
      "typeVersion": 1
    },
    {
      "parameters": {
        "model": "gpt-4-turbo",
        "options": {}
      },
      "id": "1a87c416-6b1c-4526-a2b6-20468c95ea0e",
      "name": "OpenAI Chat Model",
      "type": "@n8n/n8n-nodes-langchain.lmChatOpenAi",
      "position": [
        1420,
        520
      ],
      "typeVersion": 1,
      "credentials": {
        "openAiApi": {
          "id": "VGShjcNu2xFylc7i",
          "name": "OpenAi account"
        }
      }
    },
    {
      "parameters": {
        "prompt": "=Subject: {{ $('[email protected] trigger').first().json.Subject }}\nConversation:\n{{ $json.aggregatedSnippet }}",
        "messages": {
          "messageValues": [
            {
              "message": "Your task is to assess if the message requires a response. Return in JSON format true if it does, false otherwise.\nThe following emails don't require a response:\n- Marketing emails\n- 2FA or email confirmation emails\n- Promotional emails\n- Sales emails"
            }
          ]
        }
      },
      "id": "86017ff4-9c57-4b2a-9cd9-f62571a05ffd",
      "name": "Assess if message needs a reply",
      "type": "@n8n/n8n-nodes-langchain.chainLlm",
      "position": [
        720,
        340
      ],
      "typeVersion": 1.3
    },
    {
      "parameters": {
        "promptType": "define",
        "text": "=Subject: {{ $('[email protected] trigger').first().json.Subject }}\nConversation: {{ $('Code').item.json.aggregatedSnippet }}",
        "messages": {
          "messageValues": [
            {
              "message": "You're a helpful personal assistant and your task is to draft replies on my behalf to my incoming emails. Whenever I provide some text from an email, return an appropriate draft reply for it and nothing else.\nEnsure that the reply is suitable for a professional email setting and addresses the topic in a clear, structured, and detailed manner.\nDo not make things up.\n\nDetailed instructions:\n- Be concise and maintain a business casual tone.\n- Start with \"Hello,\", and end with \"Best,\"\n- When replying to yes-no questions, draft 2 responses: one affirmative and one negative separated by \" - - - - - - - OR - - - - - - - \"\n- If you don't know an answer, you can leave placeholders like \"[YOUR_ANSWER_HERE]\".\n- Don't use any special formatting, only plain text.\n- Reply in the same language as the inbound email.\n- Sign the email:\nBest,\nBarnaby"
            }
          ]
        }
      },
      "id": "cab1e7e5-93dc-4850-a471-e285cdbe2058",
      "name": "Generate email reply",
      "type": "@n8n/n8n-nodes-langchain.chainLlm",
      "position": [
        1300,
        380
      ],
      "typeVersion": 1.4
    },
    {
      "parameters": {
        "pollTimes": {
          "item": [
            {
              "mode": "everyMinute"
            }
          ]
        },
        "simple": false,
        "filters": {
          "q": "is:starred"
        },
        "options": {}
      },
      "id": "2a9ff08f-919a-41a8-980b-8c2bca3059e4",
      "name": "[email protected] trigger",
      "type": "n8n-nodes-base.gmailTrigger",
      "position": [
        -180,
        300
      ],
      "typeVersion": 1,
      "credentials": {
        "gmailOAuth2": {
          "id": "YJ77y3T1Mi7MO3VT",
          "name": "[email protected]"
        }
      }
    },
    {
      "parameters": {
        "resource": "draft",
        "subject": "=Re: {{ $('Merge').item.json.headers.subject }}",
        "emailType": "html",
        "message": "={{ $('Merge').item.json.text.replace(/\\n/g, \"<br />\\n\") }}",
        "options": {
          "threadId": "={{ $('Merge').item.json.threadId }}",
          "sendTo": "={{ $('Merge').item.json.headers.from }}"
        }
      },
      "id": "84b4d516-252e-444e-b998-2d4aa0f89653",
      "name": "[email protected] - Create Draft",
      "type": "n8n-nodes-base.gmail",
      "position": [
        2000,
        340
      ],
      "typeVersion": 2.1,
      "webhookId": "f0767f3b-3eb9-4fa9-9446-ca06f41a05dc",
      "credentials": {
        "gmailOAuth2": {
          "id": "YJ77y3T1Mi7MO3VT",
          "name": "[email protected]"
        }
      }
    },
    {
      "parameters": {
        "content": "## Merge data from either account",
        "height": 394,
        "width": 244
      },
      "id": "06d6241e-157c-4a09-9316-913e39a00bce",
      "name": "Sticky Note4",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        80,
        240
      ],
      "typeVersion": 1
    },
    {
      "parameters": {
        "rules": {
          "values": [
            {
              "conditions": {
                "options": {
                  "caseSensitive": true,
                  "leftValue": "",
                  "typeValidation": "strict",
                  "version": 2
                },
                "conditions": [
                  {
                    "leftValue": "={{ $('[email protected] trigger').item.json.To }}",
                    "rightValue": "[email protected]",
                    "operator": {
                      "type": "string",
                      "operation": "contains"
                    },
                    "id": "c6cb6db6-05d3-48e2-aa13-57008e0dc405"
                  }
                ],
                "combinator": "and"
              },
              "renameOutput": true,
              "outputKey": "[email protected]"
            },
            {
              "conditions": {
                "options": {
                  "caseSensitive": true,
                  "leftValue": "",
                  "typeValidation": "strict",
                  "version": 2
                },
                "conditions": [
                  {
                    "id": "8aed45dd-efa4-46a5-b225-89a3764e51ec",
                    "leftValue": "={{ $('[email protected] trigger').item.json.To }}",
                    "rightValue": "[email protected]",
                    "operator": {
                      "type": "string",
                      "operation": "contains"
                    }
                  }
                ],
                "combinator": "and"
              },
              "renameOutput": true,
              "outputKey": "[email protected]"
            }
          ]
        },
        "options": {}
      },
      "type": "n8n-nodes-base.switch",
      "typeVersion": 3.2,
      "position": [
        1700,
        420
      ],
      "id": "c78b5545-f6f3-4be8-b677-ea02ecd4834e",
      "name": "Switch"
    },
    {
      "parameters": {
        "content": "## parse the output to the correct gmail account",
        "height": 395,
        "width": 286
      },
      "id": "aeba1471-6c1b-498f-a17b-9d35a4676e5f",
      "name": "Sticky Note5",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        1620,
        240
      ],
      "typeVersion": 1
    },
    {
      "parameters": {
        "pollTimes": {
          "item": [
            {
              "mode": "everyHour"
            }
          ]
        },
        "filters": {
          "q": "is:starred"
        }
      },
      "id": "0bf07588-8cf7-42e6-b6cc-f51ec6198656",
      "name": "[email protected] trigger",
      "type": "n8n-nodes-base.gmailTrigger",
      "position": [
        -180,
        480
      ],
      "typeVersion": 1,
      "credentials": {
        "gmailOAuth2": {
          "id": "dNvutPI5OZi1yg4l",
          "name": "[email protected]"
        }
      }
    },
    {
      "parameters": {
        "resource": "draft",
        "subject": "=Re: {{ $('[email protected] trigger').item.json.Subject }}",
        "emailType": "html",
        "message": "={{ $('Generate email reply').item.json.text.replace(/\\n/g, \"<br />\\n\") }}\n",
        "options": {
          "threadId": "={{ $('[email protected] trigger').item.json.threadId }}",
          "sendTo": "={{ $('[email protected] trigger').item.json.From }}"
        }
      },
      "id": "d3a636f3-671e-4cf6-ab9b-2ad563bc0515",
      "name": "[email protected] - Create Draft",
      "type": "n8n-nodes-base.gmail",
      "position": [
        2000,
        520
      ],
      "typeVersion": 2.1,
      "webhookId": "f0767f3b-3eb9-4fa9-9446-ca06f41a05dc",
      "credentials": {
        "gmailOAuth2": {
          "id": "dNvutPI5OZi1yg4l",
          "name": "[email protected]"
        }
      }
    },
    {
      "parameters": {
        "resource": "thread",
        "operation": "get",
        "threadId": "={{ $json.threadId }}",
        "options": {}
      },
      "type": "n8n-nodes-base.gmail",
      "typeVersion": 2.1,
      "position": [
        360,
        400
      ],
      "id": "c9888b46-45c7-4496-a5e6-e8e295666fc6",
      "name": "Gmail",
      "webhookId": "24c96461-e448-41c7-bfba-b88d6fc94574",
      "credentials": {
        "gmailOAuth2": {
          "id": "dNvutPI5OZi1yg4l",
          "name": "[email protected]"
        }
      }
    },
    {
      "parameters": {
        "jsCode": "const messages = $input.all().map((item) => item.json);\nconst snippets = messages.flatMap((message) =>\n  message.messages.map((msg) => msg.snippet),\n);\nconst aggregatedSnippet = snippets.join(\"\\n\");\n\nreturn { aggregatedSnippet };\n"
      },
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        480,
        400
      ],
      "id": "1f5657c4-9d2c-4a0a-b447-44b2cd1d92ff",
      "name": "Code"
    },
    {
      "parameters": {
        "content": "## Merge data from either account",
        "height": 394,
        "width": 264
      },
      "id": "8b3ba884-eabd-45a1-b27a-953e0f885077",
      "name": "Sticky Note6",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        340,
        240
      ],
      "typeVersion": 1
    },
    {
      "parameters": {},
      "type": "n8n-nodes-base.merge",
      "typeVersion": 3,
      "position": [
        140,
        400
      ],
      "id": "14aef1ba-6894-4521-ba98-05a243f71339",
      "name": "Merge",
      "executeOnce": true,
      "alwaysOutputData": true
    },
    {
      "parameters": {
        "jsCode": "const items = $input.all();\nconst threadId = items.map((item) => ({ threadId: item?.json?.threadId }));\nreturn threadId;\n"
      },
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        100,
        600
      ],
      "id": "a168fb82-7bb8-4100-a6db-535c044edacd",
      "name": "Code1",
      "executeOnce": true
    }
  ],
  "connections": {
    "If Needs Reply": {
      "main": [
        [
          {
            "node": "Generate email reply",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "JSON Parser": {
      "ai_outputParser": [
        [
          {
            "node": "Assess if message needs a reply",
            "type": "ai_outputParser",
            "index": 0
          }
        ]
      ]
    },
    "OpenAI Chat": {
      "ai_languageModel": [
        [
          {
            "node": "Assess if message needs a reply",
            "type": "ai_languageModel",
            "index": 0
          }
        ]
      ]
    },
    "OpenAI Chat Model": {
      "ai_languageModel": [
        [
          {
            "node": "Generate email reply",
            "type": "ai_languageModel",
            "index": 0
          }
        ]
      ]
    },
    "Assess if message needs a reply": {
      "main": [
        [
          {
            "node": "If Needs Reply",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Generate email reply": {
      "main": [
        [
          {
            "node": "Switch",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "[email protected] trigger": {
      "main": [
        [
          {
            "node": "Merge",
            "type": "main",
            "index": 0
          },
          {
            "node": "Gmail",
            "type": "main",
            "index": 0
          },
          {
            "node": "Code1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "[email protected] - Create Draft": {
      "main": [
        []
      ]
    },
    "Switch": {
      "main": [
        [
          {
            "node": "[email protected] - Create Draft",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "[email protected] - Create Draft",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "[email protected] trigger": {
      "main": [
        [
          {
            "node": "Merge",
            "type": "main",
            "index": 1
          },
          {
            "node": "Gmail",
            "type": "main",
            "index": 0
          },
          {
            "node": "Code1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Gmail": {
      "main": [
        [
          {
            "node": "Code",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Code": {
      "main": [
        [
          {
            "node": "Assess if message needs a reply",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Merge": {
      "main": [
        [
          {
            "node": "Gmail",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "pinData": {},
  "meta": {
    "templateId": "2271",
    "templateCredsSetupCompleted": true,
    "instanceId": "bbe902f3896511981cf31efc1e813eda1a707c37e1607418947fbffda8d37b13"
  }
}

Could you provide the workflow json?

Select all, copy from n8n, then paste into the ``` when clicking the ‘</>’ button on the forums here.

There are a few solutions for this, depending on setup.