When to Use WHEN-VALIDATE-RECORD and When to Use WHEN-VALIDATE-ITEM in Oracle D2k / Oracle forms
The need to use both WHEN-VALIDATE-RECORD and WHEN-VALIDATE-ITEM is quite often felt in coding applications where independent validations are to be done, at the record and item levels. As a special case of this, validation between interdependent items is best done using a WHEN-VALIDATE-RECORD trigger.
You should use a WHEN-VALIDATE-RECORD trigger to perform validation when control moves out of the whole record and not when it just moves out of an item in the record.
Use a WHEN-VALIDATE-ITEM trigger to perform validation when control moves out of an item. This also implies that the control might move out of the record in the process. But the subtle difference is WHEN-VALIDATE-RECORD fires only when the control exits the whole record. It does not fire while tabbing between items in the same record.
How Does the Validation Unit Property Influence This Behavior?
Setting the Validation Unit property to Record defers the validation process until the control navigates out of the record. This is similar to the behavior of a WHEN-VALIDATE-RECORD trigger except that it also postpones the firing of the WHEN-VALIDATE-ITEM trigger until this point. At this time, WHEN-VALIDATE-ITEM fires followed by WHEN-VALIDATE-RECORD.
Two or more items interdependent on one another like a type item, a subtype item, and a value item dependent on these two. Three WHEN-VALIDATE-ITEM triggers, one for each type, subtype, and value items would suffice. However they would involve complex coding to ensure correct values and to restore correct values when the user enters the first time and then goes back and changes the type and/or subtype items without changing the value item. The WHEN-VALIDATE-ITEM for the value item does not fire because its value hasn’t been changed since it has been entered. So the code for the WHEN-VALIDATE-ITEM of value item has to be repeated in each of the WHEN-VALIDATE-ITEMS of type and sub-type items.
Imagine the complexity when there are more than three items. A WHEN-VALIDATE-RECORD trigger, on the other hand, would require only one combined validation involving all three items.
Writing individual WHEN-VALIDATE-ITEM triggers for each of the interdependent items requires the code performing the combined validation to be repeated in each trigger. This increases the complexity of coding when a lot of these items are involved. Also the probability of error is greater in this case.
On the other hand, writing a WHEN-VALIDATE-RECORD trigger requires only doing the combined validation once, and this method is completely error-proof.