Another function, which is very similar to the TESTFIELD function, is FIELDERROR. However, where TESTFIELD performs a test and terminates with either an error or an OK result, FIELDERROR presumes that the test was already performed and the field failed the test. FIELDERROR is designed to display an error message and then terminate the process. This approach is followed in much of the standard Business Central logic, especially in the posting codeunits (for example, codeunits 12, 80, and 90). The syntax is as follows:
TableName.FIELDERROR(FieldName[,OptionalMsgText]);
If we include our own message text by defining a text constant in the variable section (so that the message can be multilingual), we will have the following line of code:
Text001 must be greater than Start Time
Then, we can reference the text constant in code as follows:
IF Rec."End Time" <= "Start Time" THEN Rec.FIELDERROR("End Time",Text001);
The result is an error...