Compiling objects and error handling
The C/AL code in NAV objects is not executable itself. Before a C/AL object can be used in the application, it must be compiled into binary code that can be run.
The C/AL compiler is a part of the C/SIDE development environment, and can be run either from the object designer or in the code editor while writing the application code.
How to do it...
- Open NAV object designer and create a new codeunit.
- In the
OnRun
trigger, declare a local variableCurrDate
of type Integer:Name
DataType
CurrDate
Integer
- Add two code lines in the
OnRun
trigger:CurrDate := TODAY; MESSAGE('Today is %1,CurrDate);
- The preceding code contains two errors. Let's try to compile it. Press F11 or click Compile in the Tools menu to compile the code. A message will inform you about an incorrect type conversion and position the cursor in the line containing the first error.
- Click Save in the File menu, assign an ID and name to the new codeunit, uncheck the Compiled checkmark in the dialog box, and click OK:
- The object is saved without compiling. The Compiled column in object designer does not have a checkmark, which indicates that the object is uncompiled:
Note
Uncompiled objects cannot be executed.
- Select the codeunit
50002
and click Run in the object designer. An error message will inform you that the object must be saved in the compiled form before it can be run. - Close the message dialog and click Design.
- To fix the first error in the code, open C/AL Locals in the
OnRun
trigger and change the data type of theCurrDate
variable. Replace Integer with Date. - After fixing the error, Click Compile. This time, compilation will stop on the second line informing you about a syntax error.
- Insert the missing apostrophe to close the text constant and fix the erroneous line. The following is the correct function code:
CurrDate := TODAY; MESSAGE('Today is %1',CurrDate);
- Save the object, this time with the Compiled option, and run it from the object designer.
How it works...
Each of the two lines at the beginning of this exercise contains an error. In the first line we are assigning a date to an integer variable, and the second line is missing an apostrophe closing the text constant. When an object is compiled, only the first error is shown if the compilation breaks. Errors must be fixed one by one.
In Step 5 we are saving the object without compiling. This option is often used in the middle of the development process, when the object is incomplete and not syntactically correct yet.
It is not necessary to open objects in the code editor to compile them one at a time. It is possible to compile multiple objects in one batch. Select all objects you want to compile in the object designer and press F11 - it will run compilation for all selected objects. If several objects are selected and there are compilation errors in any of them, the list of errors will be displayed in a summary table.