Skip to main content

Notifications

Trigger conditions - filling the information gap

One of the commonly used triggers in a flow is "When a new email arrives".

The set of fields is very simple and it might be difficult to catch particular and only the one mail you so much care about. 
I have seen people building flows that are triggered on more emails than necessary and have huge conditions inside, testing if the mail which triggered the flow meets their requirements. It is not an efficient use of Power Automate to say at least.

 

There is a more simple way, a bit hidden from your eyes. You can use Settings and Trigger Conditions.
You can put several condition expressions, and you may wonder what an operator is used between them? You won't know unless you test it (unfortunately). I have already tested it for you and the answer is AND. So each line of Trigger conditions must return TRUE in order to execute the trigger.

 

Before we jump into building conditions there is a good article I can recommend you read: Power Automate Trigger Conditions made EASY by @MattWeston365. It is a good intro to how you can actually test your conditions.

 

Further down I will explain to you how to build trigger conditions

When building trigger conditions start with @ followed by functions. You can find the list of available functions in expressions in the Reference guide to using functions in expressions for Azure Logic Apps and Power Automate

A simple expression to test if the subject of the mail contains the word "Update", would look like:

 

 

 

 

@contains(triggerBody()?['subject'], 'Update')

 

 

 

 

 

If you would like to test if the subject of the mail contains the word "Update" or "Weekly", then the expression would be like this:

 

 

 

 

@or(contains(triggerBody()?['subject'], 'Update'),contains(triggerBody()?['subject'], 'Weekly'))

 

 

 

 

 

How about checking the subject and the mail body? To test if the subject of the mail contains the word "Update" or "Weekly", and the body contains a string "Community", the expression would look like this:

 

 

 

 

