Wampie
Wampie

Reputation: 13

Trigger Azure Logic App on the third business day of every month

I'm having problems designing recurrence triggers with logic apps. As far as I know, the logic apps do not support CRON expressions, and running a daily trigger with conditions does not seem to be enough, so I am totally at an loss.

Edit:

To be more precise about my problem, The logic app is for moving files from one server to other and outside constraints dictate that this move should be completed once every month, and the move should happen on the third business day (monday-friday) of the month.

I'm currently pondering on either saving a global variable to tell me whether the app has succesfully ran this month, and using conditions to check every day whether it should run on the day, or starting the running a script which determines if current date is the third weekday of current month, and using that to determine if the logic app should execute or terminate.

Upvotes: 1

Views: 2517

Answers (2)

Skin
Skin

Reputation: 11197

There are three major options I can see, two of which I would entertain the thought of here. Main reason, due to cost, I've left out the use of the Inline Javascript action because it requires an integration account which will incur substantially more cost than either of the options below.

Prerequisites

The trigger should be a recurrence every day. It will still need to run, it's just whether or not it does anything. There's no getting away from that.

Initialise a variable of type String with the current date. I've included a formula that takes UTC and converts it to your local timezone, you'd just need to change it accordingly.

convertTimeZone(utcNow(), 'UTC', 'AUS Eastern Standard Time')

ICD

Option 1 - Azure Function

An Azure Function is by far the easiest way. Create a .NET function app (or whatever language you're comfortable with but you won't be able to use the code below) and from there, create a function with the following code called GetBusinessDayOfMonthForDate ...

public static async Task<IActionResult> Run(HttpRequest req, ILogger log)
{
    DateTime dateToProcess = DateTime.Parse(req.Query["Date"]);
    
    int businessDayCount = 0;
    var tempDate = dateToProcess;

    while (tempDate.Month == dateToProcess.Month)
    {                    
        businessDayCount += (tempDate.DayOfWeek.ToString().Substring(0, 1) != "S") ? 1 : 0;
        tempDate = tempDate.AddDays(-1);
    }

    return new OkObjectResult(businessDayCount.ToString());
}

Now call that from Logic Apps directly after the previous action ...

Action

AFResult

Option 2 - Standard Actions

The basic premise of this approach is the same as the Azure Function but naturally, it's a lot more long winded.

This is the JSON definition for that approach, it includes everything you need to test it in your own environment.

{
    "definition": {
        "$schema": "https://schema.management.azure.com/providers/Microsoft.Logic/schemas/2016-06-01/workflowdefinition.json#",
        "actions": {
            "Condition": {
                "actions": {},
                "else": {
                    "actions": {
                        "Terminate": {
                            "inputs": {
                                "runStatus": "Cancelled"
                            },
                            "runAfter": {},
                            "type": "Terminate"
                        }
                    }
                },
                "expression": {
                    "and": [
                        {
                            "equals": [
                                "@variables('Business Days Up to Today')",
                                3
                            ]
                        }
                    ]
                },
                "runAfter": {
                    "Until_Day_Index_=_-5": [
                        "Succeeded"
                    ]
                },
                "type": "If"
            },
            "Initialize_Business_Days_Up_to_Today": {
                "inputs": {
                    "variables": [
                        {
                            "name": "Business Days Up to Today",
                            "type": "integer",
                            "value": 0
                        }
                    ]
                },
                "runAfter": {
                    "Initialize_Temp_Date": [
                        "Succeeded"
                    ]
                },
                "type": "InitializeVariable"
            },
            "Initialize_Current_Date": {
                "inputs": {
                    "variables": [
                        {
                            "name": "Current Date",
                            "type": "string",
                            "value": "@{convertTimeZone(utcNow(), 'UTC', 'AUS Eastern Standard Time')}"
                        }
                    ]
                },
                "runAfter": {},
                "type": "InitializeVariable"
            },
            "Initialize_Day_Index": {
                "inputs": {
                    "variables": [
                        {
                            "name": "Day Index",
                            "type": "integer",
                            "value": 0
                        }
                    ]
                },
                "runAfter": {
                    "Initialize_Business_Days_Up_to_Today": [
                        "Succeeded"
                    ]
                },
                "type": "InitializeVariable"
            },
            "Initialize_Temp_Date": {
                "inputs": {
                    "variables": [
                        {
                            "name": "Temp Date",
                            "type": "string",
                            "value": "@variables('Current Date')"
                        }
                    ]
                },
                "runAfter": {
                    "Initialize_Current_Date": [
                        "Succeeded"
                    ]
                },
                "type": "InitializeVariable"
            },
            "Until_Day_Index_=_-5": {
                "actions": {
                    "Decrement_Current_Date": {
                        "inputs": {
                            "name": "Temp Date",
                            "value": "@{addDays(variables('Current Date'), variables('Day Index'))}"
                        },
                        "runAfter": {},
                        "type": "SetVariable"
                    },
                    "Decrement_Day_Index": {
                        "inputs": {
                            "name": "Day Index",
                            "value": 1
                        },
                        "runAfter": {
                            "Increment_variable": [
                                "Succeeded"
                            ]
                        },
                        "type": "DecrementVariable"
                    },
                    "Increment_variable": {
                        "inputs": {
                            "name": "Business Days Up to Today",
                            "value": "@if(and(greaterOrEquals(dayOfWeek(variables('Temp Date')), 1), lessOrEquals(dayOfWeek(variables('Temp Date')), 6)), 1, 0)"
                        },
                        "runAfter": {
                            "Decrement_Current_Date": [
                                "Succeeded"
                            ]
                        },
                        "type": "IncrementVariable"
                    }
                },
                "expression": "@equals(variables('Day Index'), -5)",
                "limit": {
                    "count": 60,
                    "timeout": "PT1H"
                },
                "runAfter": {
                    "Initialize_Day_Index": [
                        "Succeeded"
                    ]
                },
                "type": "Until"
            }
        },
        "contentVersion": "1.0.0.0",
        "outputs": {},
        "parameters": {},
        "triggers": {
            "Recurrence": {
                "evaluatedRecurrence": {
                    "frequency": "Day",
                    "interval": 1,
                    "timeZone": "AUS Eastern Standard Time"
                },
                "recurrence": {
                    "frequency": "Day",
                    "interval": 1,
                    "timeZone": "AUS Eastern Standard Time"
                },
                "type": "Recurrence"
            }
        }
    },
    "parameters": {}
}

