Tip #1144: How to add business days

Over six months ago I got a tip from Sergio “Behind The Wall” Macías, part of the the driving force behind Spanish-speaking Dynamics 365 community, on how to create a custom workflow activity that adds business days to a specific date in Dynamics 365. In the essence, the calculations are something like this:

while(businessDaysToAdd > 0)
{
  starDate = starDate.AddDays(1);
  if(IsBusinessDay(startDate))
   businessDaysToAdd--;
}

The heart of calculations is, of course, the IsBusinessDay method that would skip obvious Saturdays and Sundays and then go through a holiday calendar to work out if the employees eat turkey instead of helping customers or if it’s business as usual (e.g. help customers eat turkey). (“Go through a holiday calendar” sounds much easier than it actually, is, trust me). I filed the tip with the intention to come back to it until the opportunity presents itself, i.e. until I have a chance to test-drive it using customer’s money, which happened just recently. Because of the vast Dynamics 365 community resources, regardless what the challenge is, I rarely feel the need to either create custom code from scratch or repurpose the existing code. On this occasion, I found two independent open source workflow libraries that would solve my challenge.

Andrii’s Ultimate Workflow Toolkit and Jason’s CRM DateTime Workflow Utilities both have Add Business Days methods though the approach and the parameters are slightly different. Both take existing date and number of days to add as an input, and have the result as an output but that’s where the similarities end.

CRM DateTime Workflow Utilities

The AddBusinessDays activity takes Holiday/Closure Calendar parameter that you can point to a holiday calendar specific for your organization / department / business unit. This is the same calendar that can be used in service scheduling to define availability of the resources. The only downside of the activity implementation is hard-coded Saturdays and Sundays as non-business days. It works in 99% of the cases but on odd occasion business needs to include some of the weekends.

Ultimate Workflow Toolkit

The AddBusinessDays activity in this toolkit does not use holiday calendar but instead takes extra two parameters: weekend days (as a string of numbers from 0 to 6 (Sun to Mon) delimited by ‘|’) and a Fetchxml expression that would return days to skip (holidays). This “developer-friendly” approach works well if your organization does not use calendars but instead keeps holidays in a custom entity.

Waiting

If you adding business days only with the purpose of waiting until the certain number of days passes e.g. remind customer of an unpaid invoice after 5 business days then you might want to consider codeless approach using SLAs.

Thanks to Andrii “Granny’s Moonshine” Butenko and Jason “I can make a kettle talk to CRM” Lattimer for their awesome contributions – I use their toolkits on a daily basis.

(Facebook and Twitter cover photo by Curtis MacNewton on Unsplash)

Leave a Reply

Your email address will not be published.