Share via


Filtering muliple emails using Search Query in Logic Apps

Question

Monday, April 3, 2017 4:41 AM

Hi,

I am retrieving multiple emails from an account using Logic Apps Action. I want to filter them on basis of "Subject" of the email.

I am not able to set the "Search Query" field of the Action. I would like to understand the syntax to set the same.

Below is the code

           "Get_emails": {
                "inputs": {
                    "host": {
                        "api": {
                            "runtimeUrl": "  ####URL###"
                        },
                        "connection": {
                            "name": "@parameters('$connections')['office365']['connectionId']"
                        }
                    },
                    "method": "get",
                    "path": "/Mail",
                    "queries": {
                        "fetchOnlyUnread": true,
                        "folderPath": "Inbox",
                        "includeAttachments": false,
                        "searchQuery": "@{triggerBody()?['Subject']}==\Payment\",
                        "skip": 0
                    }
                },
                "runAfter": {},
                "type": "ApiConnection"
            }

All replies (7)

Tuesday, April 4, 2017 2:23 AM âś…Answered | 1 vote

Hi methri,

The Exchange oData API don't support both:

Search requests

You can use the $search parameter to restrict the results of a request to the messages that match a search expression. Search strings are expressed using Advanced Query Syntax (AQS). The results are sorted by the date and time that the message was sent.

Note You can get up to 250 results from a $search request. You can use $search with only messages. Searching contacts and calendar events is not supported.

You cannot use $filter or $orderby in a search request. If you do, you will receive an error message like this one.

"fetchOnlyUnread": true transates to $filter=IsRead eq false

So in this case, you need to do variation of what Sujith is proposing:

I would do:

1) Get Unread emails

2) Array Filter on Subject

3) For each on the filter result.

This way, you only add one extra step on the filtering, and will only deal with items that are valide on the for each.

I hope this helps, Wagner.


Monday, April 3, 2017 5:28 AM | 1 vote

Hi Mehtri,

I would try this (assuming Office 365):

"searchQuery": "Subject:Payment"

based on the oData search functionality for Office 365

Unfortunately I can't test this right now, but hopefully that will put you in the right direction.

Cheers, Wagner.


Monday, April 3, 2017 7:20 PM

Wagner is correct - so something like subject:foo should work


Monday, April 3, 2017 7:23 PM

Hi Methri,

You can give a try as Wagner said,

You can try this way as well

Once you use the Get Emails Shape after that you can use For each and drop a condition and filter base on the Subject Like below

But it might be a Bit Lengthy,Just sharing it may be it helps you.

Thanks,

Sujith.

Sujith


Monday, April 3, 2017 11:34 PM

Hi Wagner,

I tried with the search Query

"searchQuery": "Subject:Payment"

but Logic Apps gives me an error response

{
  "status": 400,
  "message": "The query parameter '$filter' is not supported with '$search'.",
  "source": "127.0.0.1"
}

I am using Office 365 to get emails.

Thanks


Tuesday, April 4, 2017 5:41 AM

Hi Wagner, Thanks for your help.


Tuesday, April 4, 2017 8:08 AM

Hopefully that will do the trick. Let us know if it worked... ;-D

Cheers, Wagner.