Today's puzzle came from a friend of mine David Jubber at BMI Healthcare - thanks David !
QUESTION: What happens if you tick ON a check box but during the FieldEdit event an ERROR is thrown?
OPTIONS: Would you expect it to display the error message and...
(A) leave the check box ticked ON, or
(B) leave the check box unticked
Figure 1 shows the PeopleCode behind the check box and I'm simply throwing an Error if the user is gsmith.
|Fig 1. Code to validate the change of CheckBox|
|Fig 2. Error Message after check box ticked ON|
ANSWER: The answer is OPTION (A). The tick is left ON even though the Edit (validation) fails the Edit test.
Is this what you were expecting? It might not behave quite as you wanted. After all ... the edit failed.... so clearly I shouldn't be allowed to tick the check box on. So, let's try and code around this behaviour and undo the tick action with what seems like some obvious PeopleCode. See Fig 3.
|Fig 3. Code to revert the tick action|
|Fig 4. Illegal operation to set current field value during FieldEdit|
WorkaroundsOne possible solution might be to move the code from FieldEdit to FieldChange, use a MessageBox function instead of ERROR and handle the "exit error" from the event manually. FieldChange allows the setting of current field and can therefore reset the checkbox value to OFF.
Interestingly, Radio Buttons do not behave the same. This next example uses the same field and the same code but the radio button change is reset to it's original value if the Edit fails.
|Fig 5. Radio buttons behave as OPTION (B)|
- Which one do you prefer?
- I wonder why they behave differently?
- Can anyone suggest another solution to revert the check box on ERROR?
- Why doesn't the checkbox get highlighted red?
NOTE: I'm not responsible for the label typo on field object PTCHKBX. That's a delivered PeopleTools object. :-)