Flow

At the base all of that, throw in a condition to check if the current business day variable equals 3, if it does, run your logic.

That's included in the JSON definition above.

One of those approach is what I'd be doing so I hope that answers your question.

Upvotes: 0

VenkateshDodda
VenkateshDodda

Reputation: 5506

You can use recurrence trigger in the logic app to trigger the workflow for every three weeks on Monday .

enter image description here

For more information about recurrence trigger you can refer this documentation.


Updated Answer:

As per the requirement, we have created a logic app with recurrence as a trigger & frequency as day. This workflow will fires every day and will validates whether current date is in between (3,4,5) or not. if the condition is succeeded it will further executes the logic app actions.

Here is the logic app that we have created:

enter image description here

Here is the code view of the logic app:

{
    "definition": {
        "$schema": "https://schema.management.azure.com/providers/Microsoft.Logic/schemas/2016-06-01/workflowdefinition.json#",
        "actions": {
            "Compose_2": {
                "inputs": "@formatDateTime(utcNow(), 'dd')",
                "runAfter": {},
                "type": "Compose"
            },
            "Condition": {
                "actions": {
                    "Compose": {
                        "inputs": "@utcNow()",
                        "runAfter": {},
                        "type": "Compose"
                    }
                },
                "expression": {
                    "or": [
                        {
                            "equals": [
                                "@int(outputs('Compose_2'))",
                                3
                            ]
                        },
                        {
                            "equals": [
                                "@int(outputs('Compose_2'))",
                                4
                            ]
                        },
                        {
                            "equals": [
                                "@int(outputs('Compose_2'))",
                                5
                            ]
                        }
                    ]
                },
                "runAfter": {
                    "Compose_2": [
                        "Succeeded"
                    ]
                },
                "type": "If"
            }
        },
        "contentVersion": "1.0.0.0",
        "outputs": {},
        "parameters": {},
        "triggers": {
            "Recurrence": {
                "evaluatedRecurrence": {
                    "frequency": "Day",
                    "interval": 1,
                    "schedule": {
                        "hours": [
                            "17"
                        ],
                        "minutes": [
                            16
                        ]
                    },
                    "startTime": "2021-12-28T17:14:00",
                    "timeZone": "India Standard Time"
                },
                "recurrence": {
                    "frequency": "Day",
                    "interval": 1,
                    "startTime": "2021-12-28T17:14:00",
                    "timeZone": "India Standard Time"
                },
                "type": "Recurrence"
            }
        }
    },
    "parameters": {}
}

Note: Execution of logic app on every day may result in more billing & also logic app doesn't support any CORN expressions. In these scenarios it is suggested to use Azure time trigger functions instead of logic app.

Upvotes: 1

Related Questions