DateTime AddMonths

I recently encountered a weird workflow behaviour that was caused due to my misunderstanding of how DateTime.AddMonths works. The issue happened with a recurring workflow. Since there is no recurring workflow functionality OOB in CRM there are couple of approaches to do this.

  1. Scheduling recurring Dynamics CRM workflows with FetchXML
  2. Asynchronous Batch Process Solution

I have used a slighly modified version of these approaches to do the recurring workflow functionality. There is only one instance of workflow running every day, and it basically uses fetchxml to grab records that meet a certain criteria and sends out notifications. One of my criteria is “start date 6 months before today” used for 6 month notification. But when you use DateTime.Now.AddMonths(-6) to populate the start date condition, I encountered this behavior.

Here is a quick summary of the issue

Current Date Code Expected Actual
28/08/2015 DateTime.Today.AddMonths(-6).ToString(“s”) 28/02/2015 28/02/2015
29/08/2015 DateTime.Today.AddMonths(-6).ToString(“s”) 01/03/2015 28/02/2015
30/08/2015 DateTime.Today.AddMonths(-6).ToString(“s”) 02/03/2015 28/02/2015
31/08/2015 DateTime.Today.AddMonths(-6).ToString(“s”) 03/03/2015 28/02/2015

So, when the workflow is running on these 4 days from 28/08/2015 to 31/08/2015 and calculating 6 months before the current date, it will produce the same date. As a result when the calculated date is used in a fetch condition as a filter, it will pick the same record(s) on these 4 dates.

The solution to fix your definition of month to 30 days, if that is acceptable. In my case it was OK, as all I did was send out notifications.

tldr; If the resulting day is not a valid day in the resulting month, the last valid day of the resulting month is used



Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s