I am pleased to announce the CRM WF Date/Time Utilities. It consists of three custom workflow activities that help extend the limitations of Date/Time fields in CRM. Together these utilities can be pretty powerful when working with Date/Time fields in CRM Workflows. First, lets go over each of the three custom workflow activities.
Get Date Parts
This utility will take in a Date/Time field and return all the parts of the Date/Time such as the day, month, year, hour, minute, second, and millisecond. This is useful for a Check Condition in a workflow if you need to check for a specific time or date of the field.
Set Date Parts
This utility will take in a Date/Time field and allow a specific part of the Date/Time field to be overwritten. For example, you could always set a Date/Time to 5pm by passing in the Date/Time field and 5 for the "Hour" parameter of this workflow activity.
Add Date Parts
This utility is similar to “Set Date Parts” but instead of overriding a part of the Date/Time field, it will add a date part onto the existing Date/Time field. In the above example, if you pass in 5 for the "Hour" parameter, it will add 5 hours to the existing Date/Time rather than setting the Time to 5pm.
With that, here’s an example workflow that shows off some of the power of the Date/Time Utilities.
Birthday Fix Workflow
The goal of this workflow is to workaround the native time zone issue when storing Date fields in CRM. If you use a Date only field such as Birthdate, CRM will store the time as midnight in the user's local time zone. The issue with this is that, for example, if a user who updates the Birthdate field is in Central time zone and a user in Mountain time zone views the Birthdate, it will be one day earlier. This is because the Date/Time field is stored as midnight Central time which is 11 PM Mountain time the day before.
With the SonomaPartners.DateTimeUtilities, we can work around the time zone issue with a workflow using four simple steps.
1) First add a step for GetDateParts passing the Birthdate field into the custom activity.
2) Next use a Check Condition on the result of the GetDateParts custom activity. We want to check if the "Hour" is NOT set to 12. This way the Birthdate will not be updated multiple times if the Hour is already set to Noon.
3) Add a step for a successful Check Condition from step 1. This step will call SetDateParts passing in the Birthdate field and also with the "Hour" argument set to 12. This will overwrite the Midnight Date/Time field and set it to Noon.
Note: The default value of each argument is -2147483648 (the minimum value of an integer), therefore if you update the arguments after the first save, you will see each previous null value set to -2147483648 which is intended.
4) Lastly, add a native workflow step to Update the Contact record setting the Birthdate field to the result of the SetDateParts activity. This will set the Birthdate to Noon which gives some buffer so that users in other time zones will still see the same Date rather than the day before or the day after.
The finished workflow:
When Microsoft releases the ability to deploy custom workflow assemblies to CRM Online, these Date/Time Utilities could provide some great flexibility. By utilizing the utilities, a scheduled process could be built for CRM Online using a workflow with a few steps.
1) Create a workflow that is marked as on-demand and a child process.
2) Add a step to execute a custom workflow activity that performs a desired operation.
3) Pass the Process Execution Time into the AddDateParts custom activity and set the “Day” argument to 1
4) Create a Wait condition and set the Process Timeout equal to the result of the AddDateParts activity.
5) Kick off the same workflow as a child process
This workflow can then be triggered on-demand and will wait until the next day to trigger again. A SetDateParts activity could be used after the AddDateParts activity to set a specific time for this workflow to trigger.
Source and Download
The workflow assembly and source code for the Date/Time Utilities can be found on CodePlex. Both CRM 4.0 and 2011 versions are available. Head there and follow the project for any updates. As always, any feedback is appreciated!