IF "it only worked"

Hi All,

So I am using the IF node to determine whether the statement is true or false.

I am getting data from a google sheet and in one column there is either a yes or no and I want to determine if there is both.

It doesn’t matter how I add the conditions I either get false or I get both True and false.


I thought if I add ‘AND’ then all has to identified to either true or false?

Information on your n8n setup

  • **n8n version:1.81.4
  • **Running n8n via (Docker desktop):4.39.0

So your confusion is understandable, and is coming from the fact that the output of the previous node is 2 items. Items are “auto iterated”. A sheets node outputting 2 items will pass only 1 item at a time into the next node.

So you are only checking one of the values, not both.

There are ways to solve this tho, could you provide your logical setup JSON in a code block here on the forums to see?

EDIT:

Actually I am unsure if I understood the question. You said “if there is both” and assumed you meant it literally.

If the goal is to route “yes” cases to true branch and “no” cases to false branch?

Then you would simply have one check:
{{$json.Used}} contains (or equals) yes.

That would be it. All yes values will proc as true, all no values will proc as false.

@Craig_Ballard, the expression you used in the condition doesn’t match the provided input, so it will always return false.

To fix this, you can update your expressions:

For the first condition, change it to:

{{ $items.find(item => item.json.used === 'yes')?.json.used }}

And for the second condition, update it to:

{{ $items.find(item => item.json.used === 'no')?.json.used }}

This should give you the correct results based on your input!
Let me know if it helped!

@ThinkBot, @Knight Thanks for the reply.
I want to identify if there is ‘no’ in the column. And I only want a true or false outcome. But I get a true for ‘no’ and false for ‘yes’. @Knight when I update the if node, what do I put in the 3 columns? the fx, condition and the last fx?

@Craig_Ballard can you try this and tell me if it works?

No AND condition. That would be enough to use the true branch for yes, and false branch for no. (Unless you are still having issues)

I am unsure what @Knight is suggesting, as the above should work.

@ThinkBot, I get a true and false,

Is that what you wanted?

So you are checking for no, which output to the true path. And the yes output to the false path. That seems correct?

@ThinkBot, yes that is what I would also except.

However, I only want either a true or false statement and not both. the workflow will then detenand the next step.

The true and the false aren’t happening at the same time. Its iterative. One at a time.

The true branch will continue with the data that is true, and the false branch will continue with the data that is false.

Unless your goal is to check multiple row values at one time, the logic would be different.

what logic would I use in this case?

Could you provide the current workflow json by pasting after clicking the ‘</>’?

You could possibly use the merge node or a code node with run on all items. It really depends.

Will you always be providing just 2 entries from a sheet? Or could it be more than 2?

@ThinkBot

you made me think a bit more outside the box, I have tried lots of other nodes. I used a code node and used this code:

// Get data from the previous node (Google Sheets)
const items = $input.all();

// Check if any row has “Used” set to “no”
const hasNo = items.some(item => item.json.Used && item.json.Used.toLowerCase() === “no”);

// Return only the identification result
return [{ json: { “Used_No_Found”: hasNo } }];

this works and then I use a switch node to apply the path to use

Awesome! I am glad I could help. Would appreciate if you could mark my answer as solution. :slight_smile:

@ThinkBot, After all the extra step I tried it all ended up back at the same point of not being able to separate out the info I needed. So I ended updated the google sheet I am referring to with a new sheet with the number of yes or no in a column and then got the IF statement to look up this field. They really need to fix up the IF node to work like a If statement should. Thanks for all your help.

1 Like

Well glad you got it working to how you expect.

The If node works quite well.

I believe there may just be confusion or misunderstanding. I’d gladly try help with more information but I am unsure what your goals are.

One may be that the sheets node outputs items, items are not a single object according to n8n, items are iterated upon by the following nodes. So 10 rows from a google sheet will cause the next nodes to all run 10 times, once for each row. That means you cannot check multiple different row values at one time.

The IF node is designed to route based on a conditional, if you only wanted the flow to continue if Used was No, then you check if its No, and only route the True path from the IF node to the next node. And ignore the false route. That is safe to do, and will cause no issues.

Or if you were trying to have it not even output anything into a secondary path even if its ignorable, you can just use the Filter node instead, and will drop all items/rows that do not contain No if set to No. So only No values will continue in the scenario to the next node.

In either case, you will still be able to retrieve the current row details later in the flow respective to its item/item index. But with the code node method I suggested, that would be a little complicated if the suggested code did not work right to only pass No cases to the next node, since its being set to Run Once for all Items. (and would be used for different cases than to just pass if contains No)

But I still may be misunderstanding what your goal is.