FIELDERROR

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 NAV logic, especially in the Posting Codeunits (for example, Codeunits 12, 80, 90). The syntax is as follows:

TableName.FIELDERROR(FieldName[,OptionalMsgText]);

If we include our own message text by defining a Text Constant in the C/AL Globals | Text Constants tab (so 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 message from FIELDERROR, as shown in the following screenshot:

An error message that simply identifies the data field, but does not reference a message text looks like the following screenshot, with the record key information displayed:

Because the error message begins with the name of the field, we will need to be careful that our Text Constant is structured to make the resulting error message easy to read.

If we don't include our own message text, the default message comes in two flavors. The first instance is the case where the referenced field is not empty. Then the error message presumes that the error is due to a wrong value, as shown in the previous screenshot. In this case, where the referenced data field is empty, the error message logic presumes the field should not be empty, as shown in the following screenshot:

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.117.81.240