Preprocessor Directives in AL

APPLIES TO: Business Central 2020 release wave 2 and later

In AL, like in other programming languages, preprocessor directives can be used to make code conditional, to suppress warnings, or to enable expand and collapse in code. Preprocessor directives can be divided into the following groups. For more information about each type, use the links provided below.

Any code can be made conditional, including table fields, and checked using a conditional directive. To check code using a conditional directive, you must define a symbol to check. A symbol returns a boolean value; true or false. Symbols can be defined at the beginning of a source file and the scope of the specific symbol is the file that it is defined within. You can also define symbols in the app.json file, and then the scope is global for the extension.

Note

Built-in symbols are currently not supported in AL. Symbols must be defined in a specific file or in the app.json file.

Conditional directives

The following conditional preprocessor directives are supported in AL.

Conditional preprocessor directive Description
#if Specifies the beginning of a conditional clause. The #endif clause ends it. Compiles the code between the directives if the specified symbol being checked is defined.
#else Specifies a compound conditional clause. If none of the preceding clauses evaluates to true, the compiler will evaluate code between #else and #endif.
#elif Combines else and if. If #elif is true the compiler evaluates all code between #elif and the next conditional directive.
#endif Specifies the end of a conditional clause that begins with #if.
#define Defines a symbol that can be used to specify conditions for a compilation. For example, #define DEBUG. The scope of the symbol is the file that it was defined in.
#undef Undefines a symbol.

Defining preprocessorSymbols

Symbols can be defined globally in the app.json file. A symbol can also be defined using the #define directive in code, but if symbols are defined in the app.json file, they can be used globally. The following example defines DEBUG as a global symbol. This can then be used from code as illustrated in the Conditional code example below. A symbol has a boolean value, that means it evaluates to true or false.

The app.json syntax is:

"preprocessorSymbols": [name [,name2]]

For example:

"preprocessorSymbols": [ "DEBUG","PROD"]

For more information, see JSON Files.

Example

#if DEBUG
    trigger OnOpenPage()
    begin
        Message('Only in debug versions');
    end;
#endif

See Also

Development in AL
AL Development Environment
Conditional directives
Region Directive in AL
Pragma Directive in AL
Deprecating Explicit and Implicit With Statements
Best Practices for Deprecation of Code in the Base App
ObsoleteState Property
ObsoleteReason Property
ObsoleteTag Property
Obsolete Attribute