We have introduced a couple of new concepts and statements in this recipe.
The switch statement should always be used on the validateField method, even if we only ever intend to handle one case. An if statement might seem easier, but it will make the code less maintainable. This goes for any check like this, where the cases have the possibility to increase.
The next new concept is that we can now declare variables as we need them. This helps with scope, but shouldn't be overused. The initValue and validateField methods are good examples of explaining where the code should be declared.
The AX 2012 systemGetDate() function is deprecated in this release. DateTimeUtil provides better handling for time zones. The date can be different across time zones, and can differ between the client's machine (the browser) and the server where SCM is hosted. With SCM the user is completely unaware of where the server is, and could be working anywhere in the world.
In the validateField method, we will allow the standard code to run first; the standard call will validate the following:
- That the value is valid for the type, such as a valid date in a date field.
- If the field is a foreign key, check the value exists in the parent table.
- If the field is mandatory, check that it is filled in or that it is not zero for numeric and enum fields.