Skip to content

Let's POWER Automate

From no-code to low-code

Menu
  • Expressions
  • Filters
  • General
  • Problems
  • Triggers
  • Application specific solutions
    • Excel
    • Forms
    • Planner
    • Outlook
    • SharePoint
    • Teams
  • Resources
Menu

Run Power Automate flow only if email contains a specific attachment

Posted on January 30, 2022January 30, 2022 by Tom

“I don’t want hundreds of flow runs, the Power Automate flow should run only if the email contains attachment with a specific name!”


When you build a flow to process incoming emails, you’ll start from the ‘When a new email arrives’ trigger. This trigger allows you to do some basic configuration, e.g. process only emails with attachments, or with a specific subject. But what about the settings not available directly in the action? What if you’re not interested in all emails with attachments, but only emails with a specific one? How do you tell the flow to run only if the attachment has some name? You surely don’t want to solve it with the ‘Condition’ action.

Power Automate email attachment run

Add a trigger condition

As for any other trigger, you’re not limited only to the available options. You can add your own trigger condition to specify when the flow should run. In this case it’s when the email contains an attachment with a specific name.

If you check the trigger output, you’ll see that the attachments are an array within the trigger body. Each of the objects in this array then contains the attachment information. That’s the information you must access in the trigger condition.

Following the article on extracting values from JSON, you’ll end with the expression below. It’ll return the whole content of the “attachments” array from the trigger.

triggerOutputs()?['body']?['attachments']

Now, when you have the attachments information, you can start looking for a specific attachment.

Search for a full attachment name

If you’re looking for a full attachment name, you can use a very simple, not very elegant solution. Convert the array into a string, and search if it contains the full attachment name incl. the extension, e.g.

@contains(string(triggerOutputs()?['body']?['attachments']),'Ultimate PA expressions cheatsheet.docx')
Power Automate email attachment run

Since you’re searching for a full name, there’s no risk that the array would contain exactly the same string in another place. It doesn’t matter if you search only among the file names, or if you keep also the other information.

Search for a part of a name

When you search only for a single word, or if you want to use another condition than ‘contains’, it’s better to extract the file names. With only the file name you can use also the startsWith(…) and endsWith(…) conditions.

In this situation you can’t preprocess the attachments in any way. You can’t add another action to extract the attachment names, you must access them directly in the trigger. Therefore, you’ll need the xpath(…) expression. The approach is the same as when exporting multiple choice columns to csv. Take the array, and extract only a specific value – here it’s the attachment ‘name’.

xpath(xml(json(concat('{"body":{"value":', triggerOutputs()?['body']?['attachments'], '}}'))), '/body/value/name/text()')

Convert it into a string for easier use in the trigger condition with the expression below. It’ll return attachment names separated by comma.

join(xpath(xml(json(concat('{"body":{"value":', triggerOutputs()?['body']?['attachments'], '}}'))), '/body/value/name/text()'),',')

All that’s left is to turn it into a trigger condition, e.g. to check if the attachment name starts with the string ‘Ultimate’.

@startswith(join(xpath(xml(json(concat('{"body":{"value":', triggerOutputs()?['body']?['attachments'], '}}'))), '/body/value/name/text()'),','), 'Ultimate')
Power Automate email attachment run

Note: just be careful about the startsWith(….) condition as it’ll check only the name of the first attachment. The same applies to endsWith(…) and the last attachment.

Summary

If you want to run Power Automate flow only for email with a specific attachment, you’ve got two options. You can run the flow for all incoming emails and evaluate them in a flow, or you can add a trigger condition to reduce the flow runs.

Don’t be worried only because the trigger action doesn’t have a field for that. With trigger conditions you can do much more, as already shown when searching for multiple subjects. Just extract the attachment information from a trigger, and build a trigger condition that’ll check it. This way you can save flow runs, your flow will be simpler, and the flow run history will be easy to read.


Do you struggle with the various expressions, conditions, filters, or HTTP requests available in Power Automate?

I send one email per week with a summary of the new solutions, designed to help even non IT people to automate some of their repetitive tasks.

All subscribers have also access to resources like a SharePoint Filter Query cheat sheet or Date expressions cheat sheet.

Zero spam, unsubscribe anytime.

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Now available:

The Ultimate Power Automate expressions cheat sheet
Equip yourself with the tool to translate your thoughts into Power Automate expressions!

Do you struggle with the various expressions, conditions, filters, or HTTP requests available in Power Automate?

I send one email per week with a summary of the new solutions, designed to help even non IT people to automate some of their repetitive tasks.

All subscribers have also access to resources like a SharePoint Filter Query cheat sheet or Date expressions cheat sheet.

Zero spam, unsubscribe anytime.

Hello and welcome!

My name is Tom and I'm a business process automation consultant and Microsoft MVP living in the Czech Republic. I’ve been working with Microsoft technologies for almost 10 years, currently using mainly Power Automate, SharePoint, Teams, and the other M365 tools.

I believe that everyone can automate part of their work with the Power Automate platform. You can achieve a lot by "clicking" the flows in the designer, but you can achieve much more if you add a bit of coding knowledge. And that's what this blog is about.

To make the step from no-code Power Automate flows to low-code flows: using basic coding knowledge to build more complex yet more efficient flows to automate more of your daily tasks.

  • Format results of date calculation in SharePoint columnJune 29, 2022
  • Why is your Power Automate flow creating duplicatesJune 26, 2022
  • How to create a unique identifier in your Power Automate flowJune 22, 2022
  • How to assign custom SharePoint permission level with Power AutomateJune 19, 2022
  • Remove permissions from a specific SharePoint user with Power AutomateJune 15, 2022

Power Automate blogs worth visiting

Damien Bird
Dennis (Expiscornovus)
Paul Murana

© 2022 Let's POWER Automate | Powered by Superbs Personal Blog theme