Validating Data #2: Using VALIDATE

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:

  1. System Validation
  2. 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.

NewValue parameter

The second parameter allows to combine two statements in one, as:

Record.VALIDATE(Field, NewValue);

is equal to:

Record.Field := NewValue;
Record.VALIDATE(Field);

Notes

  • 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.

More Information

2 Comments

  1. There are, various MS Dynamics partners have build their solutions to do that.

  2. Pingback: Validating Data #5: Data- and XMLports – Van Vugt's DynamiXs

Leave a Reply to Adminflux Cancel reply

Your email address will not be published. Required fields are marked *