I am trying to add and remove labels from my emails in Gmail. I built a workflow that should do this but when I run it, it isn’t working. So I am searching for all messages that contain the order number. For this particular order, I have 6 emails. It finds all 6 messages. So this is working. Then I go to the loop over items and then off the loop branch it goes into removing the labels I don’t want anymore and adding the label I want. Then its supposed to repeat this for all messages. But it seems to only do it for the first message it finds. I can’t seem to figure out what I have done wrong here. It loops through 6 times but its using the same message. And the only message that gets updated is the first message. Ive been struggling with this for the past day. I can’t seem to find the issue. I am hoping someone out there can tell me what I did wrong.
Hey @jlhsolutions , I believe you add the label sucessfully but fail to remove the unwanted label(s).
The problem is with how you remove the labels. Note that the labelIds is an array (and you do not iterate or unnest them). How do you determine which one to remove? Also note that you can remove only one lable at a time.
Again, check the configuration of “GMail: Remove Shipping Labels”. You might have to expend you loop to achieve what you need. For example, you might need to add “Split Out” node just before this node so that you have access to all the labels of the message.
Hi @ihortom I have split out my labels into separate nodes. Not every message will these labels, but if it does, I want to remove them. Do I need to do an IF statement on each of these?
I added the split out node, but I am getting an error now. I’ve never used this node before and I can’t find a good example of how to use this.
How do I convert this number "“labelIds”:
“Label_1597807338159448749"” into the text that I have for my labels?
I thought I couldn’t remove a bunch of labels at once, which is why I broke the node out into multiple nodes. Can I do this? This shows the text name for the label. How do I convert it to the number as shown above.
Hey @jlhsolutions , great find! I learnt something from your post. You can scrap all my ideas and your workflow could be simplifed dramatically. No need for the Loop, you do not need Spli Out node, you just need 3 GMail nodes with the Resource “Message” (!)
For example,
When the resource is “Label” you create/delete labels in your mailbox, not the individual messages. Is that what you are after?
Hi @ihortom, This is what I originally tried. But then discovered it’s not working as I wanted it to.
So, I purchased the plugin for Gmail from Oskar so when I open a message, it forwards the email to n8n webhook. From there I use the code node to extract the order number out of the email. I then search for all emails that contain that order number. I then want to remove certain labels (Not Shipped, Partially Shipped, etc.) and mark them all as Received. I also want to remove any tracking carrier labels (UPS, FedEx, USPS, etc.) as I no longer need those.
For this particular order, I ended up with 6 emails so I want to be able to update them all so I don’t have to manually do it anymore. That is why I thought maybe I needed the loop as it finds all emails but then it starts with the first email but then doesn’t move through the list of emails found.
I hope this makes sense on what I am trying to accomplish.
Hey @jlhsolutions , I still believe that replacing the loop with the last two Gmail nodes (per my last workflow example) should do. Whether 1 item (email) or 6 items it does not matter. They all will be processed (lables removed and new added).
I have tried this with several different emails and I haven’t been able to make it work. I don’t know if its because they have separate ids? That was why I was trying loop.
Here is a screenshot of my node output to remove the labels I don’t want anymore.
As you can see there are 6 messages on the left. They all have a different json.id number. But on the right side, all it does it use the first message and does it 6 times.
Here is a screenshot for all the emails for this particular order.
The workflow has too many characters, so it won’t let me upload them. At this point, I am stumped as to why I can’t get this to work correctly. It seems so simple, but I must be missing something.
Your message ID reference is not correct which causes this issue. Replace it with just {{ $json.id }}. I believe “Find all messages” node immediately preceeds Gmail3 node as you have removed Loop node.