RRP
RRP

Reputation: 45

get emails for last x number of hours in logic app

I am using "get emails v3" action in "outlook.com" for logic apps. I want to search emails based on subject filters in todays date only. I want following search criteria where I want emails between two times.

((receivedDateTime:@{utcNow()})) BETWEEN (receivedDateTime:@{addToTime(utcNow(), 1, 'Day')})

Is there any way to do that?

Upvotes: 0

Views: 2058

Answers (2)

SwethaKandikonda
SwethaKandikonda

Reputation: 8254

After reproducing it was clear that this is because of the format of receivedDateTime is not as same as the format of utcNow(). To achieve your requirement, you need to compare the dates in same format using condition. Below is how I to achieve your requirement using condition action.

Left side Condition

formatDateTime(items('For_each_2')?['receivedDateTime'],'dd/MM/yyyy')

Right side Condition

formatDateTime(addDays(utcNow(), -1), 'dd/MM/yyyy')

enter image description here

Here is the complete flow of my logic app

enter image description here

RESULTS:

enter image description here

Below is the code view of my Logic App

{
    "definition": {
        "$schema": "https://schema.management.azure.com/providers/Microsoft.Logic/schemas/2016-06-01/workflowdefinition.json#",
        "actions": {
            "For_each_2": {
                "actions": {
                    "Condition": {
                        "actions": {
                            "Append_to_array_variable": {
                                "inputs": {
                                    "name": "Email",
                                    "value": "@items('For_each_2')"
                                },
                                "runAfter": {},
                                "type": "AppendToArrayVariable"
                            }
                        },
                        "expression": {
                            "and": [
                                {
                                    "greaterOrEquals": [
                                        "@formatDateTime(items('For_each_2')?['receivedDateTime'],'dd/MM/yyyy')",
                                        "@formatDateTime(addDays(utcNow(), -1), 'dd/MM/yyyy')"
                                    ]
                                },
                                {
                                    "lessOrEquals": [
                                        "@formatDateTime(items('For_each_2')?['receivedDateTime'],'dd/MM/yyyy')",
                                        "@formatDateTime(utcNow(),'MM/dd/yyyy')"
                                    ]
                                }
                            ]
                        },
                        "runAfter": {},
                        "type": "If"
                    }
                },
                "foreach": "@body('Get_emails_(V3)')?['value']",
                "runAfter": {
                    "Initialize_variable": [
                        "Succeeded"
                    ]
                },
                "type": "Foreach"
            },
            "Get_emails_(V3)": {
                "inputs": {
                    "host": {
                        "connection": {
                            "name": "@parameters('$connections')['office365']['connectionId']"
                        }
                    },
                    "method": "get",
                    "path": "/v3/Mail",
                    "queries": {
                        "fetchOnlyFlagged": false,
                        "fetchOnlyUnread": false,
                        "fetchOnlyWithAttachment": false,
                        "folderPath": "Inbox",
                        "importance": "Any",
                        "includeAttachments": false
                    }
                },
                "runAfter": {},
                "type": "ApiConnection"
            },
            "Initialize_variable": {
                "inputs": {
                    "variables": [
                        {
                            "name": "Email",
                            "type": "array"
                        }
                    ]
                },
                "runAfter": {
                    "Get_emails_(V3)": [
                        "Succeeded"
                    ]
                },
                "type": "InitializeVariable"
            }
        },
        "contentVersion": "1.0.0.0",
        "outputs": {},
        "parameters": {
            "$connections": {
                "defaultValue": {},
                "type": "Object"
            }
        },
        "triggers": {
            "manual": {
                "inputs": {
                    "schema": {}
                },
                "kind": "Http",
                "type": "Request"
            }
        }
    },
    "parameters": {
        "$connections": {
            "value": {
                "office365": {
                    "connectionId": "/subscriptions/<SUBID>/resourceGroups/<RG>/providers/Microsoft.Web/connections/office365",
                    "connectionName": "office365",
                    "id": "/subscriptions/<SUBID>/providers/Microsoft.Web/locations/centralus/managedApis/office365"
                }
            }
        }
    }
}

Upvotes: 0

Skin
Skin

Reputation: 11277

If I've understood you correctly then this is the sort approach you need to take.

Get Emails v3

There's an additional parameter to that connector called Search Query.

In there, you can put an expression that looks like the following ...

received>@{formatDateTime(addDays(utcNow(), -1), 'MM/dd/yyyy')}

You're close, if you look at the doco though, the name of the field is different to what comes back in the response.

https://learn.microsoft.com/en-gb/graph/search-query-parameter

You may just need to play around with the formula to give you exactly what you want given the response is all in UTC. I think -1 day is the easiest but it may not be perfect for you.

Upvotes: 0

Related Questions