Workflows play a vital role in automating business processes and ensuring timely communication with stakeholders. However, there are instances when sending notifications or performing certain actions during working hours becomes critical, especially to avoid inconveniencing recipients during non-working hours or weekends. To address this need, a custom workflow step has been designed to calculate the next working day after a specific interval. 

Let's take a look at how it works.

CalculateNextWorkingDay Steps

The custom step, known as "CalculateNextWorkingDay," is an essential addition to any workflow involving time-sensitive communications. It accepts two input parameters – "DateTime StartDate" and "int Interval" – and provides one output parameter named "DateTime CalculatedDate." 

When implementing the workflow, simply set the "StartDate" to the desired date and the "Interval" to the number of working days to be skipped before the next working day. The "CalculatedDate" will then be automatically populated with the date of the next working day, accounting for weekends and business hours. 

Let's explore further into the workings of the custom step "CalculateNextWorkingDay": 

1. Input Parameters: 

StartDate: This parameter holds the initial date from which the calculation begins. It accepts a date and time value, allowing flexibility in scheduling the workflow. 

Interval: The "Interval" parameter specifies the number of working days to skip before reaching the next working day. This input ensures precise control over the workflow's execution. 

2. Output Parameter: 

CalculatedDate: After the custom step is executed, the "CalculatedDate" output parameter will be populated with the date of the next working day as per the specified interval. 

3. Handling Non-Working Hours: 

The custom step accounts for non-working hours to ensure that notifications and actions are not scheduled during inconvenient times. For instance: 

  • If the "StartDate" falls after 6:00 PM, the custom step automatically adjusts it to 8:00 AM the next day, avoiding communication during late evening hours.
  • Similarly, if the "StartDate" is before 8:00 AM, it is set to 8:00 AM on the same day, preventing actions from being initiated too early in the morning. 

4. Handling Weekends: 

The custom step also considers weekends, ensuring that notifications are not scheduled on Saturdays and Sundays. 

  • If the "StartDate" falls on a Saturday, the custom step adjusts it to 8:00 AM the following Monday, effectively skipping the weekend.
  • If the "StartDate" falls on a Sunday, it is set to 8:00 AM on the subsequent Monday. 

5. Calculating the Next Working Day: 

The custom step efficiently calculates the next working day after considering non-working hours and weekends.

  • It calculates the number of complete weeks in the specified interval to avoid counting weekends.
  • Then, it determines the remaining days after the complete weeks and adds them to the calculated date.
  • If the final date falls on a Saturday or Sunday, the custom step further adjusts it to the following Monday, ensuring notifications are delivered on the next working day.

Code Example

Here is the code example: 

The step has two input parameters:

  • 'DateTime StartDate'
  • 'int Interval'

And one output parameter:

  •   'DateTime CalculatedDate' 

To calculate the next working day after an interval of 'X' days, you simply need to set the process timeout using this date. This ensures that your emails and notifications won't annoy people at midnight or early in the morning.

The 'CalculateNextWorkingDay' class is shown below: 

public class CalculateNextWorkingDay : CodeActivity 
        [Input("Date Time")] 
        public InArgument<DateTime> StartDate { get; set; } 
        public InArgument<int> Interval { get; set; } 
        [Output("Calculated Date")] 
        public OutArgument<DateTime> CalculatedDate { get; set; } 

        protected override void Execute(CodeActivityContext executionContext) 
                var interval_days = Interval.Get<int>(executionContext); 
                var startDate = StartDate.Get<DateTime>(executionContext); 
                if (startDate.Hour >= 18) 
                    startDate = startDate.Date.AddDays(1).AddHours(8); //set 8:00 next day 
                else if (startDate.Hour < 8) 
                    startDate = startDate.Date.AddHours(8); //set 8:00 
                if (startDate.DayOfWeek == DayOfWeek.Saturday) 
                    startDate = startDate.Date.AddDays(2).AddHours(8); 
                else if (startDate.DayOfWeek == DayOfWeek.Sunday) 
                    startDate = startDate.Date.AddDays(1).AddHours(8); 
                int weeks = interval_days / 5; 
                int days = interval_days % 5; 

                DateTime calculatedDate = startDate.AddDays(weeks * 7);                 

                if ((int)calculatedDate.DayOfWeek + days > 5) 
                    calculatedDate = calculatedDate.AddDays(days + 2); //skip weekend 
                    calculatedDate = calculatedDate.AddDays(days); 

                CalculatedDate.Set(executionContext, calculatedDate); 
            catch (Exception ex) 
                EventLog.WriteEntry("code error", "Error occured in " + ex.InnerException.ToString(), EventLogEntryType.Error); 


By incorporating the "CalculateNextWorkingDay" custom step into your workflows, you can streamline communication, prevent unnecessary interruptions, and ensure that important notifications are delivered promptly during working hours. This user-friendly enhancement demonstrates how small adjustments can significantly impact the effectiveness of automated workflows, making it a valuable addition to any business process. 

Feel free to reach out if you need any assistance. We are always ready to help.