Google Drive Folder: Search, Create If Missing, Then Always Return Folder ID

I’m building a flow that:

  1. Searches Google Drive for a folder with an exact name (e.g., company name).
  2. If found, great — I use its ID.
  3. If not found, I create it and then use that new ID.
  4. I always need the folder ID for the next step (e.g., to create subfolders like “Transcripts” and “Recaps”).

Because the Google Drive node does fuzzy matching, I’m using a Code node to filter the exact match. I then use an IF node to decide whether to create the folder.

The problem is:

When I connect both the “true” branch (found) and the “false” branch (create new) to the next step, only one branch executes and its result is passed forward. I can’t seem to consolidate both into a single variable that always holds the final folderId.

I’ve tried a bunch of things:

  • Combining outputs from both IF branches.
  • Setting a variable and then referencing it later.
  • Putting another Set or Merge node after both branches.
  • Using Run Once for All Items in a Code node to consolidate.

What is the correct pattern for: “search or create, then always use that ID” — especially when I need that folder as the parent for multiple subfolders?

Try use a “Merge” node with mode “Wait for All Inputs” after both IF branches. Or you can use a Function node to pick the folder ID after the IF node.

Basically, have the IF node run both branches as usual. Then, connect both branches to a Merge node (mode: ‘Merge By Index’) to join the outputs. After that, add a Function node that inspects both results, picks the one with the folder ID (found or created), and outputs just that ID. This Function node can return the correct folder ID no matter which branch had the data.

There is a solution.

Don’t use $json.id directly.

Use something like {{ $('Have Company?').item.json.id }}

In your case after first IF node

You can set the expression as
{{ $('Have Company?').item.json.id || $('Create Company Folder').item.json.id}}

So it will determine if the A || B.

Thank you for your reply. The merge note really doesn’t do what I would expect it to do. It doesn’t allow me to dynamically choose an input based on its values. I sort of have to explicitly tell it which value I want. What I guess makes sense if you’re simply combining two different tracks at the same time and you just still wanna wait for them both to complete before you do another thing.

But that’s really not what I’m trying to accomplish here.

Here is what i did!!

1 Like

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