Fetching multiple rows of data for email filter

Hey! I am trying to acheieve an auto-filter for emails where I can get an output of specific emails details where the subject or body has specific keywords.

I am running a workflow where emails received every hour for a gmail node is sent, passed through a function node, which has certain defined keywords to filter. If the emails meet the criteria, it reuturns ‘true’, which is finally linked with a google sheet node, to return certain details about that email like Sender, Receiver, Subject, Body, date time, and ID.

Error Code:
Using the item method doesn’t work with pinned data in this scenario. Please unpin ‘Code’ and try again.

An expression here won’t work because it uses .item and n8n can’t figure out the matching item. You can either:

  • Add the missing information to the node ‘Code’
  • Or use .first(), .last() or .all()[index] instead of .item

By default the Json has the structure of “{{ $(‘Gmail Trigger1’).item.json.headers.subject }}” and this works perfectly well when the output is a single email, but moment I get multiple outputs I get this error. I have tried upadting it to .all(), and first/last also don’t suffice the requirement.

Workflow

Return Output

.item- works perfect in testing as there can be maximum single output or when there is single output in live executions but throws above error whenever the output is more than 1

.all()- Doesn’t return multiple rows of data which would be my ideal result, where every email meeting the criteria would be added to the sheet

.first/.last- doesn’t meet the use case we have

n8n version: [email protected]
Database : SQLite (default)
EXECUTIONS_PROCESS setting: Main
Running n8n: via desktop app
Operating system: Windows 10

It looks like your topic is missing some important information. Could you provide the following if applicable.

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

The simplest Solution for you @Subhayan_Chanda is,

  1. Add a Filter node after your Function node:
  • Set condition to: {{$json.meetsCriteria}} == true
  1. Add a Split In Batches node:
  • Set “Batch Size” to 1
  1. In your Google Sheets node, use direct references:
Sender: {{$json.headers.from}}
Subject: {{$json.headers.subject}}
Body: {{$json.textPlain}}
DateTime: {{$json.headers.date}}

This Should solve the problem… Have a good one…

2 Likes

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