Using the date effectiveness feature
Date effectiveness is a new feature in Dynamics AX 2012 allowing developers to easily create date range fields. Date ranges are used for defining record validity between the specified dates, for example, defining employee contract dates.
This feature significantly reduces the amount of time that developers spend writing code and also provides a consistent approach to implement data range fields.
This recipe will demonstrate the basics of date effectiveness. We will implement date range validation on the standard E-mail templates form.
How to do it...
Carry out the following steps in order to complete this recipe:
1. In the AOT, find the SysEmailTable table and change its property as shown in the following table:
Property
Value
ValidTimeStateFieldType
Date
2. Notice the two new fields that are automatically added to the table:
3. Add the newly created ValidFrom and ValidTo fields to the existing emailIdIdx index and change the properties shown in the following table:
Property
Value
AlternateKey
Yes
ValidTimeStateKey
Yes
ValidTimeStateMode
NoGap
4. The index should look similar to the following screenshot:
5. Next, add the ValidFrom and ValidTo fields to the table's Identification group:
6. In the AOT, find the SysEmailTable form, refresh it using the Restore command which can be found in the form's right-click context menu. Then, locate its data source named SysEmailTable and change its properties as follows:
Property
Value
ValidTimeStateAutoQuery
DateRange
ValidTimeStateUpdate
Correction
7. In order to test the results, navigate to Organization administration | Setup | E-mail templates and notice the newly created fields: Effective and Expiration columns. Try creating records with the same E-mail ID and overlapping date ranges—you will notice how the system is proposing to maintain valid date ranges:
How it works...
We start the recipe by setting the ValidTimeStateFieldType property to Date on the SysEmailTable table. This automatically creates two new fields: ValidFrom and ValidTo that are used to define a date range.
Next, we add the created fields to the primary index where the EmailId field is used and adjust the following index's properties:
We set the AlternateKey property to Yes to ensure that this index is a part of an alternate key.
We set the ValidTimeStateKey property to Yes to specify that the index is used to determine the valid date ranges.
We also set the ValidTimeStateMode property to NoGap to ensure that e-mail templates with the same identification number can be created within continuous periods. The property can also be set to Gap allowing non-continuous date ranges.
Finally, we adjust the SysEmailTable form to reflect the changes. We add the newly created ValidFrom and ValidTo fields to the SysEmailTable table's Identification group so that they automatically appear on the form's Overview grid. We also change a few properties of the SysEmailTable data source:
Set the ValidTimeStateAutoQuery property to DateRange to ensure that all records are visible. The default AsOfDate value could be used if we want to display only the records for the current period.
Set the ValidTimeStateUpdate property to Correction, allowing the user to modify the dates.
There's more...
Forms with date effective records can be enhanced with an automatically-generated toolbar for filtering the records. This can be done with the help of the DateEffectivenessPaneController
application class.
In order to demonstrate that, let's modify the previously used SysEmailTable form and add the following code to the bottom of the form's init()
method:
DateEffectivenessPaneController::constructWithForm( this, SysEmailTable_ds);
Now when you open the form, it contains an automatically-generated date effectiveness filter at the top: