Whereas validation of user input is one side of the medal, the other is validation of code entered data, the subject of this blog post.
Code Entered Data
Just as any user input code entered data should be validated before it is allowed to be recorded in the database. A standard way of entering data through code is by the assignment statement:
“Search Name” := Name;
Like in the OnValidate trigger of the Name field on the G/L Account table, where the Search Name field of a record will be assigned the content of the Name field of that record.
By the basic nature of C/SIDE an assignment statement can only be created if the data types match on the both sides of the assignment statement. (Read more about this here.) Hereby data type validation is not needed in run-time, although even if data types match a run time error could occur due to an overflow. (Read more about this here.) . Running the code will however not trigger any (additional) validation.
Now how to trigger additional validations as discussed in my blog regarding the validation of user input? By using the VALIDATE field method.
VALIDATE field method
As the Developer and IT Pro help explains, the syntax for the VALIDATE function is:
Record.VALIDATE(Field [, NewValue])
Where Record (data type: record; subtype: any NAV table) contains the field Field you want to validate. The optional parameter NewValue is the value to be inserted in Field. Needless to say that the Data Type of NewValue must match the data type of Field.
Calling VALIDATE altogether launches a two step process in the following order:
- System Validation
- Field Validation
Thus before the OnValidate trigger of the field is executed any system validation will be carried out, like checking of table relations based on the settings of the TableRelation and ValidateTableRelation properties. I.e. the same details apply as have been described here.
The second parameter allows to combine two statements in one, as:
is equal to:
Record.Field := NewValue;
- Like Mark Brummel also mentions in his post Tip #7 – SQL Range Locks, I have often come across code where VALIDATE has been used as a secure way of assigning a new value to a field. Even though a simple assignment statement was all that was needed. You have to know that a VALIDATE comes with a price. In one of my next blogs will address this issue.
- No form control validation is executed due to a call to VALIDATE.
- Microsoft Dynamics NAV 2009 Developer and IT Pro Documentation
- A note on using VALIDATE with respect to SQL Server can be found on Waldo’s blog: What impact does my C/AL have on SQL?.
There are, various MS Dynamics partners have build their solutions to do that.