@and(or(contains(triggerBody()?['subject'], 'New'),contains(triggerBody()?['subject'], 'Closed')),(contains(triggerBody()?['body'], 'Community')

 

 

 

 

 

It is getting more complex so let me visualize it using the conditions within the flow and converting it into trigger conditions.

img3.png

@and(

(body contains "Community"),

or(

(subject contains "Update"),

(subject contains "Weekly")

)

)

 

To better understand those conditions, here is a real-life use case.

In IT Operations there is a SCOM server that monitors the infrastructure. One of the methods for subscribing to Alerts and Warning events notification is by sending them out by mail. There was some specific sort of mails coming from SCOM which should be checked and based on them an action should be triggered. 

The requirement was to catch all mails with Severity 2 and Priority 1 or Severity 2 and Priority 2. In addition, the subject should contain words: 'New' or 'Closed'. Severity and Priority are in the body of the message.

Here is the condition in a more readable format. 

 

'Severity: 2' AND  'Priority : 1'

OR

'Severity: 2' AND 'Priority: 2'

AND

subject contains "New" 

OR 

subject contains "Close"

 

When I first enter the flow I saw this condition:

 

Img1.png

 

Pretty complex, right? And the flow was running on every mail from hundreds coming from SCOM every day. 

 

Here is how you can do it using trigger expressions

Remember that there is an AND operator between each expression. Having this in mind let's break the above into two trigger expressions lines.

In the first expression, we will focus on the Severity and Priority. In the second expression, we will test a subject.

The first expression will look like this:

 

 

 

 

@or(and(contains(triggerBody()?['body'], 'Severity: 2'),contains(triggerBody()?['body'], 'Priority: 1')),and(contains(triggerBody()?['body'], 'Severity: 2'),contains(triggerBody()?['body'], 'Priority: 2')))

 

 

 

 

 

Add a second expression:

 

 

 

 

@or(contains(triggerBody()?['subject'], 'New'),contains(triggerBody()?['subject'], 'Close'))

 

 

 

 

 

Combined in the flow, in the trigger condition this will look like this:

img2.png

 

This way the flow has been triggered a few times a day compared to some hundred times before. 

 

Here is a little challenge for you!

Based on this article. would you be able to convert this example into one expression? If yes, share it with us within the comment below this article. 

 

Conclusion

Sometimes pictures and samples can explain a complex topic better than a thousand words. I hope that in this case, my example helped you to understand how to build trigger conditions in your future flows. If not, let me know and I will try to explain it better. 

If you recently build an interesting condition, please share it with us in the comments below, so more can get inspiration from your work. 

Comments

*This post is locked for comments

  • biboy852 Profile Picture biboy852 32
    Posted at
    Trigger conditions - filling the information gap

    Hi there, is it possible to use this to filter subject inside the attachment instead? 

  • CParsons09 Profile Picture CParsons09 141
    Posted at
    Trigger conditions - filling the information gap

    This is great information! I'm still failing on how to implement a trigger condition that looks at the email attachment and only proceeds if it's PDF. 

     

    This is not working: 

    contains(triggerOutputs()?['body/contentType'],'pdf')
     
    Any suggestions?
     
     
  • fideco Profile Picture fideco
    Posted at
    Trigger conditions - filling the information gap

    Amazing!

     

    Thank you so much @Michal! It just works now. 

     

     

     

     

     

  • Michal Z.  Profile Picture Michal Z. 145
    Posted at
    Trigger conditions - filling the information gap

    Hi @fideco 

    Take a look at the  Reference guide to using functions in expressions for Azure Logic Apps and Power Automate. The first example at this page is a function which returns a string in lowercase format. 

     

    toLower('<text>')

     

    Just use that in your condition and life becomes much easier. (I hope 🙂 )

     

    If you don't know how to use it, here is the example from my article. It converts to lower case whole subject and whole body and checks the result if it contains a specific words.

     

    @or(contains(toLower(triggerBody()?['subject']), 'new'),contains(toLower(triggerBody()?['subject']), 'close'))

     

     

  • fideco Profile Picture fideco
    Posted at
    Trigger conditions - filling the information gap

    Great information!

     

    I got this running on a shared mailbox, to create a Planner task when people email with a request for quote.

     

    Looks like the text is case sensitive. I need to make one for 'quote', one for 'Quote' and one for 'QUOTE'. Also for different syntax for quotation and even RFQ. 

     

    Any solution to make this trigger not case sensitive?

     

    Best regards,

    Stein Jone Hovland

  • Juan Simon Profile Picture Juan Simon 12
    Posted at
    Trigger conditions - filling the information gap

    Great Article!

    Juan Simon

  • AndreaMondello Profile Picture AndreaMondello 35
    Posted at
    Trigger conditions - filling the information gap

    GREAT article. Thanks, Michael. 

    One tip I recommend for shared flows: 

    Always add a comment to the first action to say "Trigger condition: [insert plain language description of the trigger condition here.]"

     

    This is important for someone coming after you who needs to understand why the flow may or may not be triggering, and how it works. 🙂

     

    Andrea Mondello

  • Michal Z.  Profile Picture Michal Z. 145
    Posted at
    Trigger conditions - filling the information gap

    Hi @Anonymous 

    👍Yes, you did it! 💪

    Have you found my article helpful?

  • David23 Profile Picture David23
    Posted at
    Trigger conditions - filling the information gap

    Hi Michal,

    How to build a trigger condition is described in detail. thanks for sharing this value able information I am really impressed.

     

      

  • Trigger conditions - filling the information gap

    @Michal ,

     

    Thank you for this rundown on trigger conditions. Very helpful!

     

    would the combined expression look like this?

     

    @and(or(and(contains(triggerBody()?['body'], 'Severity: 2'),contains(triggerBody()?['body'], 'Priority: 1')),and(contains(triggerBody()?['body'], 'Severity: 2'),contains(triggerBody()?['body'], 'Priority: 2'))),or(contains(triggerBody()?['subject'], 'New'),contains(triggerBody()?['subject'], 'Close')))

     

    Thanks, again!

     

    Kyle