I’m connecting an IMAP trigger on an email address. But I have an issue, when I set for instance 20 emails to unread, then they are taken into account as a single call by the IMAP trigger, resulting in 20 items in the imap call.
The issue is that I then want to manage each email independently, but only the first one is taken into account. How to manage this?
Because you tell it so by using $input.first()
Although it’s not the only issue here.
Longer answer:
The problem is that the Code node is not tuned properly.
You need to change its mode to Run once for each item and replace all instances of $input.first().json to $json
Its absolutely possible to achieve the same objective using Run once for all items mode. But then you’d need to pick all items using $input.all() and map each inbound item yourself.
FYI, $input.first() gives you access ony to the first item of all.
I recommed to read through docs (Data | n8n Docs and its subpages) to better understand the n8n peculiar data structure. It will save you a ton of time.
If the above resolves your question, please mark this post as a Solution.
Thanks a lot for your reply.
I’ll have a look at the link you provided.
Sure, it’s not the only issue here. I know that first() will just serve the first item, but it seems like if I try on all items, editing the code to get all items instead of the first one, then execution still consider only one item:
Here I have 7 items in checking-spam, but only the first one is saved in Execution Data2… Or maybe I’m not seeing something?
Also, I’m not sure it would be great to use a loop, as I have some ‘wait’ elements for multiple days at the end of my flow (sending an email, then another one 3 days later etc.)
→ If I loop on elements, it’ll wait until the loop is processed for each elements right?
I do not think this is a good strategy. Wait can fail. BTW, on some plans the duration of wait is limited.
The better strategy would be to keep a record of actions (using e.g. status for every entry or next step to do along with the action date), and then a build a process that would scan the registry daily and perform actions accordingly. This way you will also be able to watch the progress.
But this is an altogether different topic. I’d recommend to start a new one for this discussion to not confuse the future readers.
Yes, the Wait would block the workflow in its entirety.
Sure, you’re right. Thanks again for your time and answers.
As it’s emails I’m receiving I was not willing to share info on pinned data, but there you go:
I’ve pined the 2nd node as the first one contains binary data, I can’t pin it…
I understand that I might create kind of a loop for the whole process, with status right?
Actually this is related to the current topic: how to loop on IMAP elements, while not having to wait few days before the next item, as at the end of the loop (which is still long, I send emails, then wait 3 days, then resend an email).
I mean, $json.fieldName (cleanEmail, subject etc) are absolutely accessible and refer to the record that just arrived from the Loop node (in this case).
Avoid $(nodeName) notation at all cost. It is a source of bugs, a nightmare to debug and understand what’s going on. One change in the code can break all those references.
Instead you need to pass the required (and only objectively required) data down the pipeline explicitly. It sounds like an overhead but this effort will pay when you will want to add functionality to your workflow or someone else will need to look into it and fix anything.
You may ask “how do I achieve that when nodes like HTTP Request and AI agent simply do not allow to pass any data from before them?”
But it is a perfectly isolatable problem and I would recommend to decouple any following up from per-customer communication initialization, when you need any delays longer than 60 seconds.
Google Sheets, to me, does great job of holding this sort of data.
Then create a standalone workflow (workflows) that will start with a Schedule Trigger node (once a day), it will read the registry and follow a branch based on Next action if current date is the action date, for each record there. For valid (actionable today) actions it will do things and update the record with the next action and next action date.